【问题标题】:Upload directory with sub-directories in java and azure blob storage sdk在 java 和 azure blob storage sdk 中上传带有子目录的目录
【发布时间】:2019-06-22 17:37:59
【问题描述】:

我使用此代码将文件上传到 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);
    }

【问题讨论】:

  • 我猜source + "\\" + file.getName() 不包含列出文件的子目录? file还不够,为什么还要构造另一个变量sourceFile
  • @ZhaoxingLu-Microsoft 感谢您的评论

标签: java azure azure-blob-storage


【解决方案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) 是由于缺少访问C:C:\upload\bin 下文件的权限引起的。因此,您需要在当前的 Windows 环境中以管理员身份运行您的代码,如下图所示。

图 1. 如果使用 IntelliJ,请以管理员身份运行您的代码

图 2. 如果使用 Eclipse,请以管理员身份运行您的代码

图 3. 通过命令提示符以管理员身份运行您的代码


更新: 在 Azure Blob 存储上,文件和目录结构取决于 Blob 名称。所以如果你想看到如下图的文件结构,可以使用代码String blobName = file.getAbsolutePath().replace(path, "");获取blob名称。

图 4. 在我的本地机器上构建的文件和目录结构

图 5. 通过 Azure 存储资源管理器在 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);
            }
        }

    }
}

【讨论】:

  • 我的第一个代码完美地适用于有任何错误的上传文件,当我将目录放入子目录时出现错误,我按照@Peter Pan 所说的那样更改我的代码,如下所示:if (blob.exists() == false) { System.out.println(file.getAbsolutePath()); blob.uploadFromFile(file.getAbsolutePath()); } else System.out.println("File " + file.getAbsolutePath()+ " Already exist in storage"); 但它只上传文件
  • @Юрий 是否要上传名为 upload/bin/&lt;filename&gt;&lt;sub-dirctory name under C:\upload\bin&gt;/&lt;filename&gt; 的 blob?
  • 在目录 C:\upload 我有子目录 bin、backup、archive 和一些文件,我想将此结构上传到 blob 存储
  • 我想在 blob 中得到这样的结构: bin/ - file1 - file2 - fileN backup/ - file1 - file2 - fileN `
  • @Юрий 我更新了我的帖子。希望它对您的需求有所帮助。
猜你喜欢
  • 2020-08-15
  • 1970-01-01
  • 2023-04-06
  • 2016-08-06
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2020-03-12
  • 2012-12-29
相关资源
最近更新 更多