【发布时间】:2021-07-05 14:15:50
【问题描述】:
我已使用我的谷歌应用引擎后端将文件上传到我的存储桶,但现在我无法访问该文件以传递到 ffmpeg。我从 try-catch 收到此错误消息:“输入文件不存在”。我可以看到文件已上传,因为我检查了存储桶下的开发人员控制台。我正在使用谷歌提供的样板代码,但添加了 ffmpeg 进行测试。我正在尝试使用访问上传文件的路径,但这是不正确的,尽管我得到了 bucket.name 值和 blob.name 值。我为此使用了“flex”环境。
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;
【问题讨论】:
-
我认为
gs://....在开发环境中不可用。在本地环境中(至少对于图像),我通常会得到类似http://localhost:8080/_ah/img/encoded_gs_file:....的信息。这里的关键是网址有encoded_gs_file:。如果这是生产,那么您应该使用 urlhttps://storage.googleapis.com/${bucket.name}/${blob.name} -
谢谢@NoCommandLine。我已经尝试过了,但是当我尝试将其传递给 ffmpeg 函数时出现错误。这是来自 try-catch 的错误:{"code":103,"msg":"输入文件不存在"}。您是否知道在以这种方式访问文件之前需要在 google 开发者控制台中设置任何特殊权限?
-
经过更多检查后,我现在相信这不是 google api 的问题,而是 node-ffmpeg 库的问题。它不接受带有路径的仅 URL 本地文件。
-
这个 repo 向我展示了如何计算路径以及要使用哪些 npm 包:github.com/firebase/functions-samples/blob/master/…,此外,我使用这篇帖子 stackoverflow.com/questions/62652721/… 来找出 ffmpeg-static 的替代方案。现在一切都像魅力一样。
标签: node.js express google-app-engine ffmpeg