gpt4 book ai didi

angular - 在 S3 中存储和查看 Base64 图像

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:19 25 4
gpt4 key购买 nike

我正在将图像存储到 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);
}

上传服务.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;

最佳答案

你能不能这样试试。希望对你有帮助

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);
}

关于angular - 在 S3 中存储和查看 Base64 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55215411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com