微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从 azure blob springboot 获取可共享链接SAS?

如何解决如何从 azure blob springboot 获取可共享链接SAS?

我需要获得可共享的链接,世界可以在其中查看来自 azure blob 且使用 spring boot 过期的文件。提前致谢!

解决方法

尝试下面的代码来获取对 blob 具有读取权限的 SAS 令牌:

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>
  <groupId>com.azure</groupId>
  <artifactId>azure-storage-blob</artifactId>
  <version>12.9.0</version>
</dependency>

结果:

enter image description here

使用此 URL 访问此文件:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。