首页 > 解决方案 > 错误:无法使用 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();
}

标签: javaplayframeworkgoogle-cloud-storage

解决方案


推荐阅读