首页 > 解决方案 > 如何使用函数应用将文件从源文件夹复制到目标文件夹并删除 Azure 存储帐户中的源文件夹文件

问题描述

我能够将源文件夹 blob 复制到目标文件夹 blob 并删除源 blob 如何将多个 blob 复制到源文件夹到目标文件夹并删除源 blob

  1. 当我将源 blob 复制到目标文件夹时,我无法为目标文件夹中的 blob 命名

      public async static void CopyDeleteData(ILogger log)
    {
        //copy blobs - from
    
        var ConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
        CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(ConnectionString);
        CloudBlobClient sourceCloudBlobClient = sourceStorageAccount.CreateCloudBlobClient();
        CloudBlobContainer sourceContainer = sourceCloudBlobClient.GetContainerReference("Data");        
    
        CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference("SourceFolder/");
        CloudBlockBlob targetBlob = sourceContainer.GetBlockBlobReference("SourceFolder/ArchieveFolder/");
        var resultSegment = sourceContainer.GetDirectoryReference("SourceFolder/");
        var rootDirFolders = resultSegment.ListBlobsSegmentedAsync(true, BlobListingDetails.Metadata,null, null, null, null).Result;
    
        try
        {
            foreach (var blob in rootDirFolders.Results)
            {
            log.LogInformation("Blob    " + blob.Uri);
            await targetBlob.StartCopyAsync(blob.Uri);
    
            }
    
    
        }
        catch (Exception ex)
        {
            log.LogError(ex.Message);
    
            log.LogError( "Error, source BLOB probably has private access only: " + ex.Message);
        }
    
    
        await targetBlob.FetchAttributesAsync();
    
    
        while (targetBlob.CopyState.Status == Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Pending)
        {
            log.LogError("Status: " + targetBlob.CopyState.Status);
            Thread.Sleep(500);
            await targetBlob.FetchAttributesAsync();
        }
    
    
        if (targetBlob.CopyState.Status != Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Success)
        {
    
            log.LogError("Copy failed with status: " + targetBlob.CopyState.Status);
        }
    
    
        await sourceBlob.DeleteAsync();
    
    
        log.LogInformation( "Done.");
    }
    

标签: azureazure-functionsazure-blob-storageazure-function-async

解决方案


当我们使用targetBlob.StartCopyAsync(blob.Uri)将 blob 复制到目标 blob 时,我们应该将 blob 名称指定为targetBlob. 在您的代码中,您使用:

CloudBlockBlob targetBlob = sourceContainer.GetBlockBlobReference("SourceFolder/ArchieveFolder/");

所以targetBlob只有文件夹目录但没有 blob 名称。

请参考我下面的代码:

var ConnectionString = "xxxxxx";
CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudBlobClient sourceCloudBlobClient = sourceStorageAccount.CreateCloudBlobClient();
CloudBlobContainer sourceContainer = sourceCloudBlobClient.GetContainerReference("data");

CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference("SourceFolder/");
//CloudBlockBlob targetBlob = sourceContainer.GetBlockBlobReference("SourceFolder/ArchieveFolder/");   //please remove this line, get "targetBlob" in the foreach loop
var resultSegment = sourceContainer.GetDirectoryReference("SourceFolder/");
var rootDirFolders = resultSegment.ListBlobsSegmentedAsync(true, BlobListingDetails.Metadata, null, null, null, null).Result;

try
{
    foreach (var blob in rootDirFolders.Results)
    {
        string blobUri = blob.Uri.ToString();
        string blobName = blobUri.Substring(blobUri.IndexOf("SourceFolder/") + 13);
        CloudBlockBlob targetBlob = sourceContainer.GetBlockBlobReference("SourceFolder/ArchieveFolder/" + blobName);
        await targetBlob.StartCopyAsync(blob.Uri);
    }
}
catch (Exception ex)
{
    ........
}

运行代码后,我们可以发现三个 txt 文件被从 to 复制SourceFolder/SourceFolder/ArchieveFolder/(如下图两个截图所示)。

在此处输入图像描述 在此处输入图像描述


推荐阅读