Create a blob container with TypeScript
Blobs in Azure Storage are organized into containers. Before you can upload a blob, you must first create a container. This article shows how to create containers with the Azure Storage client library for JavaScript.
Prerequisites
- The examples in this article assume you already have a project set up to work with the Azure Blob Storage client library for JavaScript. To learn about setting up your project, including package installation, importing modules, and creating an authorized client object to work with data resources, see Get started with Azure Blob Storage and TypeScript.
- The authorization mechanism must have permissions to create a blob container. To learn more, see the authorization guidance for the following REST API operation:
About container naming
A container name must be a valid DNS name, as it forms part of the unique URI used to address the container or its blobs. Follow these rules when naming a container:
- Container names can be between 3 and 63 characters long.
- Container names must start with a letter or number, and can contain only lowercase letters, numbers, and the dash (-) character.
- Consecutive dash characters aren't permitted in container names.
The URI for a container resource is formatted as follows:
https://my-account-name.blob.core.chinacloudapi.cn/my-container-name
Create a container
To create a container, create a BlobServiceClient object or ContainerClient object, then use one of the following create methods:
Containers are created immediately beneath the storage account. It's not possible to nest one container beneath another. An exception is thrown if a container with the same name already exists.
The following example creates a container asynchronously from the BlobServiceClient:
async function createContainer(
blobServiceClient: BlobServiceClient,
containerName: string
): Promise<ContainerClient> {
// public access at container level
const options: ContainerCreateOptions = {
access: 'container'
};
// creating client also creates container
const {
containerClient,
containerCreateResponse
}: {
containerClient: ContainerClient;
containerCreateResponse: ContainerCreateResponse;
} = await blobServiceClient.createContainer(containerName, options);
if (containerCreateResponse.errorCode)
throw Error(containerCreateResponse.errorCode);
console.log(`container ${containerName} created`);
// do something with container
// ...
// containerClient.listBlobsFlat({ includeMetadata: true,
// includeSnapshots: false,
// includeTags: true,
// includeVersions: false,
// prefix: ''});
return containerClient;
}
Understand the root container
A root container, with the specific name $root
, enables you to reference a blob at the top level of the storage account hierarchy. For example, you can reference a blob without using a container name in the URI:
https://myaccount.blob.core.chinacloudapi.cn/default.html
The root container must be explicitly created or deleted. It isn't created by default as part of service creation. The same code displayed in the previous section can create the root. The container name is $root
.
Resources
To learn more about creating a container using the Azure Blob Storage client library for JavaScript, see the following resources.
REST API operations
The Azure SDK for JavaScript contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar JavaScript paradigms. The client library methods for creating a container use the following REST API operation:
- Create Container (REST API)