Malik Kashmiri
Malik Kashmiri

Reputation: 5851

Copy blob from one directory into another in same container Azure Storage

Problem

I am trying to copy the blob from one directory and past into other directory and the delete the blob from source. I already tried the below code but not copy and past into another folder is done but delete operation works.

Code

var cred = new StorageCredentials("storageName", "Key");
var account = new CloudStorageAccount(cred, true);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("containerName");
CloudBlockBlob sourceBlob = container.GetBlockBlobReference("folder/test.jpg");

//this doesn't work
sourceBlob.StartCopyAsync(new Uri("destination url"));
//this works
sourceBlob.DeleteAsync();

Upvotes: 6

Views: 7259

Answers (2)

Zhaoxing Lu
Zhaoxing Lu

Reputation: 6467

Upvotes: 4

Igor Yalovoy
Igor Yalovoy

Reputation: 1725

Well, you have to await async methods. StartCopyAsync fails, because you run a delete file call at the same time. Try using await or Result property.

var cred = new StorageCredentials("storageName", "Key");
var account = new CloudStorageAccount(cred, true);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("containerName");
CloudBlockBlob sourceBlob = container.GetBlockBlobReference("folder/test.jpg");

//this doesn't work
await sourceBlob.StartCopyAsync(new Uri("destination url"));
//this works
await sourceBlob.DeleteAsync();

Code not tested, but should give you a general idea.

Upvotes: 2

Related Questions