AWS S3损坏的映像

时间:2019-09-10 12:24:23

标签: javascript node.js typescript amazon-s3 aws-sdk

我需要从S3创建较小版本的图像。当我通过s3.getObject调用数据存储时,我收到损坏的图像文件,但是在S3中将其正确保存。 corrupted image

我尝试使用s3.getObject并尽可能设置await。另外,我保存接收图像的局部性,以便在对其进行锐利修改之前对其进行检查。这是在S3和我的应用之间发送图像的问题。

export const s3Config: S3.ClientConfiguration = {
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: process.env.AWS_DEFAULT_REGION,
};

async downloader(url: string)
  {
    const pathParts: string[] = url.split('/');
    const imageParts: string[] = pathParts[5].split('.');
    const path = pathParts[4].concat('/',pathParts[5]);
    const name: string = imageParts[0];
    const nameSmall = name + '_small.' + imageParts[imageParts.length -1];
    const test = name + '_test.' + imageParts[imageParts.length -1];
    this.logger.info(`Image path: ${path}`);
    AWS.config.update(s3Config);
    const s3 = new AWS.S3();
    await s3.getObject(
      {
        Bucket: mediaBucket, Key:path
      },
      function (error, data) {
        if (error != null) {
          console.log("Failed to retrieve an object: " + error);
        } else {
          console.log("Loaded " + data.ContentLength + " bytes");
        }
      }
    )
    .promise()
    .then(async data =>{
      await fs.createWriteStream('./images/'+test).write(data.Body as any);
      // tslint:disable-next-line
    await sharp(data.Body as any).resize(100).toFile('./images/'+nameSmall, (err,info)=> {
      this.logger.info('err: ', err);
      this.logger.info('info: ', info);
    });
  })
  }

我希望有权以正确的格式接收图片旅馆,但实际输出是损坏的。

0 个答案:

没有答案