下面的代码能够将指定目录中的文件上传localPath到 Azure Blob 存储容器。问题是文件没有放置在容器的根目录中,而是创建了与变量定义的文件路径匹配的文件夹localPath。string localPath = @"C:\example\path\to\files";生成的 blob 容器如下所示。.+-- example| +-- path| +-- to| +-- files| +-- file1.json| +-- file2.json| +-- file3.json| +-- file4.json| +-- file5.json需要对下面的代码进行哪些更改,以便将文件移动到容器的根目录,而不是移动到与 匹配的文件夹结构中localPath?程序.csnamespace AzureBlobStorageFileTransfer{ using Microsoft.Azure.Storage; using Microsoft.Azure.Storage.Blob; using System; using System.IO; using System.Threading.Tasks; public static class Program { public static void Main() { ProcessAsync().GetAwaiter().GetResult(); } private static async Task ProcessAsync() { CloudStorageAccount storageAccount = null; CloudBlobContainer cloudBlobContainer = null; string storageConnectionString = Environment.GetEnvironmentVariable("storageconnectionstring"); if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount)) { try { CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); cloudBlobContainer = cloudBlobClient.GetContainerReference("example"); BlobContainerPermissions permissions = new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }; await cloudBlobContainer.SetPermissionsAsync(permissions); string localPath = @"C:\example\path\to\files"; var txtFiles = Directory.EnumerateFiles(localPath, "*.json");
1 回答
鸿蒙传说
TA贡献1865条经验 获得超7个赞
您可以Path.GetFileName(currentFile)通过以下方式cloudBlobContainer.GetBlockBlobReference来完成此操作:
foreach (string currentFile in txtFiles)
{
string currentFileName = Path.GetFileName(currentFile);
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(currentFileName);
await cloudBlockBlob.UploadFromFileAsync(currentFile);
}
- 1 回答
- 0 关注
- 75 浏览
添加回答
举报
0/150
提交
取消