node.js - 将内容保存在 Google 存储文件中
问题描述
我尝试通过 Nodejs 将生成的文件从内存中的 JSON 保存到谷歌存储。
GCS 的保存过程运行良好,它正在创建文件,但它的抛出错误。(该过程有效,因为我没有发现错误)。
据我了解,错误来自storage.bucket(bucket).file(filename)
,
我怎样才能避免异常?
let file = storage.bucket('dev').file('widgets/test.json');
file.setMetadata({
contentType: contentType,',
cacheControl: 'public,no-cache,max-age=0',
});
return await file.save(json.stringify(content))`
错误 -
UnhandledPromiseRejectionWarning: Error: No such object: dev/widgets/test.json
解决方案
我遇到了同样的错误,经过一段时间后,我找到了一种传递 contentType 的方法。我希望有一天它会对某人有所帮助!
// upload the file
var idsObj = {"ids": ids};
var idsJSONStr = JSON.stringify(idsObj); // convert object to json string
const file = storage
.bucket(bucketName)
.file(fileName);
const isSaved = await file.save(idsJSONStr, {
contentType: 'application/json'
}).then(function(err) {
if (err && err.length != 0) {
console.error("The file is not saved", err);
return false;
} else {
return true;
}
});
console.log('The file is saved: ', isSaved);
推荐阅读
- linux - 无法在 Debian 9 机器中访问 tomcat 9 gui
- php - Laravel 在安装学说/dbal 时部署错误
- types - 使一个术语属于 Coq 中两种不同类型的不同层次结构?
- twitter-bootstrap - BrowserslistError:未知的浏览器查询`dead`
- php - Laravel API 使用 ValidationException 传递多个错误,但带有自定义的状态代码/标题
- android - 检测何时将新文件添加到文件夹中
- php - Bootstrap 网格环导致不同的高度
- google-cloud-platform - 如何使用 terraform 删除计算引擎 vm
- html - 隐藏位置:背景上的固定文本附件:固定,位置:相对;在下方滚动的文字后面
- zend-framework3 - Zend\ServiceManager\Exception\ServiceNotFoundException zend 框架 3