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

问题描述

我使用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来复制文件或目录吗?

解决方法

我找到了一种使用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);
,

您可以使用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 门户中的存储帐户页面上使用。

,

azure-storage-file-datalake:

https://docs.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://docs.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

这似乎没有实现这一目标的方法。