'how to enable basic auth in aws s3 without using cloudfront?

I have a S3 bucket and here I have some Image files. any one who have download link in the world can download that files.

But, I want to restrict it using basic username and password authentication. without using cloudfront way. is any alternative ways available in aws? to enable basic auth before download.



Solution 1:[1]

I'm using S3 Bucket Policy with custom header like this:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "getwithauthen",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::bucket/*.txt",
            "Condition": {
                "StringEquals": {
                    "aws:UserAgent": "username",
                    "aws:Referer": "password"
                }
            }
        },
        {
            "Sid": "allowgetwithoutauthen",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::bucket/*.pem"
        }
    ]
}

And then I get the content of txt files by using curl command:

curl --user-agent username --referer password --request GET "https://s3-url"

But I haven't tested with images yet. I think you can use curl -O to get the images

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ash Blake