// This method is used to return the signed URL , After getting the URL I can able to view the //preview of the file which is in GCS, But in my case signed URL is not working its throwing the below //Exception while accessing in the browser. How to resolve this issue?
BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, gcsFolderPrefix + fullFileName)).build();
URL url = storage.signUrl(blobInfo, 15, TimeUnit.MINUTES, Storage.SignUrlOption.withV4Signature());
return url;
//Exception
//此XML文件似乎没有任何关联的样式信息。文档树//如下所示。
<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Details>No such object: google storage location</Details>
</Error>
错误表明GCS存储桶中不存在您的文件
请仔细检查您尝试检索的路径。
你能用gsutil
下载那个文件吗?
如果您的文件是.pdf文件,则在生成SignedUrl之前,您需要在谷歌云存储中更新blob的元数据。
BlobInfo blobinfo = BlobInfo.newBuilder(BlobId.of(bucketName, gcsFolderPrefix + fullFileName))
//Add the correct content type
.setContentType("application/pdf")
.build();
// Update the Blob
storage.update(blobinfo);
URL url = storage.signUrl(blobInfo, 15, TimeUnit.MINUTES,Storage.SignUrlOption.withV4Signature());
要预览.pdf文件,必须先下载该文件,然后才能看到它。
如果你只想在浏览器中查看文件,你应该向浏览器指示应该在浏览器中观看文件,因此HTTP响应应该包括以下标题:
Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"
通过在Blob信息中搜索。谷歌云存储Java API的Builder类,我们可以在Java中设置Content-Type和Content-Disposition如下:
BlobInfo blobinfo = BlobInfo.newBuilder(BlobId.of(bucketName, gcsFolderPrefix + fullFileName))
//Add the correct content type
.setContentType("application/pdf")
.setContentDisposition("inline")
.build();
// Update the Blob
storage.update(blobinfo);
URL url = storage.signUrl(blobInfo, 15, TimeUnit.MINUTES,Storage.SignUrlOption.withV4Signature());
要下载文件而不是查看:
Content-Type: application/pdf
Content-Disposition: attachment; filename="filename.pdf"
BlobInfo blobinfo = BlobInfo.newBuilder(BlobId.of(bucketName, gcsFolderPrefix + fullFileName))
//Add the correct content type
.setContentType("application/pdf")
.setContentDisposition("attachment")
.build();
// Update the Blob
storage.update(blobinfo);
URL url = storage.signUrl(blobInfo, 15, TimeUnit.MINUTES,Storage.SignUrlOption.withV4Signature());