如何使用Java生成Azure Blob存储SAS URL?

问题描述

我想生成一个SAS URL,可以与用户共享以连接到存储帐户并将文件上传到任何位置。

如何使用Java api生成SAS网址。

我找到了一个文档,但看起来所有api都已弃用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'

解决方法

有一个可以调用的REST API。参见https://docs.microsoft.com/en-us/rest/api/storageservices/create-service-sas

,

以下代码对我有用。

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));
,

经过测试后,您还可以按照以下形式生成SAS:

        StorageSharedKeyCredential credential = new StorageSharedKeyCredential("<accountName>","<accountKey>");


        String sas = new BlobServiceSasSignatureValues()
                .setExpiryTime(OffsetDateTime.now().plusHours(1))
                .setPermissions(new BlobSasPermission().setReadPermission(true))
                .setContainerName("<containerName>")
                .setBlobName("blobName")
                .generateSasQueryParameters(credential)
                .encode();

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...