I'm using azure-storage-blob java SDK to upload file to Azure blob and generating SAS key to download file. Here below are my code.
String accountName = "accountName";
String accountKey = "accountKey";
String formatUrl = "https://%s.blob.core.windows.net";
String endpoint = String.format(Locale.ROOT, formatUrl, accountName);
StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
BlobClient blobClient = blobServiceClient.getBlobContainerClient(containerName).getBlobClient(blobName);
OffsetDateTime expiryTime = OffsetDateTime.now().plusDays(1);
BlobSasPermission permission = new BlobSasPermission().setReadPermission(true);
BlobServiceSasSignatureValues values = new BlobServiceSasSignatureValues(expiryTime, permission)
.setStartTime(OffsetDateTime.now());
String sasToken = blobClient.generateSas(values);
String fileUrl = blobClient.getBlobUrl();
String downloadLink = fileUrl + "?" + sasToken;
It's work fine to download a file. But if you generate link once per second, the first and second link works fine, maybe there is an error snice clicking third link. There will display an error page from Azure
AuthenticationFailed
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:******Time:2021-08-13T09:28:36.7981577Z
Signature not valid in the specified time frame: Start [Fri, 13 Aug 2021 09:28:37 GMT] - Expiry [Sat, 14 Aug 2021 09:28:37 GMT] - Current [Fri, 13 Aug 2021 09:28:36 GMT]
What's should I do?