簡體   English   中英

在Java和Azure Blob存儲SDK中上載帶有子目錄的目錄

[英]Upload directory with sub-directories in java and azure blob storage sdk

我使用此代碼將文件上傳到azure blob存儲,但是當我嘗試使用子目錄加載目錄時,出現錯誤“遇到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);
    }

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

if (blob.exists() == false) {
    blob.uploadFromFile(file.getAbsolutePath());
} else System.out.println("File " + file.getAbsolutePath() + " Already exist in storage");

我在我的環境中測試您的代碼,它也可以工作。 但是,根據我的經驗, FileNotFoundException encountered: C:\\upload\\bin" :(Access is denied)問題FileNotFoundException encountered: C:\\upload\\bin" :(Access is denied)是由於缺少對C:C:\\upload\\bin下文件的訪問權限所致。因此,您需要運行如下圖所示,以管理員身份在當前Windows環境中執行代碼。

圖1.如果使用IntelliJ,請以管理員身份運行代碼

在此處輸入圖片說明

圖2.如果使用Eclipse以管理員身份運行代碼

在此處輸入圖片說明

圖3.通過命令提示符以管理員身份運行代碼

在此處輸入圖片說明


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

圖4.在本地計算機上構建的文件和目錄結構 在此處輸入圖片說明

圖5.通過Azure Storage Explorer在Azure Blob存儲上的上述操作 在此處輸入圖片說明

這是我完整的代碼。

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);
            }
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM