我正在使用文本转 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);
}
});
我是一名优秀的程序员,十分优秀!