To create a SAS (Shared Access Signature) token for Azure Storage in Java to be used by a client, you can use the Azure Storage SDK for Java. Here are the steps to follow:
pom.xml
file:<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.11.0</version>
</dependency>
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobSASPermission;
import com.azure.storage.blob.models.BlobSASQueryParameters;
import com.azure.storage.blob.models.UserDelegationKey;
import com.azure.storage.common.StorageSharedKeyCredential;
import java.time.OffsetDateTime;
StorageSharedKeyCredential
object by providing your storage account name and account key:StorageSharedKeyCredential credential = new StorageSharedKeyCredential("<your-storage-account>", "<your-storage-account-key>");
BlobServiceClientBuilder
to create a BlobServiceClient
object:BlobServiceClientBuilder builder = new BlobServiceClientBuilder();
BlobServiceClient client = builder
.endpoint("https://<your-storage-account>.blob.core.windows.net")
.credential(credential)
.buildClient();
getUserDelegationKey
method of the BlobServiceClient
object:OffsetDateTime startTime = OffsetDateTime.now();
OffsetDateTime expiryTime = startTime.plusMinutes(10);
UserDelegationKey key = client.getUserDelegationKey(startTime, expiryTime);
BlobSASQueryParameters
object by providing the required parameters:BlobSASPermission permissions = new BlobSASPermission().setReadPermission(true).setListPermission(true);
OffsetDateTime startTime = OffsetDateTime.now();
OffsetDateTime expiryTime = startTime.plusMinutes(10);
BlobSASQueryParameters params = new BlobSASQueryParameters(
expiryTime,
permissions)
.setStartTime(startTime)
.setContentDisposition("attachment; filename=myfile.txt")
.setContentEncoding("gzip")
.setContentLanguage("en-US")
.setContentType("text/plain")
.setVersion("2018-11-09")
.setProtocol("https")
.setUserDelegationKey(key); // use if you generated a user delegation key
encode
method of the BlobSASQueryParameters
object:String sasToken = params.encode();
That's it! You can now use the sasToken
string to authenticate your client when accessing your Azure Storage account.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-07-02 11:00:00 +0000
Seen: 18 times
Last updated: Feb 16 '23
How can the rejection of the class text_plain from JavaMail API due to a VerifyError be confirmed?
What is the process for generating a dynamic subdomain/URL using vue.js?
How can the style of the loader be modified while the form submission is being processed?
I'm attempting to develop a Javascript-based comments section for my website.
What are some feasible methods to enable MIDI file playback on a web browser?
How can I resolve the issue of being unable to use Fetch to POST an array of Selected Checkboxes?
What is the method to hide the scroll button when reaching the bottom?
What is the process of using a custom nunjucks filter to filter collections in an eleventy template?