【发布时间】:2021-07-30 04:14:20
【问题描述】:
我在这里使用 Next.js。我需要将图像上传到我的 Firebase 存储。但它给了我大量的错误。好的,它给了我 1 个错误。
初始化firebase admin sdk
// firebase.js
import * as admin from "firebase-admin";
import serviceAccount from "./firebase.serviceAccount.json";
if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL
});
}
const firestore = admin.firestore();
const storage = admin.storage();
c
export { firestore, storage };
上传图片
// route.js
import { storage } from "../../firebase";
const router = async (req, res) => {
// ...
// Getting file from api request and so on...
// ...
const file = <an instance of File object>;
const currentTime = Date.now();
const fileName = file.name;
const fileTitle = `${currentTime}-${fileName}`;
const metadata = {
contentType: file.type
};
const task = storage.ref().child(fileTitle).put(file, metadata);
task.then(snapshot => snapshot.ref.getDownloadURL())
.then(console.log)
.catch(console.error);
};
export default router;
错误
无法导入 Node.js 的 Cloud Storage 客户端库。确保安装“@google-cloud/storage”npm 包。原始错误:Error: EIO: i/o error, read
虽然我已经在我的包列表中安装了@google-cloud/storage
【问题讨论】:
标签: javascript node.js firebase next.js firebase-storage