C# Azure Storage - How to Upload a File to an alre

2019-07-28 07:43发布

问题:

Hoping for a bit of help on how to upload a file into an existing container in Azure Storage. I have followed Azure doc - but it seem to pointing to creating a new container and upload a file there. If I already create a container beforehand in Azure, for example, as "testresult1", and then try to upload a file there using C# code then it complains that the container "testresult1" already exists.

Here is the code I've used that is wrong:

CloudBlobContainer = cloudBlobClient.GetContainerReference("testresult1");

The sample code in Microsoft doc works without having any issue, but it creates a new container, "quickstartblobs" plus the GUID, then uploads the file there. I am trying to see if there is any option where if a container already exists then how we can upload one or more files into that container using the code, without creating a new container for each uploaded file.

A relevant question: is there any upper limit on how many containers can be created in Azure Storage? Is it related to Azure subscription?

Thank you.

回答1:

You could use CreateIfNotExistsAsync method, which initiates an asynchronous operation that creates the container if it does not already exist.

Use the following code to get your existed container and follow the Doc to go on uploading file.

cloudBlobContainer = cloudBlobClient.GetContainerReference("testresult1");
await cloudBlobContainer.CreateIfNotExistsAsync();

is there any upper limit on how many containers can be created in Azure Storage? Is it related to Azure subscription?

Max number of blob containers, blobs, file shares, tables, queues, entities, or messages per storage account is no limit.

And max size of single blob container is 500 TiB. Max number of stored access policies per blob container is 5.

For more details, you could refer to azure subscription service limits.



回答2:

Method to get the container.

    public CloudBlobContainer GetCloudBlobContainer(string strBlobContainerName)
    {
        CloudStorageAccount storageAccount = 
CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["BlobConnectionString"]));
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer blobContainer = blobClient.GetContainerReference(strBlobContainerName);

                    blobContainer.CreateIfNotExists();
                    blobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
                    return blobContainer;
     }

Below code use to upload the file on Azure.

CloudBlobContainer blobContainer = new BlobStorageServiceBLL().GetCloudBlobContainer(containerName);
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(uploadedFileName);
blob.UploadFromStream(file.InputStream);

Note: My GetCloudBlobContainer method into BlobStorageServiceBLL controller which is different class file but you can manage it in your same class file.