Home > database >  how to enable basic auth in aws s3 without using cloudfront?
how to enable basic auth in aws s3 without using cloudfront?

Time:03-25

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.

CodePudding user response:

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

  • Related