将本地文件夹中的图像上传到S3

时间:2018-05-28 23:00:00

标签: node.js amazon-s3 file-upload binaryfiles

在我的应用程序中,我将图像上传到本地/ tmp文件夹并进行一些转换。图片在那里妥善保存。之后我想将这些图像上传到S3存储桶,但到目前为止我只设法生成空白图片。

这是我的代码:

//Pick the local image and make it binary
var fs = require('fs');
var bufferedData = '';
fs.readFile(imagePath, function (err, data) {
   if (err) { throw err; }
   bufferedData = new Buffer(data, 'binary');
}


//Send data to s3    
const uploadToS3 = async (idKey: string, modifiers: string, bufferedData) => {
  try {
    return await S3.upload({
      Bucket: 'mirage-thumbnails',
      Key: `${process.env.APP_ENV}/${idKey}/${modifiers}`,
      Body: bufferedData,
      ContentType: 'image/png',
      ACL: 'public-read',
      CacheControl: 'max-age=0',
    }).promise();
  } catch (e) {
    console.error(e.message, e);
  }
};

1 个答案:

答案 0 :(得分:0)

readFile是异步的,您需要等到它完成才能将其上传到S3。但是,您可以提供readable streamreadFile,而不是使用s3.upload,这将允许您上传大文件而不会耗尽内存,并使代码更容易。

S3.upload({
    Bucket: 'mirage-thumbnails',
    Key: `${process.env.APP_ENV}/${idKey}/${modifiers}`,
    Body: fs.createReadStream(imagePath),
    ContentType: 'image/png',
    ACL: 'public-read',
    CacheControl: 'max-age=0',
}).promise();

在您的代码中,bufferedData在调用uploadToS3时未填充。您应该等到文件被读取,然后调用uploadToS3。代码应如下所示:

const fs = require('fs');
const promisify = require('util').promisify;

// Promisify readFile, to make code cleaner and easier.
const readFile = promisify(fs.readFile);

const uploadToS3 = async(idKey, modifiers, data) => {
  return S3.upload({
    Bucket: 'mirage-thumbnails',
    Key: `${process.env.APP_ENV}/${idKey}/${modifiers}`,
    Body: data,
    ContentType: 'image/png',
    ACL: 'public-read',
    CacheControl: 'max-age=0',
  }).promise();
};

const uploadImage = async(path) => {
  const data = await readFile(imagePath);
  // Wait until the file is read
  return uploadToS3('key', 'modifier', data);
};

uploadImage('./some/path/image.png')
  .then(() => console.log('uploaded!'))
  .catch(err => console.error(err));

使用流,只需将uploadImage更改为:

const uploadImage = async(path) => {
      const stream = fs.createReadStream(path);
      return uploadToS3('key', 'modifier', stream);
};