【发布时间】:2019-08-08 11:16:44
【问题描述】:
我正在将图像存储到 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
当我下载文件并在文本编辑器中打开时,我会看到,具体取决于我存储的内容。两种方法都试了,都看不到图片:
/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;
}
【问题讨论】:
-
能否请您写下图片上传代码,以便我们找出问题所在,因为当您直接从 url 打开图片时,您无法看到图片,所以我猜图片有问题从 base64 转换图像。
-
@YashRami 等一下。
标签: angular typescript amazon-web-services amazon-s3 angular6