I am able to copy source folder blob to destination folder blob and delete the source blobs how to copy multiple blobs to source to destination folder and delete source blobs
when I am copying source blobs to destination folder I am unable to give a name for a blob in destination folder
public async static void CopyDeleteData(ILogger log) { //copy blobs - from var ConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage"); CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(ConnectionString); CloudBlobClient sourceCloudBlobClient = sourceStorageAccount.CreateCloudBlobClient(); CloudBlobContainer sourceContainer = sourceCloudBlobClient.GetContainerReference("Data"); CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference("SourceFolder/"); CloudBlockBlob targetBlob = sourceContainer.GetBlockBlobReference("SourceFolder/ArchieveFolder/"); var resultSegment = sourceContainer.GetDirectoryReference("SourceFolder/"); var rootDirFolders = resultSegment.ListBlobsSegmentedAsync(true, BlobListingDetails.Metadata,null, null, null, null).Result; try { foreach (var blob in rootDirFolders.Results) { log.LogInformation("Blob " + blob.Uri); await targetBlob.StartCopyAsync(blob.Uri); } } catch (Exception ex) { log.LogError(ex.Message); log.LogError( "Error, source BLOB probably has private access only: " + ex.Message); } await targetBlob.FetchAttributesAsync(); while (targetBlob.CopyState.Status == Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Pending) { log.LogError("Status: " + targetBlob.CopyState.Status); Thread.Sleep(500); await targetBlob.FetchAttributesAsync(); } if (targetBlob.CopyState.Status != Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Success) { log.LogError("Copy failed with status: " + targetBlob.CopyState.Status); } await sourceBlob.DeleteAsync(); log.LogInformation( "Done."); }
When we use
targetBlob.StartCopyAsync(blob.Uri)
to copy a blob to the target blob, we should specify the blob name totargetBlob
. In you code, you use:So
targetBlob
only have the folder directory but not blob name.Please refer to my code below:
After running the code, we can find three txt files are copied from
SourceFolder/
toSourceFolder/ArchieveFolder/
(shown as below two screenshots).