【问题标题】:How to copy Azure storage files/directories using java azure-storage-file-datalake如何使用 java azure-storage-file-datalake 复制 Azure 存储文件/目录
【发布时间】:2021-01-10 01:54:43
【问题描述】:

我使用 azure-storage-file-datalake for java 在我的 Azure 存储帐户上进行文件系统操作,我可以打开文件、删除甚至重命名/移动文件或目录。

我找不到任何将文件/文件夹复制到其他位置的方法。

这就是我重命名/移动文件/目录的方式:

    DataLakeServiceClient storageClient = new DataLakeServiceClientBuilder().endpoint(endpoint).credential(credential).buildClient();
    DataLakeFileSystemClient dataLakeFileSystemClient = storageClient.getFileSystemClient("storage");
    DataLakeFileClient fileClient = dataLakeFileSystemClient.getFileClient("src/path");
    fileClient.rename("storage", "dest/path");

有没有其他方法可以使用azure-storage-file-datalake SDK 甚至azure-storage-blob SDK 来复制文件或目录?

【问题讨论】:

    标签: java azure azure-storage azure-data-lake azure-storage-files


    【解决方案1】:

    我找到了一种使用 com.azure.storage.blob.BlobClient 在同一存储帐户中复制 blob(文件)的方法。

    使用beginCopy方法如下:

    BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().endpoint(spnEndpoint).credential(credential).buildClient();
    BlobContainerClient blobContainerClient =  blobServiceClient.getBlobContainerClient("containerName");
    BlobClient dst = blobContainerClient.getBlobClient("destination/blob");
    BlobClient src = blobContainerClient.getBlobClient("src/blob");
    dst.beginCopy(src.getBlobUrl(), null);
    

    【讨论】:

      【解决方案2】:

      您可以使用azure-sdk-for-java复制文件

      String CONNECT_STRING = "DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account key>;EndpointSuffix=core.windows.net";
      String SHARE_NAME = "share-name";
      
      ShareFileClient client = new ShareFileClientBuilder()
              .connectionString(CONNECT_STRING)
              .shareName(SHARE_NAME)
              .resourcePath("path/to/target/file.txt")
              .buildFileClient();
      
      String srcFile = "https://<account-name>.file.core.windows.net/share-name/path/to/source/file.txt";
      SyncPoller<ShareFileCopyInfo, Void> poller = client.beginCopy(srcFile, null, Duration.ofSeconds(2));
      final PollResponse<ShareFileCopyInfo> pollResponse = poller.poll();
      final ShareFileCopyInfo value = pollResponse.getValue();
      System.out.printf("Copy source: %s. Status: %s.%n", value.getCopySourceUrl(), value.getCopyStatus());
      

      以上示例使用连接字符串创建文件客户端,您也可以使用 SAS 令牌创建文件客户端,详见 java doc ShareFileClientBuilder。连接字符串和/或 SAS 令牌都可以在 Azure 门户中的存储帐户页面上使用。

      【讨论】:

        【解决方案3】:

        【讨论】:

        • 我并不是要完全使用rename 方法来复制文件。我正在寻找用于在这些 java 客户端中复制文件/文件夹的任何其他方法。你有什么建议吗?
        • 目前尚不清楚这个答案想说什么。您打算用文档的两个参考来说明什么?
        猜你喜欢
        • 2022-11-20
        • 1970-01-01
        • 1970-01-01
        • 2021-02-24
        • 2021-10-26
        • 2021-02-05
        • 2020-03-12
        • 2017-07-11
        • 2022-11-02
        相关资源
        最近更新 更多