目前,我正在使用@google-cloud/storageNPM软件包将文件直接上传到谷歌云存储桶.这需要一些技巧,因为我只有图像的base64编码字符串.我必须:
- 解码字符串
- 将其另存为文件
- 将文件路径发送到下面的脚本,以上传到谷歌云存储
- 删除本地文件
我希望避免将文件完全存储在文件系统中,因为我使用的是Google App Engine,如果删除操作因任何原因无法工作,我不想使文件系统过载/将垃圾文件留在文件系统中.这就是我的上传脚本现在的样子:
// Convert the base64 string back to an image to upload into the Google Cloud Storage bucket
var base64Img = require('base64-img');
var filePath = base64Img.imgSync(req.body.base64Image, 'user-uploads', 'image-name');
// Instantiate the GCP Storage instance
var gcs = require('@google-cloud/storage')(),
bucket = gcs.bucket('google-cloud-storage-bucket-name');
// Upload the image to the bucket
bucket.upload(__dirname.slice(0, -15) + filePath, {
destination: 'profile-images/576dba00c1346abe12fb502a-original.jpg',
public: true,
validation: 'md5'
}, function(error, file) {
if (error) {
sails.log.error(error);
}
return res.ok('Image uploaded');
});
是否可以直接上传图像的base64编码字符串,而不必将其转换为文件,然后使用路径上传?