![](/img/trans.png)
[英]Error when uploading file to Google Cloud Storage in node.js
[英]Uploading to google cloud storage with Node.js
與Node.js和Google Cloud斗爭。 我正在嘗試將文件上傳到Google Cloud Storage中的存儲桶。 基本上,我在此答案中使用代碼: https : //stackoverflow.com/a/45253054/324691 ,但我無法使其正常工作。 這是我的代碼:
const Storage = require('@google-cloud/storage');
const storage = new Storage();
var form = new formidable.IncomingForm();
// form.maxFieldsSize = 20 * 1024 * 1024; // default
form.maxFieldsSize = 20 * 1024;
// form.maxFileSize = 200 * 1024 * 1024;
// 4 MB / minute, 1 hour
form.maxFileSize = 4 * 60 * 1024 * 1024;
// Limit to 10 minutes during tests (4 MB/minute)
form.maxFileSize = 2 * 1024 * 1024;
form.maxFileSize = 4 * 10 * 1024 * 1024;
form.encoding = 'utf-8';
form.keepExtensions = true;
form.type = 'multipart';
return new Promise((resolve, reject) => {
form.parse(req, (err, fields, files) => {
console.warn("form.parse callback", err, fields, files);
if (err) {
reject(new Error(err));
return;
}
var file = files.upload;
if(!file){
reject(new Error("no file to upload, please choose a file."));
return;
}
console.info("about to upload file as a json: " + file.type);
var filePath = file.path;
console.log('File path: ' + filePath);
console.log('File name: ' + file.name);
var bucket = storage.bucket(bucketName)
console.info("typeof bucket.upload", typeof bucket.upload);
bucket.upload(filePath, {
destination: file.name
}).then(() => {
resolve(true); // Whole thing completed successfully.
return true;
}).catch((err) => {
console.warn("catch bucket.upload, err", err);
reject(new Error('Failed to upload: ' + JSON.stringify(err)));
});
console.warn("After bucket.upload");
});
console.warn("After form.parse");
}).then(() => {
console.warn("form.parse then");
res.status(200).send('Yay!');
return true;
}).catch(err => {
console.warn("form.parse catch");
console.error('Error while parsing form: ' + err);
res.status(500).send('Error while parsing form: ' + err);
});
控制台日志中的輸出看起來正常(包括file.path和file.name),但是當它到達bucket.upload
(類型為function
)時,它會崩潰,並出現Error while parsing form: Error: Failed to upload: {"message":"Error during request."}
出現Error while parsing form: Error: Failed to upload: {"message":"Error during request."}
。 然后說Execution took 1910 ms, user function completed successfully
。 在那之后,最后一個Function worker killed by signal: SIGTERM
。
我一直在使用帶有和不帶有“ gs://”的bucketName
進行測試。
我一直在測試不同的授權方式:
const storage = new Storage({ projectId: projectId });
const storage = new Storage({ keyFileName: 'path-to-keyfile.json'});
const storage = new Storage();
密鑰文件是服務帳戶的密鑰文件(請參閱https://cloud.google.com/docs/authentication/getting-started )。
這是一個Firebase項目,我正在本地Firebase服務器上進行測試( firebase serve --only functions,hosting
)。
這有什么問題嗎?
存儲的身份驗證應如下所示:
const storage = new Storage({
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
databaseURL: "YOUR_DATABASE_URL",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGE_SENDER_ID"
});
與本地文件相比,使用多部分外觀也有所不同。 因此,用於上傳的代碼為:
const fileName = 'file_name.extension";
const fileUpload = bucket.file(fileName);
const uploadStream = fileUpload.createWriteStream({
metadata: {
contentType: file.mimetype
}
});
uploadStream.on('error', (err) => {
console.log(err);
return;
});
uploadStream.on('finish', () => {
console.log('Upload success');
});
uploadStream.end(file.buffer);
最后,bucketName不應包含gs://-它應如下所示: project_id.appspot.com 。
PS。 上面的代碼中使用busboy處理了mimetype的上傳。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.