gpt4 book ai didi

node.js - 文件上传到 S3 - 0 字节

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:53 24 4
gpt4 key购买 nike

我正在使用文本转 MP3 模块来创建文本转语音 MP3 文件。现在,该文件已成功保存到我的文件系统中。我也正在尝试将其上传到S3。它显示在 S3 中,但文件是空的(0 字节)。

txtomp3.getMp3(myverse, function(err, binaryStream){

if(err){
console.log(err);
return;
}
var file = fs.createWriteStream("FileName.mp3"); // write it down the file
file.write(binaryStream);
file.end();

var myfile = fs.createReadStream("FileName.mp3");

AWS.config.update({ accessKeyId: '...', secretAccessKey: '...' });

var s3 = new AWS.S3();
s3.putObject({
Bucket: 'myverses',
Key: 'del2.mp3',
Body: myfile,
ACL: 'public-read'
},function (resp) {
console.log(arguments);
console.log('Successfully uploaded package.');
});
});
});

最佳答案

Uploading a File to an Amazon S3 Bucket AWS 文档中有关于如何上传文件的清晰示例:

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});

// Create S3 service object
s3 = new AWS.S3({apiVersion: '2006-03-01'});

// call S3 to retrieve upload file to specified bucket
var uploadParams = {Bucket: process.argv[2], Key: '', Body: ''};
var file = process.argv[3];

var fs = require('fs');
var fileStream = fs.createReadStream(file);
fileStream.on('error', function(err) {
console.log('File Error', err);
});
uploadParams.Body = fileStream;

var path = require('path');
uploadParams.Key = path.basename(file);

// call S3 to retrieve upload file to specified bucket
s3.upload (uploadParams, function (err, data) {
if (err) {
console.log("Error", err);
} if (data) {
console.log("Upload Success", data.Location);
}
});

关于node.js - 文件上传到 S3 - 0 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48727507/

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