Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
756 views
in Technique[技术] by (71.8m points)

elixir - How to force Google Storage file to have `content-disposition: inline` instead of `attachment`?

I'm using GoogleApi.Storage.V1.Api.Objects.storage_objects_insert_simple to upload files to Google Storage. However, it seems that no matter what I do, the content-disposition HTTP response header is being set to attachment which forces browsers to download a file rather than displaying it in a browser tab.

Here's my usage:

{: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
  )

Does anyone know how to force the content disposition header to have a value of inline?

Update

After inspecting the value of object, I'm seeing the following:

%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==",
  ...

However, after fetching a file via curl -I <url>, I see the following:

HTTP/2 200 
...
content-type: image/png
content-disposition: attachment
...
question from:https://stackoverflow.com/questions/65838239/how-to-force-google-storage-file-to-have-content-disposition-inline-instead-o

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems like my mistake here was in using the mediaLink field as the URL. This URL even has download as part of the URL: https://storage.googleapis.com/download/storage/....

If I instead use name field to build the URL, then it works as expected.

send_resp(
  conn,
  200,
  "https://storage.googleapis.com/my-bucket/" <> object.name
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...