问题描述
我的源虚拟文件夹中有一个Blob,我需要将源Blob移到另一个虚拟文件夹中,并使用azure函数app删除该源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).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;
}
}
解决方法
如果要复制Blob并将其删除,请参考以下步骤
- 列出带有前缀的blob。前缀用于过滤结果,以仅返回名称以指定前缀开头的Blob
private static async Task< List<BlobItem>> ListBlobsHierarchicalListing(BlobContainerClient container,string? prefix,int? segmentSize)
{
string continuationToken = null;
var blobs = new List<BlobItem>();
try
{
do
{
var resultSegment = container.GetBlobsByHierarchyAsync(prefix: prefix,delimiter: "/")
.AsPages(continuationToken,segmentSize);
await foreach (Page<BlobHierarchyItem> blobPage in resultSegment)
{
foreach (BlobHierarchyItem blobhierarchyItem in blobPage.Values)
{
if (blobhierarchyItem.IsPrefix)
{
Console.WriteLine("Virtual directory prefix: {0}",blobhierarchyItem.Prefix);
var result = await ListBlobsHierarchicalListing(container,blobhierarchyItem.Prefix,null).ConfigureAwait(false);
blobs.AddRange(result);
}
else
{
Console.WriteLine("Blob name: {0}",blobhierarchyItem.Blob.Name);
blobs.Add(blobhierarchyItem.Blob);
}
}
Console.WriteLine();
// Get the continuation token and loop until it is empty.
continuationToken = blobPage.ContinuationToken;
}
} while (continuationToken != "");
return blobs;
}
catch (RequestFailedException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
- 复制并删除
private static async Task CopyAndDeltet(BlobItem item,BlobContainerClient des,BlobContainerClient source,string newFolder,string sasToken) {
try
{
var sourceBlobUrl = source.GetBlobClient(item.Name).Uri.ToString() + "?" + sasToken;
var desblobName = newFolder + item.Name.Substring(item.Name.IndexOf("/"));
var operation = await des.GetBlobClient(desblobName).StartCopyFromUriAsync(new Uri(sourceBlobUrl)).ConfigureAwait(false);
var response = await operation.WaitForCompletionAsync().ConfigureAwait(false);
if (response.GetRawResponse().Status == 200)
{
await source.GetBlobClient(item.Name).DeleteAsync().ConfigureAwait(false);
}
}
catch (Exception)
{
throw;
}
}
private static string generateSAS(StorageSharedKeyCredential cred,string containerName,string blobName) {
var sasBuilder = new BlobSasBuilder()
{
BlobContainerName = containerName,BlobName = blobName,StartsOn = DateTime.UtcNow.AddMinutes(-2),ExpiresOn = DateTime.UtcNow.AddHours(1)
};
sasBuilder.SetPermissions(BlobSasPermissions.Read);
return sasBuilder.ToSasQueryParameters(cred).ToString();
}
此外,请注意,如果要复制大量Blob或大尺寸Blob,则Azure函数不是一个好的选择。 Azure功能仅可用于运行一些短期任务。建议您使用Azure data factory或Azcopy。