Manage blob properties and metadata with Java

In addition to the data they contain, blobs support system properties and user-defined metadata. 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 Set Blob Properties (REST API), Get Blob Properties (REST API), Set Blob Metadata (REST API), or Get Blob 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. Under the covers, 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.

Note

Blob index tags also provide the ability to store arbitrary user-defined key/value attributes alongside an Azure Blob storage resource. While similar to metadata, only blob index tags are automatically indexed and made searchable by the native blob service. Metadata cannot be indexed and queried unless you utilize a separate service such as Azure Search.

To learn more about this feature, see Manage and find data on Azure Blob storage with blob index.

Set and retrieve properties

To set properties on a blob, use the following method:

The following code example sets the ContentType and ContentLanguage system properties on a blob.

Any properties not explicitly set are cleared. The following code example first gets the existing properties on the blob, then uses them to populate the headers that aren't being updated.

public void setBlobProperties(BlobClient blobClient) {
    BlobProperties properties = blobClient.getProperties();

    // Set the ContentLanguage and ContentType headers, and populate the remaining
    // headers from the existing properties
    BlobHttpHeaders blobHeaders = new BlobHttpHeaders()
            .setContentLanguage("en-us")
            .setContentType("text/plain")
            .setCacheControl(properties.getCacheControl())
            .setContentDisposition(properties.getContentDisposition())
            .setContentEncoding(properties.getContentEncoding())
            .setContentMd5(properties.getContentMd5());

    blobClient.setHttpHeaders(blobHeaders);
    System.out.println("Set HTTP headers completed");
}

To retrieve properties on a blob, use the following method:

The following code example gets a blob's system properties and displays some of the values:

public void getBlobProperties(BlobClient blobClient) {
    BlobProperties properties = blobClient.getProperties();

    System.out.printf("BlobType: %s%n", properties.getBlobType());
    System.out.printf("BlobSize: %d%n", properties.getBlobSize());
    System.out.printf("ContentLanguage: %s%n", properties.getContentLanguage());
    System.out.printf("ContentType: %s%n", properties.getContentType());
}

Set and retrieve metadata

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

The following code example sets metadata on a blob:

public void addBlobMetadata(BlobClient blobClient) {
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("docType", "text");
    metadata.put("category", "reference");

    try {
        blobClient.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 getProperties method on your blob to populate the metadata collection, then read the values, as shown in the example below. The getProperties method retrieves blob properties and metadata by calling both the Get Blob Properties operation and the Get Blob Metadata operation.

The following code example reads metadata on a blob and prints each key/value pair:

public void readBlobMetadata(BlobClient blobClient) {
    // Get blob properties and metadata
    BlobProperties properties = blobClient.getProperties();

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

Resources

To learn more about how to manage system properties and user-defined 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 managing system properties and user-defined metadata use the following REST API operations:

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.