[英]Upload JSON string to Google Cloud Storage without a file
我的情况是当我从其他来源接收数据作为 JSON 字符串时,然后我想将此字符串上传到 Google Cloud Storage,而不将此字符串写入本地文件并上传此文件。 有没有办法做到这一点。 谢谢你。 看起来像下面的这段代码
storage
.bucket(bucketName)
.upload(jsonString, { destination: 'folder/test.json' })
.then(() => {
console.log('success');
})
.catch((err) => {
console.error('ERROR:', err);
});
所以我预计谷歌云存储会有一个文件 test.json 包含来自 jsonString 的内容
如果有人在这里寻找答案的片段,它正在使用 file.save()。 请注意,数据应该是字符串化的。
const storage = new Storage();
exports.entry_point = (event, context) => {
var data = Buffer.from(event.data, 'base64').toString();
data = transform(data)
var datetime = new Date().toISOString()
var bucketName = storage.bucket('your_filename')
var fileName = bucketName.file(`date-${datetime}.json`)
fileName.save(data, function(err) {
if (!err) {
console.log(`Successfully uploaded ${fileName}`)
}});
//-
// If the callback is omitted, we'll return a Promise.
//-
fileName.save(data).then(function() {});
};
我认为你只需要 JSON.stringify() 你的对象
storage
.bucket(bucketName)
.upload(JSON.stringify(jsonString), { destination: 'folder/test.json' })
.then(() => {
console.log('success');
})
.catch((err) => {
console.error('ERROR:', err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.