文件上传到S3 - 0字节

时间:2018-02-11 01:56:51

标签: node.js amazon-web-services amazon-s3 mp3

我正在使用text-to-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.');
  });
  });
  });

2 个答案:

答案 0 :(得分:1)

AWS文档中的

Uploading a File to an Amazon S3 Bucket有关于如何上传文件的明确示例:

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

答案 1 :(得分:-1)

这是一个解决方案,使用上传方法将存储桶中的商店对象作为laravel`中的0字节

aws.php

  return [
      'credentials' => [
      'key'    => 'XXXXX',
      'secret' => 'XXXXXXXXX',
  ],
  'region' => 'us-east-2',
  'version' => 'latest',
  ];


   $s3 = \App::make('aws')->createClient('s3');
   $adapter = new AwsS3Adapter($s3, 'bucket_name');
   $s4 = $adapter->getClient()->upload(
       'bucket_name',
       $fileName,
       fopen($imageFile->getPathName(), 'rb'),
       'authenticated-read',
       ['params' =>
          [
             'ContentType' => $imageFile->getClientMimeType()
          ]
       ]
   );

任何人帮我使用putObject

  $s3 = s3->putObject(array(
  'Bucket'     => 'bucket-name',
  'Key'        => $fileName,
  'sourceFile' => fopen($imageFile, 'rb'),
  'contentLength' => $imageFile->getClientSize(),
  'ACL'   => 'authenticated-read'
  ));