azure - 如何使用函数应用将文件从源文件夹复制到目标文件夹并删除 Azure 存储帐户中的源文件夹文件
问题描述
我能够将源文件夹 blob 复制到目标文件夹 blob 并删除源 blob 如何将多个 blob 复制到源文件夹到目标文件夹并删除源 blob
当我将源 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."); }
解决方案
当我们使用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/
(如下图两个截图所示)。
推荐阅读
- c++ - 将可变参数包的每个元素包装在包装器中并存储在 std::tuple 中
- python - 对 Pandas MultiIndex DataFrame 进行子集化
- json - 返回 json 空值 [雪花]
- javascript - 尝试将带有顺风的 MERN 堆栈部署到 Heroku 时构建失败
- python - 以 Python 方式编写多个 for 循环(包括一个类似于 sum() 函数的循环)
- python - 有没有办法从推文中获取不用于过滤推文的单词?
- c - 将 sqlite_exec 响应的行组合成来自 C 中 SQLite 回调的单个字符串
- api - JSON 路径结果名称不匹配。如何解决这个问题?
- python - 在 kivy 中编写新的动态类所需的最少代码?
- c++ - 如何在 C++ 中正确播种具有最大熵的 mersenne_twister RNG