azureazure-storage-accountazure-java-sdk

How to move Azure Storage blob to other container using Java SDK?


I'm trying to move blob from one container to another with in the same storage account. I'm using Java SDK for it.

My code:

StorageSharedKeyCredential credential = new StorageSharedKeyCredential("accountname", "accountkey");
        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().endpoint("storageaccountendpoint").credential(credential).buildClient();
        BlobContainerClient blobContainerClient =  blobServiceClient.getBlobContainerClient("failed");
        BlobClient dst = blobContainerClient.getBlobClient("https://xxxstorage.blob.core.windows.net/success/");
        BlobClient src = blobContainerClient.getBlobClient("https://xxxstorage.blob.core.windows.net/failed/Graphs.jpeg");
        dst.beginCopy(src.getBlobUrl(), null);

I have to move the blob from failed container to success container. But I'm facing 500 internal server error.

What can I try next?


Solution

  • I believe the reason for getting the error is that you are specifying blob URL instead of blob name when creating BlobClient. Also, you were not creating a BlobContainerClient for the source container.

    Please try by changing your code to:

    StorageSharedKeyCredential credential = new StorageSharedKeyCredential("accountname", "accountkey");
    BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().endpoint("storageaccountendpoint").credential(credential).buildClient();
    BlobContainerClient sourceBlobContainerClient =  blobServiceClient.getBlobContainerClient("success");
    BlobContainerClient destinationBlobContainerClient =  blobServiceClient.getBlobContainerClient("failed");
    BlobClient dst = sourceBlobContainerClient.getBlobClient("Graphs.jpeg");
    BlobClient src = destinationBlobContainerClient.getBlobClient("Graphs.jpeg");
    dst.beginCopy(src.getBlobUrl(), null);
    

    Do note that I have created a new BlobContainerClient for the source blob container.