如何使用Java的azure-storage-file-datalake复制Azure存储文件/目录

huangapple go评论115阅读模式
英文:

How to copy Azure storage files/directories using java azure-storage-file-datalake

问题

我使用 azure-storage-file-datalake 来进行Java编程,以便在我的Azure存储账户上执行文件系统操作,我可以打开文件,删除文件,甚至重命名/移动文件或目录。

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

以下是如何重命名/移动文件或目录的示例代码:

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

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

英文:

I use azure-storage-file-datalake for java to make file system operations on my Azure storage account, I can open files, delete, and even rename/move files or directories.

I can't find any way to copy files/folder to other location.

That's how I rename/move files/directories:

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

Is there any other method I can use to copy files or directories using the azure-storage-file-datalake SDK or even the azure-storage-blob SDK?

答案1

得分: 1

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

使用 beginCopy 方法,如下所示:

  1. BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().endpoint(spnEndpoint).credential(credential).buildClient();
  2. BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("containerName");
  3. BlobClient dst = blobContainerClient.getBlobClient("destination/blob");
  4. BlobClient src = blobContainerClient.getBlobClient("src/blob");
  5. dst.beginCopy(src.getBlobUrl(), null);
英文:

I found a method to copy blobs(files) within the same storage account using com.azure.storage.blob.BlobClient.

using beginCopy method as follows:

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

答案2

得分: 0

你可以使用 azure-sdk-for-java 来复制文件:

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

上述示例使用连接字符串来创建文件客户端,你也可以使用 SAS 令牌来创建文件客户端,详见 Java 文档 ShareFileClientBuilder。连接字符串和/或 SAS 令牌均可在 Azure 门户中的存储账户页面中找到。

英文:

You can use azure-sdk-for-java to copy file

  1. String CONNECT_STRING = &quot;DefaultEndpointsProtocol=https;AccountName=&lt;account-name&gt;;AccountKey=&lt;account key&gt;;EndpointSuffix=core.windows.net&quot;;
  2. String SHARE_NAME = &quot;share-name&quot;;
  3. ShareFileClient client = new ShareFileClientBuilder()
  4. .connectionString(CONNECT_STRING)
  5. .shareName(SHARE_NAME)
  6. .resourcePath(&quot;path/to/target/file.txt&quot;)
  7. .buildFileClient();
  8. String srcFile = &quot;https://&lt;account-name&gt;.file.core.windows.net/share-name/path/to/source/file.txt&quot;;
  9. SyncPoller&lt;ShareFileCopyInfo, Void&gt; poller = client.beginCopy(srcFile, null, Duration.ofSeconds(2));
  10. final PollResponse&lt;ShareFileCopyInfo&gt; pollResponse = poller.poll();
  11. final ShareFileCopyInfo value = pollResponse.getValue();
  12. System.out.printf(&quot;Copy source: %s. Status: %s.%n&quot;, value.getCopySourceUrl(), value.getCopyStatus());

The above example uses connect string to create file client, you can also use SAS token to create file client, see java doc ShareFileClientBuilder for details. Both connect string and/or SAS token are available on your storage account page in azure portal.

答案3

得分: -1

azure-storage-file-datalake:

链接:https://learn.microsoft.com/en-us/java/api/com.azure.storage.file.datalake.datalakedirectoryclient.rename?view=azure-java-stable#com_azure_storage_file_datalake_DataLakeDirectoryClient_rename_java_lang_String_java_lang_String_


链接:https://learn.microsoft.com/en-us/java/api/com.azure.storage.file.datalake.datalakefileclient.rename?view=azure-java-stable#com_azure_storage_file_datalake_DataLakeFileClient_rename_java_lang_String_java_lang_String_

azure-storage-blob

这似乎没有可实现此的方法。

英文:

azure-storage-file-datalake:

https://learn.microsoft.com/en-us/java/api/com.azure.storage.file.datalake.datalakedirectoryclient.rename?view=azure-java-stable#com_azure_storage_file_datalake_DataLakeDirectoryClient_rename_java_lang_String_java_lang_String_


https://learn.microsoft.com/en-us/java/api/com.azure.storage.file.datalake.datalakefileclient.rename?view=azure-java-stable#com_azure_storage_file_datalake_DataLakeFileClient_rename_java_lang_String_java_lang_String_

azure-storage-blob

This seems not to have a method to achieve this.

huangapple
  • 本文由 发表于 2020年9月24日 03:38:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64035152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定