javascript - Check if file exists on Amazon S3 - Stack Overflow

So, what I'm trying to do is to check with Javascript if an image exists on my Amazon S3.I'm

So, what I'm trying to do is to check with Javascript if an image exists on my Amazon S3.

I'm able to do this with the typical way of preloading the image and use onload and onerror events to check if the image is there.

var img = new Image;
img.src = imageUrl;
img.onerror = function()....
img.onload = function()...

What I'm trying to achieve now is the same but without fully download the image. Let's say I have a possible 3mb image. If I check with the way I'm doing it now, if the image doesn't exist it'll go into the onerror event, I'll do a call to my server to generate the image and then I'll check again. When the image exists, it'll download the 3mb image and it'll go into the onload event.

If the image doesn't exist, Amazon returns a 403 Forbidden status code. If it does exist, it returns a 200 Ok one.

My question is:

Is there any way to just check the status code or any other way without fully download the image?

Thanks!!

So, what I'm trying to do is to check with Javascript if an image exists on my Amazon S3.

I'm able to do this with the typical way of preloading the image and use onload and onerror events to check if the image is there.

var img = new Image;
img.src = imageUrl;
img.onerror = function()....
img.onload = function()...

What I'm trying to achieve now is the same but without fully download the image. Let's say I have a possible 3mb image. If I check with the way I'm doing it now, if the image doesn't exist it'll go into the onerror event, I'll do a call to my server to generate the image and then I'll check again. When the image exists, it'll download the 3mb image and it'll go into the onload event.

If the image doesn't exist, Amazon returns a 403 Forbidden status code. If it does exist, it returns a 200 Ok one.

My question is:

Is there any way to just check the status code or any other way without fully download the image?

Thanks!!

Share Improve this question asked Feb 22, 2018 at 3:22 JV LoboJV Lobo 6,3469 gold badges41 silver badges58 bronze badges 3
  • as per this answer, perhaps request headObject – Varinder Commented Feb 22, 2018 at 3:29
  • thanks for your answer @Varinder but I'm using just regular Javascript without any library – JV Lobo Commented Feb 22, 2018 at 3:31
  • You can use REST API for headObject – long.luc Commented Feb 22, 2018 at 3:55
Add a ment  | 

1 Answer 1

Reset to default 4

Try doing a HEAD request as shown in this answer

https://stackoverflow./a/333657/209067

A copy of the code from the answer linked above:

function UrlExists(url, callback)
{
    var http = new XMLHttpRequest();
    http.open('HEAD', url);
    http.onreadystatechange = function() {
        if (this.readyState == this.DONE) {
            callback(this.status != 404);
        }
    };
    http.send();
}

If your case you will need to check for 403 instead of 404

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745423531a4627073.html

相关推荐

  • javascript - Check if file exists on Amazon S3 - Stack Overflow

    So, what I'm trying to do is to check with Javascript if an image exists on my Amazon S3.I'm

    15小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信