簡體   English   中英

如何在沒有文件路徑的情況下將文件上傳到 Azure Blob Storage 容器的根目錄

[英]How to upload files to the root of a Azure Blob Storage container without the file path

下面的代碼能夠將指定目錄localPath中的文件上傳到 Azure Blob Storage 容器。 問題是文件沒有放在容器的根目錄中,而是創建了與localPath變量定義的文件路徑匹配的文件夾。

string localPath = @"C:\example\path\to\files";

生成的 blob 容器如下所示。

.
+-- example
|   +-- path
|       +-- to
|           +-- files
|               +-- file1.json
|               +-- file2.json
|               +-- file3.json
|               +-- file4.json
|               +-- file5.json

需要對下面的代碼進行哪些更改,以便將文件移動到容器的根目錄,而不是在與localPath匹配的文件夾結構中?

程序.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.");
            }
        }
    }
}

您可以將Path.GetFileName(currentFile)傳遞給cloudBlobContainer.GetBlockBlobReference來完成此操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM