0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
dev333
  • 713
  • 2
  • 17
  • 38
  • Please edit your question and include the code you have written so far and the issues you are running into. Also if you search for this thing, I am pretty sure that you will find plenty code samples for that. – Gaurav Mantri Sep 21 '22 at 12:01
  • Updated the code in the above question – dev333 Sep 21 '22 at 12:10
  • What's the issue you are running into with your code. – Gaurav Mantri Sep 21 '22 at 12:12
  • Updated the code.. I'm facing the specified blob does not exist error . What should be given as the src blob path and dest blob path ? – dev333 Sep 21 '22 at 12:21

1 Answers1

2

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.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Hi Gaurav .. I'm able to copy the blob from one container to another. But the source container still contains the blob. Ideally it should be deleted right incase of move operation. – dev333 Sep 21 '22 at 13:05
  • Again I need to delete the blob from source container. For this I need to do one more operation. Is there any possibility by using single operation perform the move operation – dev333 Sep 21 '22 at 13:06
  • There is no native move operation. You will need to delete the blob by performing the delete operation on destination blob after the copy operation is completed. – Gaurav Mantri Sep 21 '22 at 13:11
  • Hi Gaurav .. I have posted one question. Can you please look into this URL ? https://stackoverflow.com/questions/73868237/how-to-read-tags-of-an-azure-resource-using-java-sdk – dev333 Sep 30 '22 at 09:45