I need to get sharable link, where world can see file from azure blob with expiry using spring boot. Thanks in Advance!
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:

Access this file using this URL:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With