แก้ไข

แชร์ผ่าน


Manage container properties and metadata with Java

Blob containers support system properties and user-defined metadata, in addition to the data they contain. This article shows how to manage system properties and user-defined metadata with the Azure 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, see Get 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 the pom.xml file in your text editor. Install the packages by including the BOM file, or including a direct dependency.

Add import statements

Add the following import statements:

import com.azure.storage.blob.*;
import com.azure.storage.blob.models.*;
import java.util.*;

Authorization

The authorization mechanism must have the necessary permissions to work with container properties or metadata. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Reader or higher for the get operations, and Storage Blob Data Contributor or higher for the set operations. To learn more, see the authorization guidance for Get Container Properties (REST API), Set Container Metadata (REST API), or Get Container Metadata (REST API).

Create a client object

To connect an app to Blob Storage, create an instance of BlobServiceClient.

The following example uses BlobServiceClientBuilder to build a BlobServiceClient object using DefaultAzureCredential, 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 name
BlobServiceClient 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 BlobServiceClient
BlobContainerClient containerClient = blobServiceClient
        .getBlobContainerClient("<container-name>");

// If needed, you can create a BlobClient object from the BlobContainerClient
BlobClient blobClient = containerClient
        .getBlobClient("<blob-name>");

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

About properties and metadata

  • System properties: System properties exist on each Blob Storage resource. Some of them can be read or set, while others are read-only. Behind the scenes, some system properties correspond to certain standard HTTP headers. The Azure Storage client library for Java maintains these properties for you.

  • User-defined metadata: User-defined metadata consists of one or more name-value pairs that you specify for a Blob storage resource. You can use metadata to store additional values with the resource. Metadata values are for your own purposes only, and don't affect how the resource behaves.

    Metadata name/value pairs are valid HTTP headers and should adhere to all restrictions governing HTTP headers. For more information about metadata naming requirements, see Metadata names.

Retrieve container properties

To retrieve container properties, use the following method:

The following code example fetches a container's system properties and writes the property values to a console window:

public void getContainerProperties(BlobContainerClient blobContainerClient) {
    BlobContainerProperties properties = blobContainerClient.getProperties();
    System.out.printf("Public Access Type: %s, Legal Hold? %b, Immutable? %b%n",
            properties.getBlobPublicAccess(),
            properties.hasLegalHold(),
            properties.hasImmutabilityPolicy());
}

Set and retrieve metadata

You can specify metadata as one or more name-value pairs on a blob or container resource. To set metadata, use the following method:

Setting container metadata overwrites all existing metadata associated with the container. It's not possible to modify an individual name-value pair.

The following code example sets metadata on a container:

public void addContainerMetadata(BlobContainerClient blobContainerClient) {
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("docType", "text");
    metadata.put("category", "reference");

    try {
        blobContainerClient.setMetadata(metadata);
        System.out.printf("Set metadata completed %n");
    } catch (UnsupportedOperationException error) {
        System.out.printf("Failure while setting metadata %n");
    }
}

To retrieve metadata, call the following method:

The following example reads in metadata values:

public void readContainerMetadata(BlobContainerClient blobContainerClient) {
    BlobContainerProperties properties = blobContainerClient.getProperties();

    System.out.printf("Container metadata: %n");
    properties.getMetadata().entrySet().forEach(metadataItem -> {
        System.out.printf(" %s = %s%n", metadataItem.getKey(), metadataItem.getValue());
    });
}

Resources

To learn more about setting and retrieving container properties and metadata 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 setting and retrieving properties and metadata use the following REST API operations:

The getProperties method retrieves container properties and metadata by calling both the Get Blob Properties operation and the Get Blob Metadata operation.

Client library resources

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