Я сохраняю изображение на 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](https://i.stack.imgur.com/flirT.jpg)
Когда я загружаю файл и открываю в текстовом редакторе, я вижу, в зависимости отна что я храню.Я пробовал оба пути, и я не вижу изображение:
/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;
}