Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I upload a blob to an existing container in C# Function

Tags:

function

c#

blob

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.

like image 217
Doris Lv Avatar asked Mar 02 '23 05:03

Doris Lv


2 Answers

Use GetBlobContainerClient. The other awnsers you got are all for for v11.

var serviceClient = new BlobServiceClient(connectionString);
var containerClient = serviceClient.GetBlobContainerClient(containerName);
like image 94
404 Avatar answered Apr 07 '23 11:04

404


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;
        }
like image 37
AcAnanth Avatar answered Apr 07 '23 09:04

AcAnanth