gpt4 book ai didi

spring-boot - 如何从 azure blob springboot 获取可共享链接(SAS)?

转载 作者:行者123 更新时间:2023-12-04 17:19:39 24 4
gpt4 key购买 nike

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

最佳答案

尝试使用下面的代码来获取对 blob 具有读取权限的 SAS token :

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

关于spring-boot - 如何从 azure blob springboot 获取可共享链接(SAS)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66998057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com