亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

PHP
慕尼黑8549860 2024-01-20 21:02:22
下面的代碼能夠將指定目錄中的文件上傳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);

}


查看完整回答
反對 回復 2024-01-20
  • 1 回答
  • 0 關注
  • 137 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號