给定一个图像 url,我如何将该图像上传到 Google Cloud Storage 以使用 Node.js 进行图像处理?
原文由 Harris Lummis 发布,翻译遵循 CC BY-SA 4.0 许可协议
给定一个图像 url,我如何将该图像上传到 Google Cloud Storage 以使用 Node.js 进行图像处理?
原文由 Harris Lummis 发布,翻译遵循 CC BY-SA 4.0 许可协议
要添加到 Yevgen Safronov 的答案中,我们可以将请求通过管道传输到写入流中,而无需将图像显式下载到本地文件系统中。
const request = require('request');
const storage = require('@google-cloud/storage')();
function saveToStorage(attachmentUrl, bucketName, objectName) {
const req = request(attachmentUrl);
req.pause();
req.on('response', res => {
// Don't set up the pipe to the write stream unless the status is ok.
// See https://stackoverflow.com/a/26163128/2669960 for details.
if (res.statusCode !== 200) {
return;
}
const writeStream = storage.bucket(bucketName).file(objectName)
.createWriteStream({
// Tweak the config options as desired.
gzip: true,
public: true,
metadata: {
contentType: res.headers['content-type']
}
});
req.pipe(writeStream)
.on('finish', () => console.log('saved'))
.on('error', err => {
writeStream.end();
console.error(err);
});
// Resume only when the pipe is set up.
req.resume();
});
req.on('error', err => console.error(err));
}
原文由 Kevin Lee 发布,翻译遵循 CC BY-SA 3.0 许可协议
13 回答13k 阅读
7 回答2.1k 阅读
3 回答1.3k 阅读✓ 已解决
3 回答2.7k 阅读✓ 已解决
6 回答1.2k 阅读✓ 已解决
2 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
这是一个 2 个步骤的过程:
如果您想将文件保存为 jpeg 图像,则需要编辑 remoteWriteStream 流并添加自定义元数据:
我在浏览本 文档 时发现了这一点