![](/img/trans.png)
[英]C# Azure Storage - How to Upload a File to an already Existing Container
[英]How can I upload a blob to an existing container in C# Function
我正在學習如何將存儲 blob 與 Azure 函數一起使用,我遵循了這個文檔: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-do.net
// Create a BlobServiceClient object which will be used to create a container client
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
// Create the container and return a container client object
BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync("<containername>");
// Get a reference to a blob
BlobClient blobClient = containerClient.GetBlobClient("<blobname>");
// Open the file and upload its data
using FileStream uploadFileStream = File.OpenRead("<localfilepath>");
await blobClient.UploadAsync(uploadFileStream, true);
uploadFileStream.Close();
教程中的這段代碼僅展示了如何在新容器中上傳 blob,但我想上傳到現有容器。 我能做什么? 謝謝。
使用GetBlobContainerClient
。 你得到的其他 awnsers 都是為v11
准備的。
var serviceClient = new BlobServiceClient(connectionString);
var containerClient = serviceClient.GetBlobContainerClient(containerName);
試試這個方法
public static async Task<bool> UploadImageAsyncPDF(string imagepath, string originalUploadedFileName)
{
string filefullpath = string.Empty;
bool Success = false;
try
{
if (!String.IsNullOrEmpty(SecretString) && !String.IsNullOrEmpty(ContainerName))
using (Stream filestream = System.IO.File.OpenRead(imagepath))
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(SecretString);
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(ContainerName);
cloudBlobContainer.CreateIfNotExists();
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(originalUploadedFileName);
cloudBlockBlob.Properties.ContentType = "application/pdf";
if (!cloudBlockBlob.Exists())
{
await cloudBlockBlob.UploadFromStreamAsync(filestream);
filefullpath = cloudBlockBlob.Uri.ToString();
Success = true;
}
else
{
filefullpath = "AlreadyExists";
Success = true;
}
}
}
catch (Exception ex)
{
Logger.WriteLog("Exception in UploadImageAsyncPDF:" + ex);
Success = false;
}
return Success;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.