diff --git a/sdk/storage/azure-storage-blob/CHANGELOG.md b/sdk/storage/azure-storage-blob/CHANGELOG.md index b45814a70cd82..6aac69c65db42 100644 --- a/sdk/storage/azure-storage-blob/CHANGELOG.md +++ b/sdk/storage/azure-storage-blob/CHANGELOG.md @@ -3,10 +3,14 @@ ## 12.38.0-beta.2 (Unreleased) ### Features Added +- Added `getAccessTier`, `isAccessTierInferred`, `getAccessTierChangeTime` and `getSmartAccessTier`, with their + setters, to `BlobDownloadHeaders`, which Get Blob returns as Get Blob Properties does. ### Breaking Changes ### Bugs Fixed +- Fixed `BlobProperties` built from a download, such as `BlobInputStream.getProperties()` from `openInputStream`, + reporting no access tier, inferred tier, smart tier or tier change time although Get Blob had returned them. - Fixed `createIfNotExists` on `BlobContainerClient` and `BlobContainerAsyncClient`, and `createBlobContainerIfNotExists` on `BlobServiceClient` and `BlobServiceAsyncClient`, reporting every 409 as a container that already exists. They now do so only for `ContainerAlreadyExists`, and throw for any other 409, diff --git a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/implementation/models/BlobPropertiesInternalDownload.java b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/implementation/models/BlobPropertiesInternalDownload.java index 7b87651a3e253..60309b04bec58 100644 --- a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/implementation/models/BlobPropertiesInternalDownload.java +++ b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/implementation/models/BlobPropertiesInternalDownload.java @@ -158,17 +158,17 @@ public String getCopyDestinationSnapshot() { @Override public AccessTier getAccessTier() { - return null; + return headers.getAccessTier(); } @Override public Boolean isAccessTierInferred() { - return null; + return headers.isAccessTierInferred(); } @Override public AccessTier getSmartAccessTier() { - return null; + return headers.getSmartAccessTier(); } @Override @@ -188,7 +188,7 @@ public String getEncryptionScope() { @Override public OffsetDateTime getAccessTierChangeTime() { - return null; + return headers.getAccessTierChangeTime(); } @Override diff --git a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/models/BlobDownloadHeaders.java b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/models/BlobDownloadHeaders.java index b37fcdacb98a3..319a084a1fba9 100644 --- a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/models/BlobDownloadHeaders.java +++ b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/models/BlobDownloadHeaders.java @@ -1074,4 +1074,90 @@ public BlobDownloadHeaders setDownloadHint(DownloadHint downloadHint) { : com.azure.storage.blob.implementation.models.DownloadHint.fromString(downloadHint.toString())); return this; } + + /** + * Get the accessTier property: The tier of the blob. Get Blob returns it for a block blob in a Blob Storage or + * general purpose v2 account, and for a page blob in a Premium Storage account. + * + * @return the accessTier value. + */ + public AccessTier getAccessTier() { + return AccessTier.fromString(internalHeaders.getXMsAccessTier()); + } + + /** + * Set the accessTier property: The tier of the blob. Get Blob returns it for a block blob in a Blob Storage or + * general purpose v2 account, and for a page blob in a Premium Storage account. + * + * @param accessTier the accessTier value to set. + * @return the BlobDownloadHeaders object itself. + */ + public BlobDownloadHeaders setAccessTier(AccessTier accessTier) { + internalHeaders.setXMsAccessTier(accessTier == null ? null : accessTier.toString()); + return this; + } + + /** + * Get the accessTierInferred property: Whether the access tier of the blob was inferred, rather than set + * explicitly on the blob. + * + * @return the accessTierInferred value. + */ + public Boolean isAccessTierInferred() { + return internalHeaders.isXMsAccessTierInferred(); + } + + /** + * Set the accessTierInferred property: Whether the access tier of the blob was inferred, rather than set + * explicitly on the blob. + * + * @param accessTierInferred the accessTierInferred value to set. + * @return the BlobDownloadHeaders object itself. + */ + public BlobDownloadHeaders setAccessTierInferred(Boolean accessTierInferred) { + internalHeaders.setXMsAccessTierInferred(accessTierInferred); + return this; + } + + /** + * Get the accessTierChangeTime property: The time the tier of the blob was last changed. + * + * @return the accessTierChangeTime value. + */ + public OffsetDateTime getAccessTierChangeTime() { + return internalHeaders.getXMsAccessTierChangeTime(); + } + + /** + * Set the accessTierChangeTime property: The time the tier of the blob was last changed. + * + * @param accessTierChangeTime the accessTierChangeTime value to set. + * @return the BlobDownloadHeaders object itself. + */ + public BlobDownloadHeaders setAccessTierChangeTime(OffsetDateTime accessTierChangeTime) { + internalHeaders.setXMsAccessTierChangeTime(accessTierChangeTime); + return this; + } + + /** + * Get the smartAccessTier property: When the access tier of the blob is {@link AccessTier#SMART}, the concrete + * access tier the service has selected for it; otherwise {@code null}. + * + * @return the smartAccessTier value. + */ + public AccessTier getSmartAccessTier() { + return AccessTier.fromString(internalHeaders.getXMsSmartAccessTier()); + } + + /** + * Set the smartAccessTier property: When the access tier of the blob is {@link AccessTier#SMART}, the concrete + * access tier the service has selected for it; otherwise {@code null}. + * + * @param smartAccessTier the smartAccessTier value to set. + * @return the BlobDownloadHeaders object itself. + */ + public BlobDownloadHeaders setSmartAccessTier(AccessTier smartAccessTier) { + internalHeaders.setXMsSmartAccessTier(smartAccessTier == null ? null : smartAccessTier.toString()); + return this; + } } diff --git a/sdk/storage/azure-storage-blob/src/test/java/com/azure/storage/blob/BlobDownloadAccessTierTests.java b/sdk/storage/azure-storage-blob/src/test/java/com/azure/storage/blob/BlobDownloadAccessTierTests.java new file mode 100644 index 0000000000000..0db4798a64fa8 --- /dev/null +++ b/sdk/storage/azure-storage-blob/src/test/java/com/azure/storage/blob/BlobDownloadAccessTierTests.java @@ -0,0 +1,152 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.storage.blob; + +import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpHeaderName; +import com.azure.core.http.HttpHeaders; +import com.azure.core.test.http.MockHttpResponse; +import com.azure.core.util.DateTimeRfc1123; +import com.azure.storage.blob.models.AccessTier; +import com.azure.storage.blob.models.BlobDownloadHeaders; +import com.azure.storage.blob.models.BlobProperties; +import com.azure.storage.blob.specialized.BlobInputStream; +import org.junit.jupiter.api.Test; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Get Blob reports the blob's access tier, as Get Blob Properties does; the download's headers and the properties + * built from them carry it rather than leaving a caller to ask for the properties again. + */ +public class BlobDownloadAccessTierTests { + private static final byte[] CONTENT = "hello".getBytes(StandardCharsets.UTF_8); + private static final OffsetDateTime TIER_CHANGE_TIME = OffsetDateTime.of(2026, 1, 2, 3, 4, 5, 0, ZoneOffset.UTC); + + @Test + public void downloadHeadersCarryAccessTier() { + BlobDownloadHeaders headers = blobClient(tierHeaders("Cool")) + .downloadStreamWithResponse(new ByteArrayOutputStream(), null, null, null, false, null, null) + .getDeserializedHeaders(); + + assertEquals(AccessTier.COOL, headers.getAccessTier()); + assertTrue(headers.isAccessTierInferred()); + assertEquals(TIER_CHANGE_TIME, headers.getAccessTierChangeTime()); + assertNull(headers.getSmartAccessTier()); + } + + @Test + public void inputStreamPropertiesCarryAccessTier() throws IOException { + try (BlobInputStream stream = blobClient(tierHeaders("Cool")).openInputStream()) { + BlobProperties properties = stream.getProperties(); + assertEquals(AccessTier.COOL, properties.getAccessTier()); + assertTrue(properties.isAccessTierInferred()); + assertEquals(TIER_CHANGE_TIME, properties.getAccessTierChangeTime()); + } + } + + @Test + public void asyncDownloadHeadersCarryAccessTier() { + StepVerifier.create(blobAsyncClient(tierHeaders("Cool")).downloadStreamWithResponse(null, null, null, false)) + .assertNext(response -> { + BlobDownloadHeaders headers = response.getDeserializedHeaders(); + assertEquals(AccessTier.COOL, headers.getAccessTier()); + assertTrue(headers.isAccessTierInferred()); + assertEquals(TIER_CHANGE_TIME, headers.getAccessTierChangeTime()); + }) + .verifyComplete(); + } + + @Test + public void downloadHeadersCarrySmartAccessTier() { + HttpHeaders headers = tierHeaders("Smart").set(HttpHeaderName.fromString("x-ms-smart-access-tier"), "Hot"); + + BlobDownloadHeaders downloadHeaders = blobClient(headers) + .downloadStreamWithResponse(new ByteArrayOutputStream(), null, null, null, false, null, null) + .getDeserializedHeaders(); + + assertEquals(AccessTier.SMART, downloadHeaders.getAccessTier()); + assertEquals(AccessTier.HOT, downloadHeaders.getSmartAccessTier()); + } + + @Test + public void downloadWithoutAccessTierHasNone() { + BlobDownloadHeaders headers = blobClient(baseHeaders()) + .downloadStreamWithResponse(new ByteArrayOutputStream(), null, null, null, false, null, null) + .getDeserializedHeaders(); + + assertNull(headers.getAccessTier()); + assertNull(headers.isAccessTierInferred()); + assertNull(headers.getAccessTierChangeTime()); + } + + @Test + public void accessTierRoundTripsThroughSetters() { + BlobDownloadHeaders headers = new BlobDownloadHeaders().setAccessTier(AccessTier.ARCHIVE) + .setAccessTierInferred(false) + .setAccessTierChangeTime(TIER_CHANGE_TIME) + .setSmartAccessTier(AccessTier.COLD); + + assertEquals(AccessTier.ARCHIVE, headers.getAccessTier()); + assertEquals(Boolean.FALSE, headers.isAccessTierInferred()); + assertEquals(TIER_CHANGE_TIME, headers.getAccessTierChangeTime()); + assertEquals(AccessTier.COLD, headers.getSmartAccessTier()); + + headers.setAccessTier(null).setSmartAccessTier(null); + assertNull(headers.getAccessTier()); + assertNull(headers.getSmartAccessTier()); + } + + private static HttpHeaders baseHeaders() { + return new HttpHeaders().set(HttpHeaderName.CONTENT_TYPE, "application/octet-stream") + .set(HttpHeaderName.ETAG, "\"0x8DABC\"") + .set(HttpHeaderName.LAST_MODIFIED, new DateTimeRfc1123(TIER_CHANGE_TIME).toString()) + .set(HttpHeaderName.fromString("x-ms-blob-type"), "BlockBlob") + .set(HttpHeaderName.fromString("x-ms-version"), BlobServiceVersion.getLatest().getVersion()); + } + + private static HttpHeaders tierHeaders(String tier) { + return baseHeaders().set(HttpHeaderName.fromString("x-ms-access-tier"), tier) + .set(HttpHeaderName.fromString("x-ms-access-tier-inferred"), "true") + .set(HttpHeaderName.fromString("x-ms-access-tier-change-time"), + new DateTimeRfc1123(TIER_CHANGE_TIME).toString()); + } + + /** Answers every request as Get Blob does, with the range asked for. */ + private static HttpClient httpClient(HttpHeaders headers) { + return request -> { + HttpHeaders responseHeaders = new HttpHeaders(headers); + String range = request.getHeaders().getValue(HttpHeaderName.fromString("x-ms-range")); + if (range == null) { + responseHeaders.set(HttpHeaderName.CONTENT_LENGTH, String.valueOf(CONTENT.length)); + return Mono.just(new MockHttpResponse(request, 200, responseHeaders, CONTENT)); + } + responseHeaders.set(HttpHeaderName.CONTENT_LENGTH, String.valueOf(CONTENT.length)) + .set(HttpHeaderName.CONTENT_RANGE, "bytes 0-" + (CONTENT.length - 1) + "/" + CONTENT.length); + return Mono.just(new MockHttpResponse(request, 206, responseHeaders, CONTENT)); + }; + } + + private static BlobClient blobClient(HttpHeaders headers) { + return new BlobClientBuilder().endpoint("https://account.blob.core.windows.net/container/blob") + .httpClient(httpClient(headers)) + .buildClient(); + } + + private static BlobAsyncClient blobAsyncClient(HttpHeaders headers) { + return new BlobClientBuilder().endpoint("https://account.blob.core.windows.net/container/blob") + .httpClient(httpClient(headers)) + .buildAsyncClient(); + } +}