4

I want to generate a SAS url that i can share with user to connect to storage account and upload a file to any location.

How can i generate the SAS url using java api.

i found one documentation but looks like all api are depreciated https://azuresdkdocs.blob.core.windows.net/$web/java/azure-storage-blob/12.0.0/com/azure/storage/blob/sas/BlobServiceSasSignatureValues.html


Env:
Java Version: 8.0
BLOB STORAGE JAVA SDK: group: 'com.azure', name: 'azure-storage-blob', version: '12.8.0'
6
  • @Frank Gong not really helped.. i can't even see there is example documentation available for java learn.microsoft.com/en-us/azure/storage/blobs/… Commented Nov 11, 2020 at 6:09
  • Please check if this helps github.com/Azure/azure-storage-java Commented Nov 11, 2020 at 6:13
  • @MohitSingh, This is the document I post, but I found some problems, so I deleted it. Commented Nov 11, 2020 at 6:20
  • @Frank Gong currently we don't have any documentation for java api? the post you shared still had problem? Commented Nov 11, 2020 at 6:27
  • @MohitSingh. Please wait for me to test. Commented Nov 11, 2020 at 6:28

3 Answers 3

5

Following code worked for me.

BlobContainerSasPermission blobContainerSasPermission = new BlobContainerSasPermission()
                .setReadPermission(true)
                .setWritePermission(true)
                .setListPermission(true);
        BlobServiceSasSignatureValues builder = new BlobServiceSasSignatureValues(OffsetDateTime.now().plusDays(1), blobContainerSasPermission)
                .setProtocol(SasProtocol.HTTPS_ONLY);
        BlobClient client = new BlobClientBuilder()
                .connectionString("connection string")
                .blobName("")
                .buildClient();
        String blobContainerName = "test";
        return String.format("https://%s.blob.core.windows.net/%s?%s",client.getAccountName(), blobContainerName, client.generateSas(builder));
Sign up to request clarification or add additional context in comments.

Comments

0

There's a REST API that you can call. See at https://learn.microsoft.com/en-us/rest/api/storageservices/create-service-sas.

2 Comments

do we have java api for building this url?
0

You can use azure storage SDK for maven as follows:

 <dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-storage</artifactId>
    <version>8.3.0</version>
</dependency>

Then follow the below code to generate SAS token which you can append to your storage URL.

    CloudStorageAccount account = CloudStorageAccount.parse(blobConnectionString);
        
     // Create a blob service client
     CloudBlobClient blobClient = account.createCloudBlobClient();
                              
     CloudBlobContainer container = blobClient.getContainerReference(containerName);
    
     Date expirationTime = Date.from(LocalDateTime.now().plusDays(7).atZone(ZoneOffset.UTC).toInstant());
    SharedAccessBlobPolicy sharedAccessPolicy=new SharedAccessBlobPolicy();
    sharedAccessPolicy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ, 
        SharedAccessBlobPermissions.WRITE,SharedAccessBlobPermissions.ADD));
    sharedAccessPolicy.setSharedAccessStartTime(new Date());
    sharedAccessPolicy.setSharedAccessExpiryTime(expirationTime);
        
    String sasToken = container.generateSharedAccessSignature(sharedAccessPolicy, null);

1 Comment

That dependency is wrong. It's com.azure not com.microsoft.azure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.