简体   繁体   中英

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

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-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();

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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM