---
# System prepended metadata

title: Publish S3 bucket
tags: [AWS]

---

###### tags: `AWS`
# Publish S3 bucket

https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteAccessPermissionsReqd.html#block-public-access-static-site

1. 關閉 Block public access (bucket settings)

2. 編輯 Bucket policy
```
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::Bucket_name/*"
        }
    ]
}
```
<font color="#f00">Bucket_Name</font> is a placeholder for the bucket nam

Laravel 對應修改
```php=
function asset_url($url = null)
{
    if (env('APP_ENV') == 'production') {
        return preg_replace('/^storage/', env('AWS_S3_PATH', ''), $url);
    } else {
        return $url ? asset($url) : $url;
    }
}
```
需在 .env 中填入正確的 S3 URL 前綴。

Bucket permission 中 CORS 的設定(用不到)
```
[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET"
        ],
        "AllowedOrigins": [
            "https://www.my-domain.com"
        ],
        "ExposeHeaders": [],
        "MaxAgeSeconds": 3000
    }
]
```

