指定的密钥不存在通过签名url从gcs访问文件时

wfauudbj  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(393)
// 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>
mwecs4sa

mwecs4sa1#

错误表明您的文件不存在于gcs bucket中。
请仔细检查您试图检索的路径。
你能用电脑下载那个文件吗 gsutil ?

eiee3dmh

eiee3dmh2#

如果您的文件是.pdf文件,则需要在生成signedurl之前更新google云存储中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"

通过在google云存储java api的blobinfo.builder类中搜索,我们可以在java中设置内容类型和内容配置如下:

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

相关问题