Search code examples
javaazurearchiveoverwrite

Azure Archive tier overwrite files [Java]


I was testing to overwrite any file stored with archive tier on Azure and i'm getting next error (there is no problem for other tiers):

ERROR: File upload failed com.azure.storage.blob.models.BlobStorageException: BlobArchived

The methods which i'm using to copy the file are:

blob.uploadWithResponse(..); or blob.uploadFromFile(..);

Is there currently a simple way to avoid this issue and overwriting the files or its not possible for archive tier files? I tested manually (from Azure Storage) and it asks you about overwrite the data or not. Maybe there is some method in Java for doing this, but i'm not able to find it.

Thanks in advance!

Im trying to find a way to overwrite data for archive tier files in Java


Solution

  • When a blob is in the Archive tier, it is set to be offline and cannot be read or modified. Hence, you cannot overwrite a blob which is in the Archive tier.

    To overwrite a blob, you have to make sure the blob is online and isn't set to Archive.

    This can be achieved using Rehydration in two ways:

    Method 1:

    • Copy the archived blob to a new destination blob that is in either the Hot or Cool tier using the Copy Blob operation .
    • When you copy an archived blob to a new blob in an online tier, the source blob remains unmodified in the Archive tier.
    • Once you have copied the blob to an online tier, you can then overwrite it as needed.

    Method 2:

    • Changing the Blob's tier from archive to hot or cool with the Set Blob Tier operation.

    I have a Blob in Archive tier:

    enter image description here

    • To Rehydrate the Archive tier Blob, I've used the below code snippet taken from SO by @Venkatesan:
    String sourceConnectionString = "<Source_storage_Connection_string>";
            String destConnectionString = "<destination_storage_connection_string>";
            String sourceContainerName = "<source_container_name>";//demo
            String destContainerName = "<destination_container_name>";//demo
            String sourceBlobName = "<your_source_blob_name>";//demoblob/Blob.txt
            String destBlobName = "<destination>";//demo.txt
            
            BlobClient sourceBlobClient = new BlobServiceClientBuilder().connectionString(sourceConnectionString)
                    .buildClient().getBlobContainerClient(sourceContainerName).getBlobClient(sourceBlobName);
            BlobClient destBlobClient = new BlobServiceClientBuilder().connectionString(destConnectionString)
                    .buildClient().getBlobContainerClient(destContainerName).getBlobClient(destBlobName);
            
            // Generating SAS token
            OffsetDateTime sasExpiry = OffsetDateTime.now().plusMinutes(1);
            BlobSasPermission permission = new BlobSasPermission().setReadPermission(true);
            BlobServiceSasSignatureValues sas = new BlobServiceSasSignatureValues(sasExpiry, permission);
            String sourceURL = sourceBlobClient.getBlobUrl() + "?" + sourceBlobClient.generateSas(sas);
            
    
            Map<String, String> metadata = new HashMap<>();
            metadata.put("key", "value");
            
            // Performing blob copy with rehydration
            destBlobClient.beginCopy(sourceURL, metadata, AccessTier.COOL, RehydratePriority.HIGH, null, null, null);
            
            System.out.println("Blob copy operation is initiated with rehydration.");
    

    Output:

    enter image description here

    enter image description here

    • A copy of the Blob created with Cool tier:

    enter image description here

    • You can overwrite the newly generated Blob with cool tier.

    References:

    Blob rehydration from the archive tier | Microsoft Learn Rehydrate an archived blob to an online tier - Azure Storage | Microsoft Learn