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

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

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

问题

我使用 azure-storage-file-datalake 来进行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 来复制文件或目录?

英文:

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:

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

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 方法,如下所示:

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);
英文:

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

using beginCopy method as follows:

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

得分: 0

你可以使用 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 文档 ShareFileClientBuilder。连接字符串和/或 SAS 令牌均可在 Azure 门户中的存储账户页面中找到。

英文:

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

String CONNECT_STRING = &quot;DefaultEndpointsProtocol=https;AccountName=&lt;account-name&gt;;AccountKey=&lt;account key&gt;;EndpointSuffix=core.windows.net&quot;;
String SHARE_NAME = &quot;share-name&quot;;

ShareFileClient client = new ShareFileClientBuilder()
        .connectionString(CONNECT_STRING)
        .shareName(SHARE_NAME)
        .resourcePath(&quot;path/to/target/file.txt&quot;)
        .buildFileClient();

String srcFile = &quot;https://&lt;account-name&gt;.file.core.windows.net/share-name/path/to/source/file.txt&quot;;
SyncPoller&lt;ShareFileCopyInfo, Void&gt; poller = client.beginCopy(srcFile, null, Duration.ofSeconds(2));
final PollResponse&lt;ShareFileCopyInfo&gt; pollResponse = poller.poll();
final ShareFileCopyInfo value = pollResponse.getValue();
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:

确定