我正在尝试使用node.js将文档文件从本地上传到firebase文件存储。
但我得到的错误如下:
错误:未找到
以下是我的代码。你能不能请任何人帮助我。
const keyFilename="./xxxxxxxx.json";
const multer = Multer({
fileFilter: function(req, file, cb){
checkFiletype(file, cb)
},
storage: Multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024 // no larger than 5mb, you can change as needed.
}
});
const storage = googleStorage({
projectId: 'xxxx' ,
keyFilename :keyFilename
});
const bucket = storage.bucket("gs://xxxx/xxxx");
const uploadImageToStorage = (file) => {
let prom = new Promise((resolve, reject) => {
if (!file) {
reject('No image file');
}
console.log('filw',file);
let newFileName = `${file.originalname}_${Date.now()}`;
let fileUpload = bucket.file(newFileName);
const blobStream = fileUpload.createWriteStream({
metadata: {
contentType: file.mimetype
}
});
blobStream.on('error', (error) => {
console.log(error);
reject('Something is wrong! Unable to upload at the moment.');
});
blobStream.on('finish', () => {
// The public URL can be used to directly access the file via HTTP.
const url = util.format(`https://storage.googleapis.com/${bucket.name}/${fileUpload.name}`);
resolve(url);
});
blobStream.end(file.buffer);
});
return prom;
}
错误:以下是我将文件上传到firebase文件存储区时遇到的错误。
Error: Not Found
at Request._callback (C:\Users\rkanumetta\Desktop\uploadapi\node_modules\gcs
-resumable-upload\build\src\index.js:248:33)
at Request.self.callback (C:\Users\rkanumetta\Desktop\uploadapi\node_modules
\request\request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (C:\Users\rkanumetta\Desktop\uploadapi\node_modules\r
equest\request.js:1157:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (C:\Users\rkanumetta\Desktop\uploadapi\node_m
odules\request\request.js:1079:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
Something is wrong! Unable to upload at the moment.
答案 0 :(得分:0)
希望对您有用。我从本地上传了一个文件,然后在上传到Firebase存储中之后使用UUID添加了访问令牌,然后生成了下载URL。如果我们点击该生成网址,它将自动下载文件。
const keyFilename="./xxxxx.json"; //replace this with api key file
const projectId = "xxx" //replace with your project id
const bucketName = "xx.xx.appspot.com"; //Add your bucket name
var mime=require('mime-types');
const uuidv1 = require('uuid/v1');//this for unique id generation
//const mime = require('mime');
const gcs = require('@google-cloud/storage')({
projectId,
keyFilename
});
const bucket = gcs.bucket(bucketName);
const filePath = "./sample.odp";
const remotePath = "/XXXX";
const fileMime = mime.lookup(filePath);
//we need to pass those parameters for this function
var upload = (filePath, remoteFile, fileMime) => {
let uuid = uuidv1();
return bucket.upload(filePath, {
destination: remoteFile,
uploadType: "media",
metadata: {
contentType: fileMime,
metadata: {
firebaseStorageDownloadTokens: uuid
}
}
})
.then((data) => {
let file = data[0];
return Promise.resolve("https://firebasestorage.googleapis.com/v0/b/" + bucket.name + "/o/" + encodeURIComponent(file.name) + "?alt=media&token=" + uuid);
});
}
//This function is for generation download url
upload(filePath, remotePath, fileMime).then( downloadURL => {
console.log(downloadURL);
});