Make objects public
Turn off Block public access on a bucket and add a bucket policy that lets anyone read its objects without a key.
Every bucket starts with Block public access on: nothing in it can be read without a key, and no policy can change that. Making objects public takes two steps: allow public access, then add a policy that grants the public what it may do. Replace example-site with your bucket's name.
Warning
Anyone on the internet can then read what the policy grants.
Prerequisites
- Permission for
s3:PutBucketPublicAccessBlockands3:PutBucketPolicyon the bucket. Only your own organization can change them; a bucket policy cannot grant them to another. - In the console, also
s3:GetBucketPolicy: without it the policy editor is not shown ("Reading the policy needs s3:GetBucketPolicy on this bucket."). - The bucket is not one the platform writes.
Step 1: Allow public access
- Open Object Storage, open the bucket, then choose Public access.
- Under Block public access, choose Allow public access….
- Enter the bucket's name to confirm, then choose Allow public access.
The console says "Public access is allowed. A policy decides what is published."
Pass all four flags with --no-: dcloud s3 bucket-access set-block example-site --no-block-public-acls --no-ignore-public-acls --no-block-public-policy --no-restrict-public-buckets.
PUT /s3/api/buckets/example-site/public-access-block
{ "blockPublicAcls": false, "ignorePublicAcls": false, "blockPublicPolicy": false, "restrictPublicBuckets": false }resource "dcloud_bucket_public_access_block" "site" {
bucket = dcloud_bucket.site.id
block_public_acls = true
ignore_public_acls = true
block_public_policy = false
restrict_public_buckets = false
}Over S3, aws s3api delete-public-access-block --bucket example-site turns all four flags off.
Step 2: Add a public-read policy
- Under Bucket policy, choose Public-read template. It fills in the policy below; it does not save it.
- Choose Save policy.
The bucket list then marks the bucket public.
dcloud s3 bucket-access set-policy example-site --policy '{"Version":"2012-10-17","Statement":[{"Sid":"PublicRead","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::example-site/*"}]}'PUT /s3/api/buckets/example-site/policy
{ "policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"PublicRead\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::example-site/*\"}]}" }resource "dcloud_bucket_policy" "site" {
bucket = dcloud_bucket.site.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Sid = "PublicRead"
Effect = "Allow"
Principal = "*"
Action = "s3:GetObject"
Resource = "arn:aws:s3:::${dcloud_bucket.site.id}/*"
}]
})
depends_on = [dcloud_bucket_public_access_block.site]
}The policy is the console's template:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicRead",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-site/*"
}
]
}Over S3: aws s3api put-bucket-policy --bucket example-site --policy file://policy.json.
Result
Anyone can fetch https://example-site.s3.<domain>/<key> without signing. Turning Block public
access back on (the console's Block public access button, all four flags true) closes it
at once, whatever the policy says.
A grant held to source addresses no wider than a /8 (IPv4) or a /32 (IPv6) does not count as public, so it is
accepted while Block public access is on. The policy grammar is in
Access control.
If it is refused
- 403
This policy would make the bucket public, and Block Public Access is on for it. Turn off "Block public policies" first if you really mean to publish its objects. - 400 when the policy is longer than 20 KB,
The policy is not valid JSON, or the statement that breaks the grammar. - 403
This bucket is managed by the platform; its access cannot be changed. In the console the buttons are unavailable and say "The platform manages this bucket; its access cannot be changed." - 403
Forbiddenwithout the permission; in the console the button saysNeeds s3:PutBucketPublicAccessBlock on this bucket.orNeeds s3:PutBucketPolicy on this bucket. - Uploads with a public ACL (
--acl public-read,x-amz-acl: public-read) are refused withAccessControlListNotSupported"The bucket does not allow ACLs. Make objects public with a bucket policy instead.": a bucket policy is the only way to publish objects.