【问题标题】:How to upload files to the root of a Azure Blob Storage container without the file path如何在没有文件路径的情况下将文件上传到 Azure Blob 存储容器的根目录
【发布时间】:2020-01-28 18:39:07
【问题描述】:

下面的代码能够将指定目录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 匹配的文件夹结构中?

Program.cs

namespace 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");

                    foreach (string currentFile in txtFiles)
                    {
                        CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(currentFile);
                        await cloudBlockBlob.UploadFromFileAsync(currentFile);
                    }

                }
                catch (StorageException ex)
                {
                    Console.WriteLine("Error returned from the service: {0}", ex.Message);
                }
            }
            else
            {
                Console.WriteLine(
                    "A connection string has not been defined in the system environment variables. " +
                    "Add a environment variable named 'storageconnectionstring' with your storage " +
                    "connection string as a value.");
            }
        }
    }
}

【问题讨论】:

    标签: c# .net-core azure-blob-storage


    【解决方案1】:

    您可以将Path.GetFileName(currentFile) 传递给cloudBlobContainer.GetBlockBlobReference 来完成此操作:

    foreach (string currentFile in txtFiles)
    {
        string currentFileName = Path.GetFileName(currentFile);
        CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(currentFileName);
        await cloudBlockBlob.UploadFromFileAsync(currentFile);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-30
      • 2015-07-13
      • 2020-02-04
      • 1970-01-01
      • 2017-12-13
      • 2019-08-13
      • 2017-01-24
      • 2017-08-19
      相关资源
      最近更新 更多