【发布时间】:2021-01-23 16:49:50
【问题描述】:
我正在尝试使用 node.js 后端中的 Admin SDK 将图像上传到我的 Firebase 的 Cloud Firestore。以下是我的代码:
const admin = require("firebase-admin");
const serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "something.appspot.com.appspot.com"
});
var bucket = admin.storage().bucket();
bucket.upload('ns_icon.png').then(() => {
console.log("uploaded");
})
以下代码产生以下错误消息:
server is running on port: 8000
(node:37108) UnhandledPromiseRejectionWarning: Error: Not Found
at new ApiError (C:\Users\PC\OneDrive - University of Waterloo\Hugue\Startups\Pllanet\Tech\Admin Console\node_modules\@google-cloud\common\build\src\util.js:59:15)
at Util.parseHttpRespBody (C:\Users\PC\OneDrive - University of Waterloo\Hugue\Startups\Pllanet\Tech\Admin Console\node_modules\@google-cloud\common\build\src\util.js:194:38)
at Util.handleResp (C:\Users\PC\OneDrive - University of Waterloo\Hugue\Startups\Pllanet\Tech\Admin Console\node_modules\@google-cloud\common\build\src\util.js:135:117)
at C:\Users\PC\OneDrive - University of Waterloo\Hugue\Startups\Pllanet\Tech\Admin Console\node_modules\@google-cloud\common\build\src\util.js:434:22
at onResponse (C:\Users\PC\OneDrive - University of Waterloo\Hugue\Startups\Pllanet\Tech\Admin Console\node_modules\retry-request\index.js:214:7)
at C:\Users\PC\OneDrive - University of Waterloo\Hugue\Startups\Pllanet\Tech\Admin Console\node_modules\teeny-request\build\src\index.js:150:17
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:37108) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting
a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:37108) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
有上传代码的文件在ns_icon.png的同一目录下,即根目录。是不是我指定的路径不对?
【问题讨论】:
-
文档建议您应该传递要上传的文件的完全限定路径名。 googleapis.dev/nodejs/storage/latest/Bucket.html#upload
标签: javascript node.js firebase google-cloud-storage firebase-storage