【问题标题】:Google App Engine - Access file after uploaded to bucketGoogle App Engine - 上传到存储桶后访问文件
【发布时间】: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:。如果这是生产,那么您应该使用 url https://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


【解决方案1】:

我使用 cmets 中的信息创建了这个社区 wiki。

This repository 显示所需的 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);

This another post 展示了如何替换 ffmpeg_static.path 安装"ffmpeg-installer/ffmpeg" 以及如何设置正确的路径

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);

【讨论】:

    猜你喜欢
    • 2015-09-27
    • 2017-05-04
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 2016-01-24
    • 2020-08-21
    • 2013-09-10
    • 2015-06-30
    相关资源
    最近更新 更多