I am learning how to use Storage blob with Azure Functions, and I followed this document: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-dotnet
// 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();
This code in tutorial just show how to upload a blob in a new container, but I want to upload to an existing container. What can I do? Thanks.
Use GetBlobContainerClient
. The other awnsers you got are all for for v11
.
var serviceClient = new BlobServiceClient(connectionString);
var containerClient = serviceClient.GetBlobContainerClient(containerName);
Try this way
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With