CloudBlobContainer.CreateIfNotExistsAsync Method

 

Updated: February 24, 2017

Initiates an asynchronous operation that creates the container if it does not already exist.

Namespace:   Microsoft.WindowsAzure.Storage.Blob
Assembly:  Microsoft.WindowsAzure.Storage (in Microsoft.WindowsAzure.Storage.dll)

NameDescription
System_CAPS_pubmethodCreateIfNotExistsAsync()

Initiates an asynchronous operation that creates the container if it does not already exist.

System_CAPS_pubmethodCreateIfNotExistsAsync(BlobContainerPublicAccessType, BlobRequestOptions, OperationContext)

Initiates an asynchronous operation that creates the container if it does not already exist.

System_CAPS_pubmethodCreateIfNotExistsAsync(BlobContainerPublicAccessType, BlobRequestOptions, OperationContext, CancellationToken)

Initiates an asynchronous operation that creates the container if it does not already exist.

System_CAPS_pubmethodCreateIfNotExistsAsync(BlobRequestOptions, OperationContext)

Initiates an asynchronous operation that creates the container if it does not already exist.

System_CAPS_pubmethodCreateIfNotExistsAsync(BlobRequestOptions, OperationContext, CancellationToken)

Initiates an asynchronous operation that creates the container if it does not already exist.

System_CAPS_pubmethodCreateIfNotExistsAsync(CancellationToken)

Initiates an asynchronous operation that creates the container if it does not already exist.

This method checks whether the container exists before creating it, and therefore requires read permissions as well as write permissions.

A call to the CreateIfNotExistsAsync method makes a read request to the service to check whether the container exists. If it does not exist, a second write call is made to the service to create the container. Compare this to the CreateAsync method, which makes a single write call to the service, and fails if the container does already exist.

Here's an example that creates a new container if it does not already exist. The example uses a GUID to name the container to ensure uniqueness. Note that the call to the GetContainerReference method does not make a request to the service; it creates a named instance of the container in local memory only.

private static async Task<CloudBlobContainer> CreateSampleContainerAsync(CloudBlobClient blobClient)
{
    // Get a reference to the container.
    CloudBlobContainer container = blobClient.GetContainerReference("sample-container");

    try
    {
        // Create the container if it does not already exist.
        await container.CreateIfNotExistsAsync();
    }
    catch (StorageException e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
        throw;
    }

    return container;
}

Return to top
Show: