问题描述
我使用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:
azure-storage-blob
这似乎没有实现这一目标的方法。