【问题标题】:Can't Override Content-Disposition to 'inline' in Firebase Storage with Admin SDK for Node.js无法使用适用于 Node.js 的 Admin SDK 在 Firebase 存储中将 Content-Disposition 覆盖为“内联”
【发布时间】:2019-01-10 01:37:33
【问题描述】:

我想将 Firebase 存储中的默认 content-disposition: attachment HTTP 标头覆盖为 content-disposition: inline,以便从我的存储中提供由浏览器打开的公共图像(如 <a href="..."> 链接)以供查看(未下载)。

重要的默认标题如下:

content-disposition: attachment
content-type: image/jpeg

默认情况下,我认为是由content-disposition: attachment 强制下载的图片,这就是为什么我尝试在 Firebase Admin SDK 上传期间覆盖它:

bucket.upload(photoUrl, {
  destination,
  public: true,
  metadata: {
    contentDisposition: 'inline',
  },
}).then((data) =>
  console.log(data[1].mediaLink),
);

我清除了存储中的所有图像并使用此代码上传新图像,但我仍然收到content-disposition: attachment。也许这是设计使然,也许我的代码是错误的。

图像的 URL 类似于:https://www.googleapis.com/download/storage/v1/b/MY_PROJECT/o/IMAGE_PATH?generation=SOME_NUMBER&alt=media

如果可能,将content-disposition 覆盖为inline 的正确方法是什么?

相关问题:From firebase storage download file to iframe

更新

在 Chrome devtools 控制台中,我收到警告:“资源解释为文档,但使用 MIME 类型图像/jpeg 传输”。我尝试将 .jpg 扩展附加到上传的图像目的地,但我仍然收到此警告。

【问题讨论】:

    标签: node.js firebase firebase-storage firebase-admin


    【解决方案1】:

    元数据字段称为Content-Disposition。您是否尝试过类似以下的方法?

    const meta = {};
    meta['Content-Disposition'] = 'inline';
    bucket.upload(photoUrl, {
      destination,
      public: true,
      metadata: meta,
    });
    

    【讨论】:

    【解决方案2】:

    官方答复

    我已经在 GitHub 上的nodejs-storage 上发布了一个问题,并从@stephenplusplus 获得了this response

    我相信您需要为这种行为使用签名 URL。这 mediaLink 地址似乎是用来下载的,而且 需要授权才能访问(除非您制作了存储桶/文件 公开)。

    签名的 URL 将创建一个唯一的 URL 供用户访问,不相关 GCS 中对象的隐私。它还将继承元数据 你设置。要获取在浏览器中打开的签名 URL...

    根据他的回答修改了代码:

    bucket.upload(photoUrl, {
      destination,
    }).then((data) => {
    
      const file = data[0];
      return file.getSignedUrl({
        action: 'read',
        expires: '12-31-2118',
      });
    }).then((data) =>
      console.log(data[0]),
    );
    

    参见signed URLs in Cloud Storage documentationFile.getSignedUrl for NodeJS

    解决方法

    const blob = await fetch(mediaLink).then((res) => res.blob());
    const blobUrl = URL.createObjectURL(blob);
    // To keep adblockers away from your window:
    const win = window.open();
    win.document.head.innerHTML = `...<img src="${blobUrl}">...`;
    

    【讨论】:

      猜你喜欢
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 2019-08-02
      • 2018-07-04
      • 1970-01-01
      • 2016-12-18
      • 2021-07-30
      相关资源
      最近更新 更多