![](/img/trans.png)
[英]Want to download and upload file from a folder inside the blob using new sdk - Azure.Storage.Blobs package
[英]Unable to upload a file to Azure Blob Storage using Azure.Storage.Blobs NuGet package
我正在嘗試使用 C# 使用 .NET Core (V3.1) 控制台應用程序將示例文檔文件上傳到 azure 存儲 blob。 我在本地運行 Visual Studio 2019 中的控制台應用程序。
我有
步驟 1:創建一個 .NET Core 控制台應用程序和 Nuget 包安裝
Azure.Storage.Blobs
Azure.Storage.Common
步驟 2:創建一個名為“files”的 azure 存儲帳戶和容器
步驟3:在本地機器上創建一個word/pdf文檔。
Step4:初始化存儲連接字符串、容器名稱、文件路徑
步驟5:在控制台應用程序中上傳文件然后在本地調試以進行測試的代碼。
整個方法:
public static async Task upload_ToBlob(string storageAccount_connectionString, string fileToUpload, string containerName)
{
try
{
Console.WriteLine("Inside upload method");
// Get a reference to a container named "sample-container" and then create it
BlobContainerClient blobContainerClient = new BlobContainerClient(storageAccount_connectionString, containerName);
// Get a reference to a blob named "fileToUpload" in a container named "files"
string filename_withExtension = Path.GetFileName(fileToUpload);
BlobClient blob = blobContainerClient.GetBlobClient(filename_withExtension);
// Upload local file
await blob.UploadAsync(fileToUpload);
Console.WriteLine("Upload Completed!");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
class Program
{
static async Task Main()
{
string storageAccount_connectionString = "DefaultEndpointsProtocol=https;AccountName=sghackathondemo;AccountKey=kCzwrMVmG8r4E9d1q9LKA0W8OuCtPp3WWbeBWoMfyW/6idSN0wPPU+QBnBVndAv+v9tS63gcnpUY1R1CnsHB8A==;EndpointSuffix=core.windows.net";
string storageAccount_ContainerName = "files";
string filePath = @"D:\Learning\test.txt";
await upload_ToBlob(storageAccount_connectionString, filePath, storageAccount_ContainerName).ConfigureAwait(false);
}
}
我既沒有出現異常也無法上傳文件。 自動 Visual Studio (2019) 調試過程在await blob.UploadAsync(filePath);后停止;
終於我明白為什么文件沒有上傳到存儲 blob 以及為什么 Visual Studio 調試會自動停止。
UploadAsync接受 Stream 作為輸入。
通過下面的小代碼片段更改,它工作正常。
// Open this file and upload it to blob
using (FileStream fileStream = File.OpenRead(filePath))
{
await blob.UploadAsync(fileStream);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.