gpt4 book ai didi

java - 无法使用 Spring Boot 为 azure 虚拟目录中存在的文件生成下载链接

转载 作者:行者123 更新时间:2023-12-03 06:23:48 24 4
gpt4 key购买 nike

我有一个包含许多虚拟目录的容器,例如: https://accountName.blob.core.windows.net/containerName/blobName ,并且 blob 名称是 azure 上的虚拟目录,例如: number/name/UUID/anotherUUID/file.txt ,我尝试下载 file.txt 但每次都会出现以下错误:BlobNotFound虽然我可以使用 azure 门户中的 sas token 生成下载链接并获取该文件的下载链接并且下载成功,但是当我从 Spring Boot 应用程序内部生成下载链接时,我收到上面的错误。

这是上传 blob 的代码:

    BlobClient blob = container.getBlobClient(filePath); // where file path is the virutal directory path
blob.upload(BinaryData.fromBytes(bytes));

and here is the code for downloading the blob :

BlobClient blobClient = blobServiceClient.getBlobContainerClient(container name)
.getBlobClient(filepath);

OffsetDateTime expiryTime = OffsetDateTime.now()
.plusDays(1);
BlobSasPermission permission = new BlobSasPermission().setReadPermission(true);
BlobServiceSasSignatureValues values = new BlobServiceSasSignatureValues(expiryTime, permission).setStartTime(
OffsetDateTime.now());
String sasToken = blobClient.generateSas(values);

String fileUrl = blobClient.getBlobUrl();
StringBuilder stringBuilder = new StringBuilder(fileUrl);
stringBuilder.append("?");
String last = stringBuilder.toString();
String result = java.net.URLDecoder.decode(last, StandardCharsets.UTF_8.name());

return result+sasToken;

最佳答案

我使用下面的代码在我的环境中重现了您的要求并得到了预期的结果。检查它是否可以帮助您解决问题并实现您的要求。

  • 确保您的容器具有所需的权限

在这里,我使用后映射通过 REST API 调用上传文件。

@PostMapping("/upload")
public String uploadFile(@RequestParam(value = "file") MultipartFile file) throws IOException {

String str = "DefaultEndpointsProtocol=https;AccountName=<your_account_name>;AccountKey=<your_account_key>;EndpointSuffix=core.windows.net";

OffsetDateTime expiryTime = OffsetDateTime.now().plusDays(1);
BlobSasPermission permission = new BlobSasPermission().setReadPermission(true);
BlobServiceSasSignatureValues values = new BlobServiceSasSignatureValues(expiryTime, permission)
.setStartTime(OffsetDateTime.now());
BlobContainerClient container = new BlobContainerClientBuilder().connectionString(str)
.containerName("<your_container_name>").buildClient();

BlobClient blob = container.getBlobClient("ParentDir/"+"ChildDir/"+file.getOriginalFilename());
blob.upload(file.getInputStream(), file.getSize(), true);
String sasToken = blob.generateSas(values);

String result = java.net.URLDecoder.decode(blob.getBlobUrl() + "?", StandardCharsets.UTF_8.name());

return result + sasToken;
}

更新:

当调用 BlobClient 类时,如果我们将文件名作为 container.getBlobClient() 中的参数传递,它将在容器内创建文件。要在目录中创建文件,我们必须使用 / 传递目录以及 container.getBlobClient() 中的文件名,如下所示:

BlobClient blob = container.getBlobClient("ParentDir/"+"ChildDir/"+file.getOriginalFilename());

enter image description here

enter image description here

更新输出:

我可以使用生成的 URL 下载文件,如下所示:

enter image description here

关于java - 无法使用 Spring Boot 为 azure 虚拟目录中存在的文件生成下载链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75712170/

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