【发布时间】:2021-01-26 06:59:21
【问题描述】:
我正在使用 GoogleApi.Storage.V1.Api.Objects.storage_objects_insert_simple 将文件上传到 Google 存储。但是,似乎无论我做什么,content-disposition HTTP 响应标头都被设置为attachment,这会强制浏览器下载文件而不是在浏览器选项卡中显示它。
这是我的用法:
{:ok, object} =
GoogleApi.Storage.V1.Api.Objects.storage_objects_insert_simple(
gconn,
"my-uploads-bucket",
"multipart",
%{
name: Path.basename(path),
contentType: content_type,
contentDisposition: "inline" # <-- this doesn't seem to have an effect
},
path
)
有谁知道如何强制内容处置标头的值为inline?
更新
检查object 的值后,我看到以下内容:
%GoogleApi.Storage.V1.Model.Object{
acl: nil,
bucket: "my-uploads-bucket",
cacheControl: nil,
componentCount: nil,
contentDisposition: "inline", # <-- !!!
contentEncoding: nil,
contentLanguage: nil,
contentType: "image/png",
crc32c: "MGa01Q==",
...
但是,通过curl -I <url> 获取文件后,我看到以下内容:
HTTP/2 200
...
content-type: image/png
content-disposition: attachment
...
【问题讨论】:
-
即使在使用 gsutil 强制
Content-Disposition元数据后,您是否会得到相同的行为?至少有 this reference 用于该特定标头和至少签名 URL(我相信您没有使用)之间的问题,我相信您可能会觉得有用。 -
这可能是库本身的问题。在这种情况下,我建议您打开一个问题here。
-
@DanielOcando 感谢您的回复。我没有使用签名 URL,因为任何拥有该 URL 且不需要身份验证的人都应该可以访问这些 URL。是否可能需要在 Google Cloud 中进行一些配置才能将内容处置标头设置为
inline? -
如果你使用
gsutil setmeta -h "Content-Disposition: inline" gs://[YOUR-BUCKET]/[YOUR-OBJECT],你还会有这种行为吗?正如here 所建议的那样,Content-Disposition 标头的值存储为对象元数据,并在以后对对象的请求中使用。 -
所以,我的错误似乎在于使用
mediaLink字段作为 URL。这个 URL 甚至有download作为 URL 的一部分:https://storage.googleapis.com/download/storage/...。如果我改为使用name字段来构建 URL,那么它会按预期工作
标签: google-cloud-storage elixir