gpt4 book ai didi

java - 在 SpringBoot 应用程序中下载时 Azure BlobNotFound

转载 作者:行者123 更新时间:2023-11-30 06:05:05 25 4
gpt4 key购买 nike

我的任务是使用 Azure Blob 存储从应用程序下载简单的 .txt 文件。该代码应该可以工作。我没有写它,但它对我来说看起来不错,从我稍后将在这篇文章中展示的内容来看,它确实连接到了 Azure,而且更重要的是,它只有在我测试应用程序时才真正起作用本地主机,但不在公开网站上。

这些是我所做的步骤:

  1. 上传文件到存储(下划线的是其中之一): enter image description here
  2. 添加了正确的链接到应通过 REST API 下载附件的按钮
  3. 当然,我还添加了对数据库中附件的引用(其 ID、名称等)
  4. 这是它在前端的样子: enter image description here
  5. 这就是我得到的: enter image description here

我在某处看到这可能是由 Azure CORS 设置不允许应用访问存储引起的。这是我到目前为止所做的:

  1. 访问portal.azure.com并更改CORS设置,如下所示: enter image description here
  2. 在此 Microsoft 链接下找到了有关将一些代码放入应用程序的信息,但它不是 Java。我猜Java中有一些类似的方式: https://blogs.msdn.microsoft.com/windowsazurestorage/2014/02/03/windows-azure-storage-introducing-cors/ 。 Azure Portal中添加CORS规则后还需要吗?

另外,我发现这可能是由存储访问权限引起的。公共(public)访问级别设置为容器: enter image description here

不确定它是否提供任何内容,但这些是容器的属性: enter image description here

我收到的 BlobNotFound 错误还可能是什么问题?希望我已经在这里提供了足够的信息,但如果需要更多信息,请在评论中留言,我会提供。

这是应该下载此方法附件的代码,包含在 3 个类中:

Controller 类部分:

@GetMapping("/download/{id}")
@ResponseStatus(HttpStatus.OK)
public void downloadAttachment(@PathVariable long id, HttpServletResponse response) throws IOException {
dataUploadRequestAttachmentService.downloadStaticAttachment(response, id);
}

Controller 服务类部分:

public void downloadStaticAttachment(HttpServletResponse response, long id) throws IOException {
ArticleAttachment articleAttachment = this.findAttachment(id);
String mimeType = URLConnection.guessContentTypeFromName(articleAttachment.getName());

if (mimeType == null){
mimeType = "application/octet-stream";
}

response.setContentType(mimeType);
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", articleAttachment.getName()));

azureBlobStorageArticleAttachmentService.downloadArticleAttachment(
articleAttachment.getName(),
articleAttachment.getId(),
response.getOutputStream()
);
}

以及 AzureBlobStorageArticleAttachmentService 类:

public void downloadArticleAttachment(String attachmentName, Long articleId, OutputStream outputStream) {
try {
CloudBlockBlob blob = container.getBlockBlobReference(String.format("%s_%s", articleId, attachmentName));
blob.download(outputStream);
} catch (URISyntaxException | StorageException e) {
e.printStackTrace();
log.error(String.format("Download article attachment %s error", attachmentName));
}
}

最佳答案

根据您的描述,请调试检查代码中是否获取到正确的 blob 名称:CloudBlockBlob blob = container.getBlockBlobReference(String.format("%s_%s",articleId,attachmentName));

这里有一个关于如何使用Java SDK下载blob的演示供您引用:

/// <summary>
/// download blob to memory
/// </summary>
/// <param name="containerName">blob container name</param>
/// <param name="blobName">blob Name</param>
public static ByteArrayOutputStream downloadBlobToMemory(String containerName, String blobName) {
CloudStorageAccount account = null;
CloudBlobContainer container = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
account = CloudStorageAccount.parse(ConnString);
CloudBlobClient client = account.createCloudBlobClient();
container = client.getContainerReference(containerName);
container.createIfNotExists();
CloudBlockBlob cloudBlockBlob = container.getBlockBlobReference(blobName);

byteArrayOutputStream=new ByteArrayOutputStream();
cloudBlockBlob.download(byteArrayOutputStream);

}catch(Exception ex) {
ex.printStackTrace();
}

return byteArrayOutputStream;
}


/// <summary>
/// download blob to local disk
/// </summary>
/// <param name="containerName">blob container name</param>
/// <param name="blobName">blob Name</param>
/// <param name="filePath"> for example: C:\\Test\test.txt</param>
public static void downloadBlobToDisk(String containerName, String blobName, String filePath) {
CloudStorageAccount account = null;
CloudBlobContainer container = null;
try {
account = CloudStorageAccount.parse(ConnString);
CloudBlobClient client = account.createCloudBlobClient();
container = client.getContainerReference(containerName);
container.createIfNotExists();
CloudBlockBlob cloudBlockBlob = container.getBlockBlobReference(blobName);
FileOutputStream fileOutputStream=new FileOutputStream(filePath);
cloudBlockBlob.download(fileOutputStream);
}catch(Exception ex) {
ex.printStackTrace();
}
}

关于java - 在 SpringBoot 应用程序中下载时 Azure BlobNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51513700/

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