Хранение и просмотр изображения Base64 в S3 - PullRequest
2 голосов
/ 18 марта 2019

Я сохраняю изображение на 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...

ИЛИ

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;

}

1 Ответ

0 голосов
/ 18 марта 2019

Можешь попробовать вот так. Надеюсь, это поможет

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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...