Create a blob container with Java
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 Java.
Prerequisites
- This article assumes you already have a project set up to work with the Azure Blob Storage client library for Java. To learn about setting up your project, including package installation, adding
import
directives, and creating an authorized client object, see Get Started with Azure Storage and Java. - 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.windows.net/my-container-name
Create a container
To create a container, call one of the following methods from the BlobServiceClient
class:
You can also create a container using one of the following methods from the BlobContainerClient
class:
Containers are created immediately beneath the storage account. It's not possible to nest one container beneath another. For the create
and createBlobContainer
methods, an exception is thrown if a container with the same name already exists.
The following example creates a container from a BlobServiceClient
object:
public BlobContainerClient createContainer(BlobServiceClient blobServiceClient, String containerName) {
// Create the container using the service client object
BlobContainerClient blobContainerClient = blobServiceClient.createBlobContainer(containerName);
return blobContainerClient;
}
Create the root container
A root container serves as a default container for your storage account. Each storage account may have one root container, which must be named $root. The root container must be explicitly created or deleted.
You can reference a blob stored in the root container without including the root container name. The root container enables you to reference a blob at the top level of the storage account hierarchy. For example, you can reference a blob that is in the root container in the following manner:
https://accountname.blob.core.windows.net/default.html
The following example creates a new BlobContainerClient
object with the container name $root, then creates the container if it doesn't already exist in the storage account:
public void createRootContainer(BlobServiceClient blobServiceClient) {
// Creates a new BlobContainerClient object by appending the containerName to
// the end of the URI
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("$root");
// If the container does not already exist, create it using the container client
blobContainerClient.createIfNotExists();
}
Resources
To learn more about creating a container using the Azure Blob Storage client library for Java, see the following resources.
REST API operations
The Azure SDK for Java contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Java paradigms. The client library methods for creating a container use the following REST API operation:
- Create Container (REST API)
Code samples
Client library resources
प्रतिक्रिया
https://aka.ms/ContentUserFeedback.
जल्द आ रहा है: 2024 के दौरान हम सामग्री के लिए फीडबैक तंत्र के रूप में GitHub मुद्दों को चरणबद्ध तरीके से समाप्त कर देंगे और इसे एक नई फीडबैक प्रणाली से बदल देंगे. अधिक जानकारी के लिए, देखें:के लिए प्रतिक्रिया सबमिट करें और देखें