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

时间:2020-11-11 05:56:17

标签: azure azure-storage azure-storage-blobs azure-java-sdk

我想生成一个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'

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

以下代码对我有用。

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

答案 2 :(得分:0)

经过测试后,您还可以按照以下形式生成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();