# 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:PutBucketPublicAccessBlock` and `s3:PutBucketPolicy` on 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

**Console**
1. Open **Object Storage**, open the bucket, then choose **Public access**.
2. Under **Block public access**, choose **Allow public access…**.
3. 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."

**CLI**
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`.

**API**
```http
PUT /s3/api/buckets/example-site/public-access-block
{ "blockPublicAcls": false, "ignorePublicAcls": false, "blockPublicPolicy": false, "restrictPublicBuckets": false }
```

**Terraform**
```hcl
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

**Console**
1. Under **Bucket policy**, choose **Public-read template**. It fills in the policy below; it does
   not save it.
2. Choose **Save policy**.

The bucket list then marks the bucket **public**.

**CLI**
```sh
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/*"}]}'
```

**API**
```http
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/*\"}]}" }
```

**Terraform**
```hcl
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:

```json
{
  "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](access-control.md#bucket-policies).

## 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 `Forbidden` without the permission; in the console the button says
  `Needs s3:PutBucketPublicAccessBlock on this bucket.` or
  `Needs s3:PutBucketPolicy on this bucket.`
- Uploads with a public ACL (`--acl public-read`, `x-amz-acl: public-read`) are refused with
  `AccessControlListNotSupported` "The bucket does not allow ACLs. Make objects public with a
  bucket policy instead.": a bucket policy is the only way to publish objects.

## Related

- [Host a static website](host-a-static-website.md)
- [Share a bucket with another organization](share-a-bucket.md)
- `s3.bucket-access.set-policy` · `dcloud s3 bucket-access set-policy` · `dcloud_bucket_policy` · `dcloud_bucket_public_access_block`
