介紹如何使用 AWS 命令列界面(awscli)的 s3api 指令設定 S3 bucket 與 object 的 ACL,控制資料存取權限。
列出所有 Buckets
若要列出自己所有的 buckets,可以使用 list-buckets 指令:
# 查詢所有 Buckets aws --profile=default \ --endpoint-url=http://s3.example.com \ s3api list-buckets
{
"Buckets": [
{
"Name": "office",
"CreationDate": "2019-10-09T09:00:30.373Z"
},
{
"Name": "guide",
"CreationDate": "2020-06-16T08:59:10.577Z"
}
],
"Owner": {
"DisplayName": "Office_Guide",
"ID": "officeguide"
}
}
如果只需要列出 buckets 的名稱,可以加上 --query "Buckets[].Name" 參數:
# 查詢所有 Buckets(僅列出名稱) aws --profile=default \ --endpoint-url=http://s3.example.com \ s3api list-buckets \ --query "Buckets[].Name"
[
"office",
"guide"
]
取得 Bucket ACL 設定
若要取得指定 Bucket 目前的 ACL 設定值,可以使用 get-bucket-acl 指令。例如查詢 office 這個 bucket 的 ACL 設定:
# 查詢指定 Bucket ACL 設定值 aws --profile=default \ --endpoint-url=http://s3.example.com \ s3api get-bucket-acl \ --bucket office
{
"Owner": {
"DisplayName": "Office_Guide",
"ID": "officeguide"
},
"Grants": [
{
"Grantee": {
"DisplayName": "Office_Guide",
"ID": "officeguide",
"Type": "CanonicalUser"
},
"Permission": "FULL_CONTROL"
}
]
}
更改 Bucket ACL 設定
若要更新指定 Bucket 的 ACL 設定值,可以使用 put-bucket-acl 指令:
# 更改指定 Bucket ACL 設定值 aws --profile=default \ --endpoint-url=http://s3.example.com \ s3api put-bucket-acl \ --bucket office \ --grant-full-control id=officeguide \ --grant-read id=myuser1,id=myuser2
在更改 ACL 的時候,會蓋掉原本的設定,同時需要以 --grant-full-control 授予 bucket 擁有者完整的權限。
在這個例子中,除了設定讓擁有者具有全部的權限之外,同時也使用 --grant-read 授予 myuser1 與 myuser2 兩位使用者讀取權限。
除了上述的兩種權限之外,其餘可用權限還有寫入(--grant-write)、ACL 讀取(--grant-read-acp)與 ACL 寫入(--grant-write-acp),詳細的用法可參考 AWS 的說明文件。
如果想以 JSON 格式來輸入設定值,可以先以 --generate-cli-skeleton 產生 JSON 格式的範例:
# 產生 JSON 輸入格式範例 aws s3api put-bucket-acl --generate-cli-skeleton
{
"ACL": "public-read",
"AccessControlPolicy": {
"Grants": [
{
"Grantee": {
"DisplayName": "",
"EmailAddress": "",
"ID": "",
"Type": "CanonicalUser",
"URI": ""
},
"Permission": "WRITE"
}
],
"Owner": {
"DisplayName": "",
"ID": ""
}
},
"Bucket": "",
"ContentMD5": "",
"GrantFullControl": "",
"GrantRead": "",
"GrantReadACP": "",
"GrantWrite": "",
"GrantWriteACP": ""
}
依照此格式將輸入的 JSON 檔案準備好之後,即可使用 JSON 輸入檔的方式來修改 bucket 的 ACL 設定:
# 以 JSON 輸入方式更改指定 Bucket ACL 設定值 aws --profile=default \ --endpoint-url=http://s3.example.com \ s3api put-bucket-acl \ --cli-input-json input.json
取得 Object ACL 設定
若要取得指定 Object 目前的 ACL 設定值,可以使用 get-object-acl 指令:
# 查詢指定 Object ACL 設定值 aws --profile=default \ --endpoint-url=http://s3.example.com \ s3api get-bucket-acl \ --bucket office \ --key my_file.txt
{
"Owner": {
"DisplayName": "Office_Guide",
"ID": "officeguide"
},
"Grants": [
{
"Grantee": {
"DisplayName": "Office_Guide",
"ID": "officeguide",
"Type": "CanonicalUser"
},
"Permission": "FULL_CONTROL"
}
]
}
更改 Object ACL 設定
若要更新指定 Object 的 ACL 設定值,可以使用 put-object-acl 指令:
# 更改指定 Object ACL 設定值 aws --profile=default \ --endpoint-url=http://s3.example.com \ s3api put-object-acl \ --bucket office \ --key my_file.txt \ --grant-full-control id=officeguide \ --grant-read id=myuser1,id=myuser2
這裡的授權設定方式跟 bucket 非常類似,亦可使用 JSON 的方式來輸入設定值。
參考資料:G. T. Wang

