错误:无法使用适用于SignUrl的Google Cloud StorageOptions加载PDF文档

时间:2018-08-20 13:09:20

标签: java playframework google-cloud-storage

我正在尝试使用Google Cloud SignUrl从Web API获取PDF文档,并希望在应用程序中显示。出现“ 错误:无法加载PDF文档”。

我关注签名网址:- https://cloud.google.com/storage/docs/access-control/signed-urls

下面的代码对图像文件有效。

我尝试在代码中注释。 它的代码也获取所有文件和pdf文件的signurl,但是我无法弄清楚它如何获取pdf文件以供查看?

请输入我的代码:

ControllerClass :-

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();
}

0 个答案:

没有答案