0

我的源虚拟文件夹中有一个 blob,我需要将源 blob 移动到另一个虚拟文件夹并使用 azure function app 删除源 blob

  1. 将 blob 数据从一个目录复制到另一个目录

  2. 删除源 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;
        }
        
    }
4

1 回答 1

0

如果要复制blob并删除它们,请参考以下步骤

  1. 列出带有前缀的 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;
            }
            
        }
  1. 复制和删除
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 数据工厂Azcopy

于 2020-09-17T06:26:15.120 回答