admin管理员组文章数量:1201362
My case is when I receive data from some other source as a JSON string, then I want to upload this string to Google Cloud Storage without writing this string to a local file and upload this file. Is there any way to do this. Thank you. Look like this code below
storage
.bucket(bucketName)
.upload(jsonString, { destination: 'folder/test.json' })
.then(() => {
console.log('success');
})
.catch((err) => {
console.error('ERROR:', err);
});
so I expected the Google Cloud Storage will have a file test.json with content from the jsonString
My case is when I receive data from some other source as a JSON string, then I want to upload this string to Google Cloud Storage without writing this string to a local file and upload this file. Is there any way to do this. Thank you. Look like this code below
storage
.bucket(bucketName)
.upload(jsonString, { destination: 'folder/test.json' })
.then(() => {
console.log('success');
})
.catch((err) => {
console.error('ERROR:', err);
});
so I expected the Google Cloud Storage will have a file test.json with content from the jsonString
Share Improve this question edited Aug 7, 2018 at 9:57 Quoc Van Tang asked Aug 3, 2018 at 7:28 Quoc Van TangQuoc Van Tang 1,1935 gold badges17 silver badges36 bronze badges 1- Does this answer your question? How do I upload a base64 encoded image (string) directly to a Google Cloud Storage bucket using Node.js? – Liam Commented Oct 5, 2022 at 14:03
3 Answers
Reset to default 7It looks like this scenario has already been addressed in this other question.
Both answers look good, but it will probably be easier to use the file.save() method (second answer).You can find the description of this method and yet another example here.
Hope this helps.
expanding on @ch_mike's answer
const { Storage } = require('@google-cloud/storage')
const storage = new Storage()
const bucket = storage.bucket(bucketName)
const saveJsonFile = (data) => {
const timestamp = new Date().getTime()
const fileName = `${timestamp}.json`
const file = bucket.file(fileName)
const contents = JSON.stringify(data)
return file.save(contents)
}
In case some one is looking for the snippet of the answer here it is using the file.save(). Please do note that the data should be stringfy.
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() {});
};
本文标签: javascriptUpload JSON string to Google Cloud Storage without a fileStack Overflow
版权声明:本文标题:javascript - Upload JSON string to Google Cloud Storage without a file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738624665a2103393.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论