s3.upload()有效,但上传损坏的图像 [英] s3.upload() works but uploads broken image

查看:115
本文介绍了s3.upload()有效,但上传损坏的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AWS中有一个Lambda函数,该函数与POST方法关联,该方法基本上获取图像URL并将图像上传到S3存储桶.

我的Lambda函数如下所示:

var s3 = new AWS.S3();

exports.handler = (event, context, callback) => {
  let encodedImage = JSON.parse(event.body).user_avatar;
  let decodedImage = Buffer.from(encodedImage, 'base64');
  var filePath = "avatars/" + event.queryStringParameters.username + ".jpg"

  var params = {
    "Body": decodedImage,
    "Bucket": "MYBUCKETNAME.com",
    "Key": filePath
  };
  s3.upload(params, function (err, data) {
    if (err) {
      callback(err, null);
    } else {
      let response = {
        "statusCode": 200,
        "headers": {
          "my_header": "my_value"
        },
        "body": JSON.stringify(data),
        "isBase64Encoded": false
      };
      callback(null, response);
    }
  });
};

似乎将图像上传到指定的存储桶,但是当我查看图像时,它似乎已损坏.

对于AWS提供的GUI之外的任何内容,我都没有经验.如果有人对可能发生的事情有任何想法,我将不胜感激.预先感谢!

解决方案

上载图像文件时,还应指定内容类型.对于jpeg,它是image/jpeg.对于png是image/png.如果您忽略内容类型,则图像可能根本不会显示(只有黑色空间).

内容类型

有一些node.js库可以为您确定图像类型.这只是许多此类库之一:

图像类型

var params = {
  Body: decodedImage,
  Bucket: "MYBUCKETNAME.com",
  Key: filePath,
  Content-Type: 'image/jpeg'
};

I've got a Lambda function in AWS that is hooked up to a POST method which basically takes an image URL and uploads the image to an S3 bucket.

My Lambda function looks like this:

var s3 = new AWS.S3();

exports.handler = (event, context, callback) => {
  let encodedImage = JSON.parse(event.body).user_avatar;
  let decodedImage = Buffer.from(encodedImage, 'base64');
  var filePath = "avatars/" + event.queryStringParameters.username + ".jpg"

  var params = {
    "Body": decodedImage,
    "Bucket": "MYBUCKETNAME.com",
    "Key": filePath
  };
  s3.upload(params, function (err, data) {
    if (err) {
      callback(err, null);
    } else {
      let response = {
        "statusCode": 200,
        "headers": {
          "my_header": "my_value"
        },
        "body": JSON.stringify(data),
        "isBase64Encoded": false
      };
      callback(null, response);
    }
  });
};

It seems to upload the image to the bucket specified, but when I view the image it seems to be broken.

I'm very inexperienced with using AWS for anything outside of the GUI they provide. If anyone has any idea of what might be going on I'd appreciate any help. Thanks in advance!

解决方案

When uploading an image file you should also specify the content type. For jpeg it is image/jpeg. For png it is image/png. If you leave out content type the image might not display at all (just black space).

Content-Type

There are node.js libraries to figure out the image type for you. This is just one of many such libraries:

image-type

var params = {
  Body: decodedImage,
  Bucket: "MYBUCKETNAME.com",
  Key: filePath,
  Content-Type: 'image/jpeg'
};

这篇关于s3.upload()有效,但上传损坏的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆