我的源虚拟文件夹中有一个 blob,我需要将源 blob 移动到另一个虚拟文件夹并使用 azure function app 删除源 blob
将 blob 数据从一个目录复制到另一个目录
删除源 blob
请指导我完成功能应用代码如何将 blob 从一个目录复制到另一个目录并删除 blob
将 blob 复制到另一个目录时遇到一些问题
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;
}
}