This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
This article shows how to set or change the access tier for a block blob using theAzure Storage client library for Java.
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.
Open thepom.xml file in your text editor. Install the packages byincluding the BOM file, orincluding a direct dependency.
Add the followingimport statements:
import com.azure.core.util.polling.LongRunningOperationStatus;import com.azure.core.util.polling.PollResponse;import com.azure.core.util.polling.SyncPoller;import com.azure.storage.blob.*;import com.azure.storage.blob.models.*;import com.azure.storage.blob.options.BlobBeginCopyOptions;The authorization mechanism must have the necessary permissions to set a blob's access tier. 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 forSet Blob Tier.
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.
To manage costs for storage needs, it can be helpful to organize your data based on how frequently it's accessed and how long it needs to be retained. Azure storage offers different access tiers so that you can store your blob data in the most cost-effective manner based on how it's being used.
Azure Storage access tiers include:
To learn more about access tiers, seeAccess tiers for blob data.
While a blob is in the Archive access tier, it's considered to be offline, and can't be read or modified. In order to read or modify data in an archived blob, you must first rehydrate the blob to an online tier. To learn more about rehydrating a blob from the Archive tier to an online tier, seeBlob rehydration from the Archive tier.
Setting the access tier is only allowed on block blobs. To learn more about restrictions on setting a block blob's access tier, seeSet Blob Tier (REST API).
Note
To set the access tier toCold using Java, you must use a minimumclient library version of 12.21.0.
You can set a blob's access tier on upload by using theBlobUploadFromFileOptions class. The following code example shows how to set the access tier when uploading a blob:
public void uploadBlobWithAccessTier(BlobContainerClient blobContainerClient, Path filePath) { String fileName = filePath.getFileName().toString(); BlobClient blobClient = blobContainerClient.getBlobClient(fileName); BlobUploadFromFileOptions options = new BlobUploadFromFileOptions(filePath.toString()) .setTier(AccessTier.COOL); try { Response<BlockBlobItem> blockBlob = blobClient.uploadFromFileWithResponse(options, null, null); } catch (UncheckedIOException ex) { System.err.printf("Failed to upload from file: %s%n", ex.getMessage()); }}To learn more about uploading a blob with Java, seeUpload a blob with Java.
You can change the access tier of an existing block blob by using one of the following methods:
The following code example shows how to change the access tier to Cool for an existing blob:
public void changeBlobAccessTier(BlobClient blobClient) { // Change the blob's access tier to cool blobClient.setAccessTier(AccessTier.COOL);}If you're rehydrating an archived blob, use thesetAccessTierWithResponse method. Set thetier parameter to a validAccessTier value ofHOT,COOL,COLD, orARCHIVE. You can optionally set thepriority parameter to a validRehydratePriority valueHIGH orSTANDARD.
The following code example shows how to rehydrate an archived blob by changing the access tier to Hot:
public void rehydrateBlobSetAccessTier(BlobClient blobClient) { // Rehydrate the blob to hot tier using a standard rehydrate priority blobClient.setAccessTierWithResponse( AccessTier.HOT, RehydratePriority.STANDARD, null, null, null);}ThesetAccessTierWithResponse method can also accept aBlobSetAccessTierOptions parameter to specify configuration options.
You can change the access tier of an existing block blob by specifying an access tier as part of a copy operation. To change the access tier during a copy operation, use theBlobBeginCopyOptions class.
You can use thesetTier method to specify theAccessTier value asHOT,COOL,COLD, orARCHIVE. If you're rehydrating a blob from the archive tier using a copy operation, use thesetRehydratePriority method to specify theRehydratePriority value asHIGH orSTANDARD.
The following code example shows how to rehydrate an archived blob to the Hot tier using a copy operation:
public void rehydrateBlobUsingCopy( BlobClient sourceArchiveBlob, BlobClient destinationRehydratedBlob) { // Note: the destination blob must have a different name than the archived source blob // Start the copy operation and wait for it to complete final SyncPoller<BlobCopyInfo, Void> poller = destinationRehydratedBlob.beginCopy( new BlobBeginCopyOptions(sourceArchiveBlob.getBlobUrl()) .setTier(AccessTier.HOT) .setRehydratePriority(RehydratePriority.STANDARD)); PollResponse<BlobCopyInfo> response = poller .waitUntil(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED);}To learn more about copying a blob with Java, seeCopy a blob with Java.
To learn more about setting access tiers using the Azure Blob Storage client library for Java, see the following resources.
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 access tiers use the following REST API operation:
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?