Skip to content

Latest commit

 

History

History
89 lines (52 loc) · 5.13 KB

File metadata and controls

89 lines (52 loc) · 5.13 KB
title Create a blob container with Java
titleSuffix Azure Storage
description Learn how to create a blob container in your Azure Storage account using the Java client library.
services storage
author stevenmatthew
ms.service azure-blob-storage
ms.topic how-to
ms.date 08/05/2024
ms.author shaas
ms.devlang java
ms.custom devx-track-java, devguide-java, devx-track-extended-java

Create a blob container with Java

[!INCLUDE storage-dev-guide-selector-create-container]

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.

[!INCLUDE storage-dev-guide-prereqs-java]

Set up your environment

[!INCLUDE storage-dev-guide-project-setup-java]

Add import statements

Add the following import statements:

:::code language="java" source="~/azure-storage-snippets/blobs/howto/Java/blob-devguide/blob-devguide-containers/src/main/java/com/blobs/devguide/containers/ContainerCreate.java" id="Snippet_Imports":::

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 role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for Create Container (REST API).

[!INCLUDE storage-dev-guide-create-client-java]

[!INCLUDE storage-dev-guide-about-container-naming]

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:

:::code language="java" source="~/azure-storage-snippets/blobs/howto/Java/blob-devguide/blob-devguide-containers/src/main/java/com/blobs/devguide/containers/ContainerCreate.java" id="Snippet_CreateContainer":::

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:

:::code language="java" source="~/azure-storage-snippets/blobs/howto/Java/blob-devguide/blob-devguide-containers/src/main/java/com/blobs/devguide/containers/ContainerCreate.java" id="Snippet_CreateRootContainer":::

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:

[!INCLUDE storage-dev-guide-resources-java]

[!INCLUDE storage-dev-guide-next-steps-java]