第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

在 java 和 azure blob storage sdk 中上傳帶有子目錄的目錄

在 java 和 azure blob storage sdk 中上傳帶有子目錄的目錄

茅侃侃 2022-05-25 15:54:21
我使用此代碼將文件上傳到 azure blob 存儲,但是當(dāng)我嘗試使用子目錄加載目錄時(shí)出現(xiàn)錯(cuò)誤“遇到 FileNotFoundException:C:\upload\bin”:(訪問被拒絕),是加載文件的任何解決方案嗎?源目錄中的目錄?try {        CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);        CloudBlobClient serviceClient = account.createCloudBlobClient();        // Container name must be lower case.        CloudBlobContainer container = serviceClient.getContainerReference(containerName);        container.createIfNotExists();        File source = new File(path);        if (source.list().length > 0) {            for (File file : source.listFiles()) {                CloudBlockBlob blob = container.getBlockBlobReference(file.getName());                if (blob.exists() == false) {                    File sourceFile = new File(source + "\\" + file.getName());                    blob.upload(new FileInputStream(sourceFile), sourceFile.length());                    System.out.println("File " + source + "\\" + file.getName() + " Load to blob storage");                } else System.out.println("File " + source + "\\" + file.getName() + " Already exist in storage");            }        } else System.out.println("In folder " + path + " are no files ");    } catch (FileNotFoundException fileNotFoundException) {        System.out.print("FileNotFoundException encountered: ");        System.out.println(fileNotFoundException.getMessage());        System.exit(-1);    } catch (StorageException storageException) {        System.out.print("StorageException encountered: ");        System.out.println(storageException.getMessage());        System.exit(-1);    } catch (Exception e) {        System.out.print("Exception encountered: ");        System.out.println(e.getMessage());        System.exit(-1);    }
查看完整描述

1 回答

?
largeQ

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊

正如@ZhaoxingLu-Microsoft 所說,file生成的對象source.listFiles()足以通過 獲取絕對文件路徑file.getAbsolutePath(),因此您可以編寫如下代碼。


if (blob.exists() == false) {

    blob.uploadFromFile(file.getAbsolutePath());

} else System.out.println("File " + file.getAbsolutePath() + " Already exist in storage");

我在我的環(huán)境中測試你的代碼,它也可以工作。但是,根據(jù)我的經(jīng)驗(yàn),您的問題FileNotFoundException encountered: C:\upload\bin" :(Access is denied)是由于缺少訪問C:或下的文件的權(quán)限引起的C:\upload\bin。因此,您需要在當(dāng)前的 Windows 環(huán)境中以管理員身份運(yùn)行您的代碼,如下圖所示。


圖 1. 如果使用 IntelliJ,請以管理員身份運(yùn)行您的代碼

http://img1.sycdn.imooc.com//628de0cb0001e5e703020257.jpg

圖 2. 如果使用 Eclipse,請以管理員身份運(yùn)行您的代碼

http://img1.sycdn.imooc.com//628de0d70001933b03030213.jpg

圖 3. 通過命令提示符以管理員身份運(yùn)行您的代碼

http://img1.sycdn.imooc.com//628de0e200013b2902810223.jpg

更新:在 Azure Blob 存儲上,文件和目錄結(jié)構(gòu)取決于 blob 名稱。因此,如果您想查看如下圖的文件結(jié)構(gòu),可以使用代碼String blobName = file.getAbsolutePath().replace(path, "");獲取 blob 名稱。

圖 4. 在我的本地機(jī)器上構(gòu)建的文件和目錄結(jié)構(gòu) 

http://img1.sycdn.imooc.com//628de1020001d38f02900200.jpg

圖 5. 通過 Azure 存儲資源管理器在 Azure Blob 存儲上進(jìn)行上述操作 

http://img1.sycdn.imooc.com//628de11300017b5706470105.jpg

這是我的完整代碼。


private static final String path = "D:\\upload\\";

private static final String storageConnectionString = "<your storage connection string>";

private static final String containerName = "<your container for uploading>";


private static CloudBlobClient serviceClient;


public static void upload(File file) throws InvalidKeyException, URISyntaxException, StorageException, IOException {

    // Container name must be lower case.

    CloudBlobContainer container = serviceClient.getContainerReference(containerName);

    container.createIfNotExists();

    String blobName = file.getAbsolutePath().replace(path, "");

    CloudBlockBlob blob = container.getBlockBlobReference(blobName);

    if (blob.exists() == false) {

        blob.uploadFromFile(file.getAbsolutePath());

    } else {

        System.out.println("File " + file.getAbsolutePath() + " Already exist in storage");

    }

}


public static void main(String[] args)

        throws URISyntaxException, StorageException, InvalidKeyException, IOException {

    CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);

    serviceClient = account.createCloudBlobClient();

    File source = new File(path);

    for (File fileOrDir : source.listFiles()) {

        boolean isFile = fileOrDir.isFile();

        if(isFile) {

            upload(fileOrDir);

        } else {

            for(File file: fileOrDir.listFiles()) {

                upload(file);

            }

        }


    }

}


查看完整回答
反對 回復(fù) 2022-05-25
  • 1 回答
  • 0 關(guān)注
  • 180 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號