0
votes

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?

3

3 Answers

1
votes

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);
0
votes

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

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

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.