Azure functions 如何使用函数app复制blob数据并将其存储在子目录中

Azure functions 如何使用函数app复制blob数据并将其存储在子目录中,azure-functions,azure-blob-storage,azure-function-app,azure-functions-runtime,azure-function-app-proxy,Azure Functions,Azure Blob Storage,Azure Function App,Azure Functions Runtime,Azure Function App Proxy,我的源虚拟文件夹中有一个blob,我需要将源blob移动到另一个虚拟文件夹,并使用azure function app删除源blob 正在将blob数据从1个目录复制到另一个目录 删除源blob 请通过功能应用程序代码指导我如何将blob从一个目录复制到另一个目录并删除blob 在将blob复制到另一个目录时,我遇到了一些问题 public async static void CopyDelete(ILogger log) { var ConnectionStrin

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

  • 正在将blob数据从1个目录复制到另一个目录

  • 删除源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;
            }
            
        }
    

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

  • 列出带有前缀的blob。前缀用于过滤结果,以仅返回名称以指定前缀开头的blob
  • 有关更多详细信息,请参阅和


    此外,请注意,如果要复制大量blob或大尺寸blob,Azure函数不是一个好选择。Azure函数只能用于运行一些短期任务。我建议您使用或。

    您必须先下载blob,然后使用新名称上载blob。若要开始查看它是否对您有用,您可以吗?我在目标文件夹中要复制多个Blob并删除源Blob我是否需要sas来复制和删除中的文件sameContainer@karthikkasula如果是,,您不能创建sas令牌。如何在没有sas的情况下将文件从源文件夹复制到目标文件夹并删除源文件我正在为源文件夹和目标文件夹使用相同的容器。您可以分享一些示例吗
     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();
            }