【问题标题】:Resizing and Serving Images from Google Cloud Storage with Nodejs?使用 Nodejs 从 Google Cloud Storage 调整图像大小和提供图像?
【发布时间】:2018-07-17 05:17:38
【问题描述】:

我找到了这个链接upload and resize the image with google storage。我查看了 google 文档,但似乎 google 不支持 nodejs。

如何在 nodejs 上实现 API?还是直接输入google storage link并获取输出serve_url的方式?

【问题讨论】:

标签: node.js google-app-engine google-cloud-storage


【解决方案1】:

Google 确实支持 NodeJS,but on App Engine Flexible Enviroment, not in Standard environment,您分享的 article 使用的是标准环境。那篇文章也使用了特定的get_serving_url() method in Python which is not implemented in NodeJS

但是我认为您可以使用Signed URLs 实现一些类似的功能。您有一个示例 here 关于如何在 NodeJS 中获取签名 URL。如您所见,您可以获得在任何时候使用 SignedURL 时都会覆盖文件的写入权限:

//- // 生成一个 URL 以允许写入权限。这意味着任何人 使用此 URL // 可以发送带有新数据的 POST 请求,该数据将 覆盖文件。 //-

您可以使用示例中的代码:

file.getSignedUrl({
  action: 'write',
  expires: '03-17-2025'
}, function(err, url) {
  if (err) {
    console.error(err);
    return;
  }

  // The file is now available to be written to.
  var writeStream = request.put(url);
  writeStream.end('New data');

  writeStream.on('complete', function(resp) {
    // Confirm the new content was saved.
    file.download(function(err, fileContents) {
      console.log('Contents:', fileContents.toString());
      // Contents: New data
    });
  });
}); 

获得图片的签名 URL 后,您可以在每次上传相同大小的图片时使用相同的 URL。只是你必须找到自己调整图像大小的方法。

还有 this nodejs package 用于管理签名的 URL。

【讨论】:

    猜你喜欢
    • 2014-06-16
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2021-02-04
    • 2021-01-06
    • 1970-01-01
    相关资源
    最近更新 更多