gpt4 book ai didi

node.js - 错误: Not Found in firebase file storage

转载 作者:太空宇宙 更新时间:2023-11-04 00:09:06 24 4
gpt4 key购买 nike

我正在尝试使用 node.js 将文档文件从本地上传到 firebase 文件存储。

但我收到如下错误:

Error: Not Found

下面是我的代码。请有人帮我解决这个问题吗?

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.

最佳答案

希望对您有用。我从本地上传了一个文件,然后使用 UUID 添加了访问 token ,然后上传到 firebase 存储中。之后我生成了下载 url。如果我们点击生成 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);

});

关于node.js - 错误: Not Found in firebase file storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50797637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com