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

2

5

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.

Deaminate answered 29/7, 2020 at 8:13 Comment(2)
Use blobServiceClient.GetContainerReference.Seabrooke
I tried, but it said blobServiceClient doesn't contain a definition for GetContainerReference. Am I missing a using directive? What directive I need to add?Deaminate
P
7

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

var serviceClient = new BlobServiceClient(connectionString);
var containerClient = serviceClient.GetBlobContainerClient(containerName);
Picklock answered 29/7, 2020 at 8:46 Comment(1)
Thank you very much, that is exactly I want.Deaminate
H
2

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;
        }
Haggard answered 29/7, 2020 at 8:18 Comment(2)
Thank you. Can I do that without CloudBlobContainer ? If it possible just use BlobContainerClient?Deaminate
@DorisLv of course! use BlobServiceClient and GetBlobContainerClientHaggard

© 2022 - 2024 — McMap. All rights reserved.