|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +package com.microsoft.durabletask.azureblobpayloads; |
| 4 | + |
| 5 | +import com.azure.core.credential.TokenCredential; |
| 6 | +import com.azure.core.http.rest.Response; |
| 7 | +import com.azure.core.util.Context; |
| 8 | +import com.azure.storage.blob.BlobClient; |
| 9 | +import com.azure.storage.blob.BlobContainerClient; |
| 10 | +import com.azure.storage.blob.BlobServiceClient; |
| 11 | +import com.azure.storage.blob.BlobServiceClientBuilder; |
| 12 | +import com.azure.storage.blob.models.BlobHttpHeaders; |
| 13 | +import com.azure.storage.blob.models.BlobStorageException; |
| 14 | +import com.azure.storage.blob.models.PublicAccessType; |
| 15 | + |
| 16 | +import java.io.ByteArrayInputStream; |
| 17 | +import java.io.ByteArrayOutputStream; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.io.OutputStream; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.UUID; |
| 23 | +import java.util.zip.GZIPInputStream; |
| 24 | +import java.util.zip.GZIPOutputStream; |
| 25 | + |
| 26 | +/** |
| 27 | + * Azure Blob Storage implementation of {@link PayloadStore}. |
| 28 | + * <p> |
| 29 | + * Stores payloads as blobs and returns opaque tokens in the form {@code blob:v1:<container>:<blobName>}. |
| 30 | + * Supports optional gzip compression. The blob container is created automatically on first upload. |
| 31 | + */ |
| 32 | +public final class BlobPayloadStore extends PayloadStore { |
| 33 | + |
| 34 | + static final String TOKEN_PREFIX = "blob:v1:"; |
| 35 | + private static final String CONTENT_ENCODING_GZIP = "gzip"; |
| 36 | + |
| 37 | + private final BlobContainerClient containerClient; |
| 38 | + private final LargePayloadStorageOptions options; |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a new {@code BlobPayloadStore} from the given options. |
| 42 | + * |
| 43 | + * @param options the storage options |
| 44 | + * @throws IllegalArgumentException if neither connection string nor account URI/credential are provided |
| 45 | + */ |
| 46 | + public BlobPayloadStore(LargePayloadStorageOptions options) { |
| 47 | + if (options == null) { |
| 48 | + throw new IllegalArgumentException("options must not be null."); |
| 49 | + } |
| 50 | + |
| 51 | + String containerName = options.getContainerName(); |
| 52 | + if (containerName == null || containerName.isEmpty()) { |
| 53 | + throw new IllegalArgumentException("Container name must not be null or empty."); |
| 54 | + } |
| 55 | + |
| 56 | + boolean hasConnectionString = options.getConnectionString() != null |
| 57 | + && !options.getConnectionString().isEmpty(); |
| 58 | + boolean hasIdentityAuth = options.getAccountUri() != null && options.getCredential() != null; |
| 59 | + |
| 60 | + if (!hasConnectionString && !hasIdentityAuth) { |
| 61 | + throw new IllegalArgumentException( |
| 62 | + "Either ConnectionString or AccountUri and Credential must be provided."); |
| 63 | + } |
| 64 | + |
| 65 | + BlobServiceClient serviceClient; |
| 66 | + if (hasIdentityAuth) { |
| 67 | + serviceClient = new BlobServiceClientBuilder() |
| 68 | + .endpoint(options.getAccountUri().toString()) |
| 69 | + .credential(options.getCredential()) |
| 70 | + .buildClient(); |
| 71 | + } else { |
| 72 | + serviceClient = new BlobServiceClientBuilder() |
| 73 | + .connectionString(options.getConnectionString()) |
| 74 | + .buildClient(); |
| 75 | + } |
| 76 | + |
| 77 | + this.containerClient = serviceClient.getBlobContainerClient(containerName); |
| 78 | + this.options = options; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Package-private constructor for testing with an injected {@link BlobContainerClient}. |
| 83 | + */ |
| 84 | + BlobPayloadStore(BlobContainerClient containerClient, LargePayloadStorageOptions options) { |
| 85 | + this.containerClient = containerClient; |
| 86 | + this.options = options; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String upload(String payload) { |
| 91 | + String blobName = UUID.randomUUID().toString().replace("-", ""); |
| 92 | + BlobClient blob = this.containerClient.getBlobClient(blobName); |
| 93 | + |
| 94 | + byte[] payloadBytes = payload.getBytes(StandardCharsets.UTF_8); |
| 95 | + |
| 96 | + // Ensure container exists (idempotent) |
| 97 | + try { |
| 98 | + this.containerClient.createIfNotExists(); |
| 99 | + } catch (BlobStorageException e) { |
| 100 | + // 409 Conflict means it already exists — safe to ignore |
| 101 | + if (e.getStatusCode() != 409) { |
| 102 | + throw new PayloadStorageException( |
| 103 | + "Failed to create blob container '" + this.containerClient.getBlobContainerName() + "'.", e); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + try { |
| 108 | + if (this.options.isCompressionEnabled()) { |
| 109 | + ByteArrayOutputStream compressedBuffer = new ByteArrayOutputStream(); |
| 110 | + try (GZIPOutputStream gzip = new GZIPOutputStream(compressedBuffer)) { |
| 111 | + gzip.write(payloadBytes); |
| 112 | + } |
| 113 | + byte[] compressedBytes = compressedBuffer.toByteArray(); |
| 114 | + BlobHttpHeaders headers = new BlobHttpHeaders().setContentEncoding(CONTENT_ENCODING_GZIP); |
| 115 | + try (InputStream stream = new ByteArrayInputStream(compressedBytes)) { |
| 116 | + blob.uploadWithResponse( |
| 117 | + stream, |
| 118 | + compressedBytes.length, |
| 119 | + null, // parallelTransferOptions |
| 120 | + headers, |
| 121 | + null, // metadata |
| 122 | + null, // tier |
| 123 | + null, // requestConditions |
| 124 | + null, // timeout |
| 125 | + Context.NONE); |
| 126 | + } |
| 127 | + } else { |
| 128 | + try (InputStream stream = new ByteArrayInputStream(payloadBytes)) { |
| 129 | + blob.upload(stream, payloadBytes.length, true); |
| 130 | + } |
| 131 | + } |
| 132 | + } catch (IOException e) { |
| 133 | + throw new PayloadStorageException("Failed to upload payload blob '" + blobName + "'.", e); |
| 134 | + } |
| 135 | + |
| 136 | + return encodeToken(this.containerClient.getBlobContainerName(), blobName); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public String download(String token) { |
| 141 | + String[] decoded = decodeToken(token); |
| 142 | + String container = decoded[0]; |
| 143 | + String name = decoded[1]; |
| 144 | + |
| 145 | + if (!container.equals(this.containerClient.getBlobContainerName())) { |
| 146 | + throw new IllegalArgumentException("Token container does not match configured container."); |
| 147 | + } |
| 148 | + |
| 149 | + BlobClient blob = this.containerClient.getBlobClient(name); |
| 150 | + |
| 151 | + try { |
| 152 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 153 | + blob.downloadStream(outputStream); |
| 154 | + byte[] rawBytes = outputStream.toByteArray(); |
| 155 | + |
| 156 | + // Check if the content is gzip-compressed by trying to read the gzip header |
| 157 | + String contentEncoding = blob.getProperties().getContentEncoding(); |
| 158 | + boolean isGzip = CONTENT_ENCODING_GZIP.equalsIgnoreCase(contentEncoding); |
| 159 | + |
| 160 | + if (isGzip) { |
| 161 | + try (GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(rawBytes)); |
| 162 | + ByteArrayOutputStream decompressedBuffer = new ByteArrayOutputStream()) { |
| 163 | + byte[] buffer = new byte[8192]; |
| 164 | + int len; |
| 165 | + while ((len = gzip.read(buffer)) != -1) { |
| 166 | + decompressedBuffer.write(buffer, 0, len); |
| 167 | + } |
| 168 | + return decompressedBuffer.toString(StandardCharsets.UTF_8.name()); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return new String(rawBytes, StandardCharsets.UTF_8); |
| 173 | + } catch (BlobStorageException e) { |
| 174 | + if (e.getStatusCode() == 404) { |
| 175 | + throw new PayloadStorageException( |
| 176 | + "The blob '" + name + "' was not found in container '" + container + "'. " + |
| 177 | + "The payload may have been deleted or the container was never created.", e); |
| 178 | + } |
| 179 | + throw new PayloadStorageException("Failed to download payload blob '" + name + "'.", e); |
| 180 | + } catch (IOException e) { |
| 181 | + throw new PayloadStorageException("Failed to decompress payload blob '" + name + "'.", e); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public boolean isKnownPayloadToken(String value) { |
| 187 | + if (value == null || value.isEmpty()) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + return value.startsWith(TOKEN_PREFIX); |
| 191 | + } |
| 192 | + |
| 193 | + static String encodeToken(String container, String name) { |
| 194 | + return TOKEN_PREFIX + container + ":" + name; |
| 195 | + } |
| 196 | + |
| 197 | + static String[] decodeToken(String token) { |
| 198 | + if (!token.startsWith(TOKEN_PREFIX)) { |
| 199 | + throw new IllegalArgumentException("Invalid external payload token."); |
| 200 | + } |
| 201 | + String rest = token.substring(TOKEN_PREFIX.length()); |
| 202 | + int sep = rest.indexOf(':'); |
| 203 | + if (sep <= 0 || sep >= rest.length() - 1) { |
| 204 | + throw new IllegalArgumentException("Invalid external payload token format."); |
| 205 | + } |
| 206 | + return new String[] { rest.substring(0, sep), rest.substring(sep + 1) }; |
| 207 | + } |
| 208 | +} |
0 commit comments