在S3中存储和查看Base64映像

时间:2019-03-18 06:06:47

标签: angular typescript amazon-web-services amazon-s3 angular6

我正在将图像存储到S3中,如下所示:

const params = {
  Bucket: "xyz-bucket",
  Key: this.folder + this.filename,
  Body: file,
  ACL: "public-read",
  ContentEncoding: 'base64',
  ContentType: 'image/jpg'
};

它成功存储在S3中为:https://s3.eu-west-2.amazonaws.com/xyz-bucket/1552861956582_0djO8.jpg

但是当我尝试查看它时,我看到: image when viewed directly using s3 url on browser

当我下载文件并在文本编辑器中打开时,我看到的取决于我存储的内容。我尝试了两种方式,但看不到图片:

/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAA...

OR

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0...

现在,问题是我在哪里错了?我保存文件的方式或我尝试查看文件的方式。

已更新

openGallery() {
const options: CameraOptions = {
  quality: 50,
  destinationType: this.camera.DestinationType.DATA_URI,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE,
  correctOrientation: true,
  sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
};
this.camera
  .getPicture(options)
  .then(data_uri => {
    this.upload(data_uri);
  }, err => console.log(err));
}

upload(file: any) {
  this.uploadService.uploadfile(file);
}

upload.service.ts

uploadfile(file) {
const bucket = new S3({
  accessKeyId: "****",
  secretAccessKey: "*******",
  region: "***"
});

var epoch_timestamp = new Date().getTime();
this.filename = epoch_timestamp + "_" + this.randomStringGenerator() + ".jpg";
const params = {
  Bucket: "xyz-bucket",
  Key: this.folder + this.filename,
  Body: file,
  ACL: "public-read",
  ContentEncoding: 'base64',
  ContentType: 'image/jpg'
};

bucket.upload(params, function(err, data) {
  if (err) {
    console.log("There was an error uploading your file: ", err);
    return false;
  }

  console.log("Successfully uploaded file.", data);
  return true;
});

randomStringGenerator(length = 5) {
var text = "";
var possible =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for (var i = 0; i < length; i++)
  text += possible.charAt(Math.floor(Math.random() * possible.length));

return text;

}

2 个答案:

答案 0 :(得分:0)

您可以这样尝试吗?希望对您有帮助

openGallery() {
const options: CameraOptions = {
 quality: 50,
 destinationType: this.camera.DestinationType.FILE_URI,
 encodingType: this.camera.EncodingType.JPEG,
 mediaType: this.camera.MediaType.PICTURE,
 correctOrientation: true,
 sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
};
this.camera
 .getPicture(options)
 .then(imageData=> {
   let base64Image = 'data:image/jpeg;base64,' + imageData; //mimetype should be change based on image type
   this.upload(base64Image);
 }, err => console.log(err));
}

upload(file: any) {
  this.uploadService.uploadfile(file);
}

答案 1 :(得分:0)

我设法解决了在请求正文中使用二进制类型的问题:

curl --location --request PUT 'https://api_id.execute-api.eu-west-1.amazonaws.com/stage/uploadpicture' \
--header 'Content-Type: image/jpeg' \
--data-binary '@a.jpg'

此问题已通过使用上述命令而不是:

得到解决。
curl --location --request PUT 'https://api_id.execute-api.eu-west-1.amazonaws.com/stage/uploadpicture' \
--header 'Content-Type: image/jpeg' \
--form 'image=@a.jpg'

数据二进制(--data-binary参数)代替了表单(--form)成功了。