如何使用函数应用程序复制 blob 数据并将其存储在子目录中
how to copy blob data and store it in sub directory by using function app
-
azure-blob-storage
-
azure-functions
-
azure-function-app-proxy
-
azure-functions-runtime
-
azure-function-app
我的源虚拟文件夹中有一个 blob,我需要将源 blob 移动到另一个虚拟文件夹并使用 azure function app 删除源 blob
正在将 blob 数据从 1 个目录复制到另一个目录
正在删除源 blob
请指导我完成 function-app Code 如何将 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;
}
}
如果要复制blob并删除它们,请参考以下步骤
- 列出带前缀的 blob。前缀用于将结果过滤为 return 仅名称以指定前缀
开头的 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 or Azcopy.
azure-blob-storage
azure-functions
azure-function-app-proxy
azure-functions-runtime
azure-function-app
我的源虚拟文件夹中有一个 blob,我需要将源 blob 移动到另一个虚拟文件夹并使用 azure function app 删除源 blob
正在将 blob 数据从 1 个目录复制到另一个目录
正在删除源 blob
请指导我完成 function-app Code 如何将 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;
}
}
如果要复制blob并删除它们,请参考以下步骤
- 列出带前缀的 blob。前缀用于将结果过滤为 return 仅名称以指定前缀 开头的 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 or Azcopy.