Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Create a blob container with Java

Feedback

In this article

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 theAzure Storage client library for Java.

Prerequisites

Set up your environment

If you don't have an existing project, this section shows you how to set up a project to work with the Azure Blob Storage client library for Java. For more information, seeGet started with Azure Blob Storage and Java.

To work with the code examples in this article, follow these steps to set up your project.

Note

This article uses the Maven build tool to build and run the example code. Other build tools, such as Gradle, also work with the Azure SDK for Java.

Install packages

Open thepom.xml file in your text editor. Install the packages byincluding the BOM file, orincluding a direct dependency.

Add import statements

Add the followingimport statements:

import com.azure.storage.blob.*;

Authorization

The authorization mechanism must have the necessary permissions to create a container. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in roleStorage Blob Data Contributor or higher. To learn more, see the authorization guidance forCreate Container (REST API).

Create a client object

To connect an app to Blob Storage, create an instance ofBlobServiceClient.

The following example usesBlobServiceClientBuilder to build aBlobServiceClient object usingDefaultAzureCredential, and shows how to create container and blob clients, if needed:

// Azure SDK client builders accept the credential as a parameter// TODO: Replace <storage-account-name> with your actual storage account nameBlobServiceClient blobServiceClient = new BlobServiceClientBuilder()        .endpoint("https://<storage-account-name>.blob.core.windows.net/")        .credential(new DefaultAzureCredentialBuilder().build())        .buildClient();// If needed, you can create a BlobContainerClient object from the BlobServiceClientBlobContainerClient containerClient = blobServiceClient        .getBlobContainerClient("<container-name>");// If needed, you can create a BlobClient object from the BlobContainerClientBlobClient blobClient = containerClient        .getBlobClient("<blob-name>");

To learn more about creating and managing client objects, seeCreate and manage client objects that interact with data resources.

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 theBlobServiceClient class:

You can also create a container using one of the following methods from theBlobContainerClient class:

Containers are created immediately beneath the storage account. It's not possible to nest one container beneath another. For thecreate andcreateBlobContainer methods, an exception is thrown if a container with the same name already exists.

The following example creates a container from aBlobServiceClient 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 newBlobContainerClient 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.

Code samples

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:

Client library resources

Related content

  • This article is part of the Blob Storage developer guide for Java. To learn more, see the full list of developer guide articles atBuild your Java app.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?