如何使用 firebase admin sdk 获取 downloadUrl firebase 存储并使用 multer 在 nodejs 中放入 img html 的标签?
How to get downloadUrl firebase storage with firebase admin sdk and put in img html's tag in nodejs with multer?
我正在构建图片上传系统。我想使用 Firebase Admin sdk 来处理这个过程。如何让 url 存储在我的 Google 云存储中,并在上传完成后在 img 标签 (html) 中显示它们。
try {
const file = req.file;
const bucket = admin.storage().bucket();
const imageBuffer = Buffer.from(file.buffer, "base64");
const imageByteArray = new Uint8Array(imageBuffer);
const options = {
resumable: false,
metadata: { contentType: file.mimetype },
predefinedAcl: "publicRead",
public: true,
};
const files = bucket.file(`img/${file.originalname}`);
await files.save(imageByteArray, options);
const field = await files.getMetadata();
console.log(field);
} catch (e) {
console.error(e);
}
使用 Node.js Admin SDK,您应该使用 getSignedUrl()
方法执行以下操作:
const { initializeApp, cert } = require('firebase-admin/app');
const { getStorage } = require('firebase-admin/storage');
const serviceAccount = require('./path/to/serviceAccountKey.json');
initializeApp({
credential: cert(serviceAccount),
storageBucket: '<BUCKET_NAME>.appspot.com'
});
// ...
async function getSignedURL(fileName) {
const bucket = getStorage().bucket();
// Or const bucket = admin.storage().bucket(); like you do in your question, depending on where and how you execute the code
const file = bucket.file(fileName);
const signedURLconfig = { action: 'read', expires: '01-01-2030' };
// See more options of the config object in the SDK documentation (link above)
const signedURLArray = await file.getSignedUrl(signedURLconfig);
return signedURLArray[0];
}
// Use the above function to save the signed URL to Firestore
const fileName = "...";
getSignedURL(fileName)
.then(signedURL => {
admin.firestore().collection("urls_collection")
.add({url: signedURL, foo: "bar", bar: "foo"})
});
我正在构建图片上传系统。我想使用 Firebase Admin sdk 来处理这个过程。如何让 url 存储在我的 Google 云存储中,并在上传完成后在 img 标签 (html) 中显示它们。
try {
const file = req.file;
const bucket = admin.storage().bucket();
const imageBuffer = Buffer.from(file.buffer, "base64");
const imageByteArray = new Uint8Array(imageBuffer);
const options = {
resumable: false,
metadata: { contentType: file.mimetype },
predefinedAcl: "publicRead",
public: true,
};
const files = bucket.file(`img/${file.originalname}`);
await files.save(imageByteArray, options);
const field = await files.getMetadata();
console.log(field);
} catch (e) {
console.error(e);
}
使用 Node.js Admin SDK,您应该使用 getSignedUrl()
方法执行以下操作:
const { initializeApp, cert } = require('firebase-admin/app');
const { getStorage } = require('firebase-admin/storage');
const serviceAccount = require('./path/to/serviceAccountKey.json');
initializeApp({
credential: cert(serviceAccount),
storageBucket: '<BUCKET_NAME>.appspot.com'
});
// ...
async function getSignedURL(fileName) {
const bucket = getStorage().bucket();
// Or const bucket = admin.storage().bucket(); like you do in your question, depending on where and how you execute the code
const file = bucket.file(fileName);
const signedURLconfig = { action: 'read', expires: '01-01-2030' };
// See more options of the config object in the SDK documentation (link above)
const signedURLArray = await file.getSignedUrl(signedURLconfig);
return signedURLArray[0];
}
// Use the above function to save the signed URL to Firestore
const fileName = "...";
getSignedURL(fileName)
.then(signedURL => {
admin.firestore().collection("urls_collection")
.add({url: signedURL, foo: "bar", bar: "foo"})
});