如何从Node中的图像URL将图像上传到Google Cloud Storage? [英] How to upload an image to Google Cloud Storage from an image url in Node?
本文介绍了如何从Node中的图像URL将图像上传到Google Cloud Storage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出图片网址,如何将其上传到Google Cloud Storage以使用Node.js进行图片处理?
Given an image url, how can I upload that image to Google Cloud Storage for image processing using Node.js?
推荐答案
这是一个两步过程:
- Download file locally using request or fetch.
Upload to GCL with the official library.
var fs = require('fs');
var gcloud = require('gcloud');
// Authenticating on a per-API-basis. You don't need to do this if you auth on a
// global basis (see Authentication section above).
var gcs = gcloud.storage({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
// Create a new bucket.
gcs.createBucket('my-new-bucket', function(err, bucket) {
if (!err) {
// "my-new-bucket" was successfully created.
}
});
// Reference an existing bucket.
var bucket = gcs.bucket('my-existing-bucket');
var localReadStream = fs.createReadStream('/photos/zoo/zebra.jpg');
var remoteWriteStream = bucket.file('zebra.jpg').createWriteStream();
localReadStream.pipe(remoteWriteStream)
.on('error', function(err) {})
.on('finish', function() {
// The file upload is complete.
});
如果要将文件另存为jpeg图像,则需要编辑remoteWriteStream流并添加自定义元数据:
If you would like to save the file as a jpeg image, you will need to edit the remoteWriteStream stream and add custom metadata:
var image = bucket.file('zebra.jpg');
localReadStream.pipe(image.createWriteStream({
metadata: {
contentType: 'image/jpeg',
metadata: {
custom: 'metadata'
}
}
}))
我在浏览这份文档时发现了这个问题
I found this while digging through this documentation
这篇关于如何从Node中的图像URL将图像上传到Google Cloud Storage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文