I have a blob in my source virtual folder and I need to move the source blob to another virtual folder and delete the source blob by using azure function app
copying blob data from 1 directory to another
deleting the source blob
please guide me through function-app Code how to copy blobs from one directory to another and delete the blobs
I am facing some Issues while copying the blobs to another directory
public async static void CopyDelete(ILogger log)
{
var ConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// details of our source file
CloudBlobContainer sourceContainer = blobClient.GetContainerReference("Demo");
var sourceFilePath = "SourceFolder";
var destFilePath = "SourceFolder/DestinationFolder";
CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(sourceFilePath);
CloudBlobDirectory dira = sourceContainer.GetDirectoryReference(sourceFilePath);
CloudBlockBlob destinationblob = sourceContainer.GetBlockBlobReference(destFilePath);
try
{
var rootDirFolders = dira.ListBlobsSegmentedAsync(true, BlobListingDetails.Metadata, null, null, null, null).Result;
foreach (var blob in rootDirFolders.Results)
{
log.LogInformation("Blob Detials " + blob.Uri);
//var sas = sourceBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
//{
// Permissions = SharedAccessBlobPermissions.Read,
// SharedAccessStartTime = DateTimeOffset.Now.AddMinutes(-5),
// SharedAccessExpiryTime = DateTimeOffset.Now.AddHours(2)
//});
// copy to the blob using the
destinationblob = sourceContainer.GetBlockBlobReference(destFilePath);
// var sourceUri = new Uri(blob.Uri);
await destinationblob.StartCopyAsync(blob.Uri);
// copy may not be finished at this point, check on the status of the copy
while (destinationblob.CopyState.Status == Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Pending)
{
await Task.Delay(1000);
await destinationblob.FetchAttributesAsync();
await sourceBlob.DeleteIfExistsAsync();
}
}
if (destinationblob.CopyState.Status != Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Success)
{
throw new InvalidOperationException($"Copy failed: {destinationblob.CopyState.Status}");
}
}
catch (Exception ex)
{
throw ex;
}
}
If you want to copy blob and delete them, please refer to the following steps
For more details, please refer to here , here and here
Besides, please note that if you want to copy a large number of blob or large size blob, the Azure function is not a good choice. Azure function just can be used to run some short time tasks. I suggest you use Azure data factory or Azcopy.