javaspring-bootazure-blob-storageazure-file-shareazure-file-copy

Moving a file from Azure Blob Container to Azure File Share in Springboot Application


I have a CloudBlobContainer which contains a file (csv) which I want to move to a Azure File Share. In the current approach I am downloading the file stored in the container to a temp file and then uploading the same temp file to the File Share.

CloudBlockBlobsourceBlob = container.getBlockBlobReference(sourceFilePath);
            CloudFile destFile = cloudDir.getFileReference(destFileName);

            File tmpCsvFile = Files.createTempFile(destFileName, "")
                    .toFile();

            sourceBlob.downloadToFile(tmpCsvFile.getAbsolutePath().substring(0,
                    tmpCsvFile.getAbsolutePath().lastIndexOf(File.separator)) + File.separator + destFileName);

            destFile.uploadFromFile(tmpCsvFile.getAbsolutePath().substring(0,
                    tmpCsvFile.getAbsolutePath().lastIndexOf(File.separator)) + File.separator + destFileName);

I was wondering if I could directly move/copy the file without needing to download it because for larger files it might take a lot of time at this part of the code.

I came across the .startCopy and tried to use it as below. The storage details, file paths/names are all same as the previous approach.

CloudBlockBlob sourceBlob = container.getBlockBlobReference(sourceFilePath);
CloudFile destFile = cloudDir.getFileReference(destFileName);
if (!destFile.exists()) {
    destFile.create(1024);
}
destFile.startCopy(sourceBlob);

With these changes I am getting the below exception.

com.microsoft.azure.storage.StorageException: The specified resource does not exist. " at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:89) ~[azure-storage-5.0.0.jar!/:?]" " at com.microsoft.azure.storage.core.StorageRequest.materializeException(StorageRequest.java:305) ~[azure-storage-5.0.0.jar!/:?]" " at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:175) ~[azure-storage-5.0.0.jar!/:?]" " at com.microsoft.azure.storage.file.CloudFile.startCopy(CloudFile.java:472) ~[azure-storage-5.0.0.jar!/:?]" " at com.microsoft.azure.storage.file.CloudFile.startCopy(CloudFile.java:362) ~[azure-storage-5.0.0.jar!/:?]" " at com.microsoft.azure.storage.file.CloudFile.startCopy(CloudFile.java:320) ~[azure-storage-5.0.0.jar!/:?]"


Solution

  • Moving a File from Azure Blob Container to Azure File Share in Spring Boot Application

    You can use the code below to copy a file from Azure Blob Storage to Azure File Share using Java.

    Code:

    public class App {
        public static void main(String[] args) {
            String connectionString = "xxxxx";
            String sourceContainerName = "test";
            String sourceBlobName = "titanic.csv";
            String destinationShareName = "share1";
            String destinationDirectoryName = "sample";
            String destinationFileName = "test.csv";
    
            // Create a BlobServiceClient object to connect to your Azure Storage account
            BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
                .connectionString(connectionString)
                .buildClient();
    
            BlobContainerClient sourceContainerClient = blobServiceClient.getBlobContainerClient(sourceContainerName);
    
            ShareFileClient fileClient = new ShareFileClientBuilder().connectionString(connectionString).shareName(destinationShareName).resourcePath(destinationDirectoryName + "/" + destinationFileName).buildFileClient();
    
            BlobClient sourceBlobClient = sourceContainerClient.getBlobClient(sourceBlobName);
            String sourceBlobUrl = sourceBlobClient.getBlobUrl();
    
            SyncPoller<ShareFileCopyInfo, Void> poller = fileClient.beginCopy(sourceBlobUrl, (Map<String, String>) null, Duration.ofSeconds(2));
            PollResponse<ShareFileCopyInfo> pollResponse = poller.poll();
            while (!pollResponse.getStatus().isComplete()) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("Thread interrupted.");
                    return;
                }
            }
            if (pollResponse.getStatus().isComplete() && pollResponse.getStatus() != null) {
                ShareFileCopyInfo value = pollResponse.getValue();
                System.out.printf("Copy source: %s. Status: %s.%n", value.getCopySourceUrl(), value.getCopyStatus());
            } else {
                System.out.println("Copy operation failed.");
            }
        }
    }
    

    Dependency:

    </dependency>
        <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-storage-blob</artifactId>
        <version>12.25.2</version>
    </dependency>
    
    <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-storage-file-share</artifactId>
      <version>12.21.4</version>
    </dependency>
    

    Output:

    Copy source: https://venkat123.blob.core.windows.net/test%2Ftitanic.csv. Status: success.
    

    File Share Screenshot

    After copying the file from Azure Blob to File Share, you can delete the source blob.

    Portal: Portal Screenshot

    Reference: Azure File Share client library for Java | Microsoft Learn