java - 错误:无法使用 Google Cloud StorageOptions for SignUrl 加载 PDF 文档
问题描述
我正在尝试使用 google cloud SignUrl 从 Web API 获取 PDF 文档,并希望在应用程序中显示。获取“错误:无法加载 PDF 文档”。
我关注signurl:- https://cloud.google.com/storage/docs/access-control/signed-urls
下面的代码适用于图像文件。
我尝试在代码中注释。它的代码也为所有文件和pdf文件获取signurl,但我无法弄清楚它是如何获取pdf文件以供查看的?
请通过我的代码:
控制器类:-
class ControllerClass{
public Result show(String fileName) throws IOException {
return redirect(mModelService.getSignedUrl(fileName));
// -- TRY other way to get pdf file.
//return redirect(mModelService.getSignedUrl(userId,fileName));
// .withHeader("Content-Type","application/pdf");
// .as("application/octet-stream");
}
}
获取签名网址:-
public String getSignedUrl(String fileName) throws IOException {
String remotePath = filePath(fileName);
BlobId blobId = BlobId.of("gcsBucket", remotePath);
Blob blob = getStorage().get(blobId);
if(blob != null && blob.exists()){
URL url = blob.signUrl(ttlMinutes, TimeUnit.MINUTES, SignUrlOption.httpMethod(HttpMethod.GET)
);
return url.toString();
}
获取存储空间:-
public com.google.cloud.storage.Storage getStorage() {
if(storage == null) {
synchronized (lock) {
if(storage == null) {
storage = StorageOptions.getDefaultInstance().getService();
}
}
}
return storage;
}
// -- TRY other way to get pdf file.
public String getSignedUrl(String fileName) throws IOException {
BlobId blobId = BlobId.of(gcsBucket, remotePath);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
/*.setContentType("application/doc")*/
/*.setContentType("application/pdf")*/
/*.setContentType("application/octet-stream")*/
.build();
URL url = cloudStorageFactory.getStorage().signUrl(blobInfo,ttlMinutes, TimeUnit.MINUTES, SignUrlOption.httpMethod(HttpMethod.GET)
// ,SignUrlOption.withContentType()
);
return url.toString();
}
解决方案
推荐阅读
- angular - Syncfusion富文本编辑器Angular 9 iFrame在材质组件中不起作用
- c# - 在 c# 上读取 xml 文件,读取阿拉伯文本提供加密字符串而不是实际的 utf-8
- html - 固定标题和侧边栏将内容推送到它们下方
- javascript - 页面加载后我的第一个 api 请求总是失败
- php - Instagram 追随者计数脚本让我很难过。(PHP)
- javascript - 通过ajax调用传递参数 - ajax url
- batch-file - 杀死、清理并重新启动 Firefox
- python - 从字典中获取所有值(包括字典中字典的值)
- python - 使用 scapy 在 IP 示例中制作 IP
- python - 如何在 Django 中重复多对多关系?