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



我使用azure存储文件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-datalakeSDK甚至azure-storage-blobSDK复制文件或目录?

我找到了一种使用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文档ShareFileClientBuilder。连接字符串和/或SAS令牌都可以在azure门户中的存储帐户页面上使用。

azure-storage-file-datalake:

https://learn.microsoft.com/en-us/java/api/com.azure.storage.file.datalake.datalakedirectoryclient.rename?view=azure-java稳定#com_azure_storage_file_talake_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稳定#com_azure_storage_file_talake_DataLakeFileClient_rename_java_lang_String_java_lang_String_

azure-storage-blob

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

最新更新