【问题标题】:Signed URLs with Content Length restriction具有内容长度限制的签名 URL
【发布时间】:2019-10-19 02:07:49
【问题描述】:

我有以下代码为文件生成签名的上传网址:

const { Storage } = require('@google-cloud/storage');
const bucketName = 'foo-upload';
const storage = new Storage();
const uploadBucket = storage.bucket(bucketName);

exports.generateUploadLink = async (req, res) => {
  let contentId = req.query.contentId || req.body.contentId;

  let file = uploadBucket.file(`${contentId}.mp3`);

  try {
    let signedUrl = await file.getSignedUrl({
      action: 'write',
      expires: Date.now() + (30 * 1000),
      contentType: 'audio/mpeg'
    });
    res.status(200).json({meta: { status: 'OK'}, uploadUrl: signedUrl[0]});
  } catch(err) {
    console.log(`Error while getting signed url: ${err}`);
    res.status(500).json({meta: { status: 'FAIL', message: `Could not generate signed upload url for: ${contentId}`, error: err}});
  }
  return;
};

如何对已签名的 Url 实施内容长度限制,以便随后 PUT 到 Url 的超过一定字节数的文件将被拒绝?

【问题讨论】:

    标签: node.js google-cloud-platform google-cloud-storage


    【解决方案1】:

    您可以指定上传必须满足的策略文档。

    Policy Document

    设置content-length-range

    将上传限制为 0 到 1,000,000,000 字节且仅适用于 Content-Type image/jpeg 的示例。

    {
     "expiration": "2020-06-16T11:11:11Z",
     "conditions": [
      ["starts-with", "$key", "" ],
      {"acl": "bucket-owner-read" },
      {"bucket": "example-bucket"},
      {"success_action_redirect": "http://www.example.com/success_notification.html" },
      ["eq", "$Content-Type", "image/jpeg" ],
      ["content-length-range", 0, 1000000000]
      ]
    }
    

    【讨论】:

    • 谢谢,我会检查并报告
    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 2012-05-07
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    相关资源
    最近更新 更多