Ask Your Question
3

How to create SAS token for Azure Storage in Java to be used by a client?

asked 2021-07-02 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-02-16 22:00:00 +0000

lalupa gravatar image

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:

  1. Add the Azure Storage SDK dependency to your Java project. You can do this by adding the following code to your pom.xml file:
<dependency>
  <groupId>com.azure</groupId>
  <artifactId>azure-storage-blob</artifactId>
  <version>12.11.0</version>
</dependency>
  1. Import the required classes in your Java file:
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;
  1. Create a StorageSharedKeyCredential object by providing your storage account name and account key:
StorageSharedKeyCredential credential = new StorageSharedKeyCredential("<your-storage-account>", "<your-storage-account-key>");
  1. Use the 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();
  1. (Optional) If you want to generate a user delegation key for your SAS token, you can use the getUserDelegationKey method of the BlobServiceClient object:
OffsetDateTime startTime = OffsetDateTime.now();
OffsetDateTime expiryTime = startTime.plusMinutes(10);
UserDelegationKey key = client.getUserDelegationKey(startTime, expiryTime);
  1. Create a 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
  1. Get the SAS token string by calling the 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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2021-07-02 11:00:00 +0000

Seen: 18 times

Last updated: Feb 16 '23