Node.js 谷歌应用引擎-上传到bucket后访问文件
我已经使用我的google app engine后端上传了一个文件到我的存储桶中,但现在我无法访问该文件以传入ffmpeg。我从try-catch中得到这个错误消息:“输入文件不存在”。我可以看到文件是上传的,因为我检查了存储桶下的开发人员控制台。我使用谷歌提供的样板代码,但添加了ffmpeg进行测试。我试图使用访问上传文件的路径,但这是不正确的,尽管我得到的是bucket.name值和blob.name值。我使用的是“flex”环境Node.js 谷歌应用引擎-上传到bucket后访问文件,node.js,express,google-app-engine,ffmpeg,Node.js,Express,Google App Engine,Ffmpeg,我已经使用我的google app engine后端上传了一个文件到我的存储桶中,但现在我无法访问该文件以传入ffmpeg。我从try-catch中得到这个错误消息:“输入文件不存在”。我可以看到文件是上传的,因为我检查了存储桶下的开发人员控制台。我使用谷歌提供的样板代码,但添加了ffmpeg进行测试。我试图使用访问上传文件的路径,但这是不正确的,尽管我得到的是bucket.name值和blob.name值。我使用的是“flex”环境 const originalFilePath = `gs:/
const originalFilePath = `gs://${bucket.name}/${blob.name}`;
以下是完整的代码:
const process = require('process'); // Required to mock environment variables
const express = require('express');
const helpers = require('./helpers/index');
const Multer = require('multer');
const bodyParser = require('body-parser');
const ffmpeg = require("ffmpeg"); //https://www.npmjs.com/package/ffmpeg
const {Storage} = require('@google-cloud/storage');
// Instantiate a storage client
const storage = new Storage();
const app = express();
app.set('view engine', 'pug');
app.use(bodyParser.json());
// Multer is required to process file uploads and make them available via
// req.files.
const multer = Multer({
storage: Multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
},
});
// A bucket is a container for objects (files).
const bucket = storage.bucket(process.env.GCLOUD_STORAGE_BUCKET);
// Display a form for uploading files.
app.get('/', (req, res) => {
res.render('form.pug');
});
// Process the file upload and upload to Google Cloud Storage.
app.post('/upload', multer.single('file'), (req, res, next) => {
if (!req.file) {
res.status(400).send('No file uploaded.');
return;
}
// Create a new blob in the bucket and upload the file data.
const blob = bucket.file(req.file.originalname);
const blobStream = blob.createWriteStream({
resumable: false,
});
blobStream.on('error', err => {
next(err);
});
blobStream.on('finish', () => {
const audioFile = helpers.replaceAllExceptNumbersAndLetters(new Date());
// this path is incorrect but I cannot find correct way to do it
const originalFilePath = `gs://${bucket.name}/${blob.name}`;
const filePathOutput = `gs://${bucket.name}/${audioFile}.mp3`;
try {
const process = new ffmpeg(originalFilePath);
process.then(function (video) {
// Callback mode
video.fnExtractSoundToMP3(filePathOutput, (error, file) => {
if (!error)
res.send(`audio file: ${file}`);
});
}, (error) => {
res.send(`process error: ${error}`);
});
} catch (e) {
res.send(`try catch error: ${JSON.stringify(e)} | bucket: ${JSON.stringify(bucket)} |
blob: : ${JSON.stringify(blob)}`);
}
});
blobStream.end(req.file.buffer);
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
module.exports = app;
我使用评论中的信息创建了这个社区wiki 显示所需的npm包
const functions = require('firebase-functions');
const { Storage } = require('@google-cloud/storage');
const path = require('path');
const os = require('os');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');
以及如何在bucket中正确构建文件的路径以将其传递到ffmpeg
// Get the file name.
const fileName = path.basename(filePath);
// Exit if the audio is already converted.
if (fileName.endsWith('_output.flac')) {
functions.logger.log('Already a converted audio.');
return null;
}
// Download file from bucket.
const bucket = gcs.bucket(fileBucket);
const tempFilePath = path.join(os.tmpdir(), fileName);
// We add a '_output.flac' suffix to target audio file name. That's where we'll upload the converted audio.
const targetTempFileName = fileName.replace(/\.[^/.]+$/, '') + '_output.flac';
const targetTempFilePath = path.join(os.tmpdir(), targetTempFileName);
const targetStorageFilePath = path.join(path.dirname(filePath), targetTempFileName);
await bucket.file(filePath).download({destination: tempFilePath});
functions.logger.log('Audio downloaded locally to', tempFilePath);
// Convert the audio to mono channel using FFMPEG.
let command = ffmpeg(tempFilePath)
.setFfmpegPath(ffmpeg_static)
.audioChannels(1)
.audioFrequency(16000)
.format('flac')
.output(targetTempFilePath);
await promisifyCommand(command);
functions.logger.log('Output audio created at', targetTempFilePath);
演示如何替换ffmpeg_static.path安装以及如何设置正确的路径
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');
let command = ffmpeg(tempFilePath)
.setFfmpegPath(ffmpegPath)
.audioChannels(1)
.audioFrequency(16000)
.format('flac')
.output(targetTempFilePath);
我认为dev env上没有
gs://..
。在本地环境中(至少对于图像),我通常会得到类似http://localhost:8080/_ah/img/encoded_gs_file:....
。这里的关键是url有编码的\u gs\u文件:
。如果这是生产,那么您应该使用urlhttps://storage.googleapis.com/${bucket.name}/${blob.name}
谢谢@NoCommandLine。我已经尝试过了,但是当我尝试将ffmpeg函数传递给它时,我遇到了一个错误。这是来自try-catch:{“code”:103,“msg”:“输入文件不存在”}的错误。你知道在以这种方式访问文件之前,需要在google开发者控制台中设置哪些特殊权限吗?经过更多检查,我现在相信这不是google API的问题,而是节点ffmpeg库的问题。它不接受带有路径的仅URL的本地文件。此repo向我展示了如何计算路径以及要使用的npm包:,此外,我还使用此帖子找到了ffmpeg静态的替代方案。现在一切都像一个符咒。