gpt4 book ai didi

node.js - Google App Engine - 上传到存储桶后访问文件

转载 作者:行者123 更新时间:2023-12-04 23:07:09 24 4
gpt4 key购买 nike

我已经使用我的谷歌应用引擎后端将一个文件上传到我的存储桶,但现在我无法访问该文件以传入 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;

最佳答案

我使用评论中的信息创建了这个社区维基。
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');

以及如何正确构建存储桶中文件的路径以将其传递给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);

关于node.js - Google App Engine - 上传到存储桶后访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67025518/

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