Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get sharable link(SAS) from azure blob springboot?

I need to get sharable link, where world can see file from azure blob with expiry using spring boot. Thanks in Advance!

like image 816
mothi sampath Avatar asked Jan 30 '26 19:01

mothi sampath


1 Answers

Try code below to get a SAS token with read permission for a blob :

import com.azure.storage.blob.sas.BlobServiceSasSignatureValues;
import com.azure.storage.blob.sas.BlobSasPermission;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import java.time.OffsetDateTime;

public class App {
        public static void main(String[] args) {

                String connString = "<storage account connection string>";
                String containerName = "<container name>";
                String blobName = "<blob name>";

                BlobServiceClient client = new BlobServiceClientBuilder().connectionString(connString).buildClient();
                BlobClient blobClient = client.getBlobContainerClient(containerName).getBlobClient(blobName);

                BlobSasPermission blobSasPermission = new BlobSasPermission().setReadPermission(true); // grant read
                                                                                                       // permission
                                                                                                       // onmy
                OffsetDateTime expiryTime = OffsetDateTime.now().plusDays(2); // after 2 days expire
                BlobServiceSasSignatureValues values = new BlobServiceSasSignatureValues(expiryTime, blobSasPermission)
                                .setStartTime(OffsetDateTime.now());

                System.out.println(blobClient.getBlobUrl() + "?" + blobClient.generateSas(values));

        }
}

maven dependency:

<dependency>
  <groupId>com.azure</groupId>
  <artifactId>azure-storage-blob</artifactId>
  <version>12.9.0</version>
</dependency>

Result:

enter image description here

Access this file using this URL:

enter image description here

like image 196
Stanley Gong Avatar answered Feb 02 '26 12:02

Stanley Gong