簡體   English   中英

如何使用 java azure-storage-file-datalake 復制 Azure 存儲文件/目錄

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

我使用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 門戶中的存儲帳戶頁面上使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM