简体   繁体   English

AWS S3 getObject to image

[英]AWS S3 getObject to image

The 'getObject' request from the AWS S3 SDK returns a 'data.Body' such as 'Uint8Array (51213) [137, 80 ....' 来自AWS S3 SDK的'getObject'请求返回'data.Body',例如'Uint8Array(51213)[137,80 ....'

How to transform this data to display it in an HTML tag 如何转换此数据以在HTML标记中显示它

<img id='imgTest' .....


s3.getObject({
    Bucket: 'mybuket',
    Key: "test/myImage.png"
}, function (errtxt, data) {
    if (errtxt) {
        console.Log("lireImage", "ERR " + errtxt);
    } else {
        console.log('lecture OK')
        let imageTest =document.getElementById('imgTest')
        imageTest.src = ????

Thank you for your answers YC 谢谢你的回答YC

I'm not looking for the URL of the image, because if I want to load this image from its URL, this image MUST BE declared 'public', otherwise there's a 403 (forbidden) error message. 我不是在寻找图像的URL,因为如果我想从其URL加载此图像,则必须将此图像声明为“public”,否则会出现403(禁止)错误消息。

That's why I'm loading the image with an 's3.getObject', with an 's3' declared and authenticated by my access keys. 这就是我用's3.getObject'加载图像的原因,我的访问键声明并验证了's3'。 In this way, we can load an image declared private. 通过这种方式,我们可以加载声明为private的图像。

What I did not know how to do was convert the received data into an image. 我不知道该怎么做是将收到的数据转换成图像。

In one of the links you gave me, I found the solution. 在你给我的一个链接中,我找到了解决方案。

let imageTest =document.getElementById('imgTest')
s3.getObject({
    Bucket: 'myBucket',
    Key: "test/myimage.png"
}, function (errtxt, file) {
    if (errtxt) {
        console.Log("lireFic", "ERR " + errtxt);
    } else {
        console.log('lecture OK')
        imageTest.src = "data:image/png;base64," + encode(file.Body);
    }
});
function encode(data)
{
    var str = data.reduce(function(a,b){ return a+String.fromCharCode(b) },'');
    return btoa(str).replace(/.{76}(?=.)/g,'$&\n');
}

All you need to do is retrieve the url of the image you with to display. 您需要做的就是检索要显示的图像的URL。

See the following answers: 请参阅以下答案:

Amazon S3 Access image by url Amazon S3通过网址访问图片

display images fetched from s3 显示从s3获取的图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM