

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立授權
<a name="access-grants-grant-create"></a>

S3 Access Grants 執行個體中的個別存取*授權*允許企業目錄中的特定身分 AWS Identity and Access Management (IAM) 委託人或使用者或群組，在您的 S3 Access Grants 執行個體中註冊的位置內取得存取權。位置會將儲存貯體或字首映射至 IAM 角色。S3 存取授權會擔任此 IAM 角色，將臨時憑證提供給承授者。

在 S3 存取授權執行個體中[至少註冊一個位置](https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-grants-location.html)之後，您可以建立存取授權。

承授者可以是 IAM 使用者或角色，也可以是目錄使用者或群組。目錄使用者是[與您的 S3 存取授權執行個體相關聯](https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-grants-instance-idc.html)之公司目錄或外部身分來源的使用者。如需詳細資訊，請參閱[S3 Access Grants 和公司目錄身分](access-grants-directory-ids.md)。若要從 IAM Identity Center 為特定目錄使用者或群組建立授權，請在 IAM Identity Center 尋找 IAM Identity Center 用來識別該使用者的 GUID，例如 `a1b2c3d4-5678-90ab-cdef-EXAMPLE11111`。如需如何使用 IAM Identity Center 檢視使用者資訊的詳細資訊，請參閱《AWS IAM Identity Center 使用者指南》**中的[檢視使用者和群組指派](https://docs.aws.amazon.com/singlesignon/latest/userguide/get-started-view-assignments.html)。

您可以授予儲存貯體、字首或物件的存取權。Amazon S3 中的字首是物件索引鍵名稱開頭的一串字元，用於組織儲存貯體中的物件。這可以是任何允許的字元字串，例如儲存貯體中以 `engineering/` 字首開頭的物件金鑰名稱。

## 子字首
<a name="subprefix"></a>

授予註冊位置的存取權時，您可以使用 `Subprefix` 欄位，將存取範圍縮小為位置範圍的一部分。如果您選擇授權的註冊位置是預設 S3 路徑 (`s3://`)，您必須縮小授權範圍。您無法建立預設位置 (`s3://`) 的存取授權來讓承授者存取 AWS 區域中的所有儲存貯體。反之，您必須將授權範圍縮小為下列其中一項：
+ 儲存貯體：`s3://bucket/*`
+ 儲存貯體內的字首：`s3://bucket/prefix*`
+ 字首內的字首：`s3://bucket/prefixA/prefixB*`
+ 物件：`s3://bucket/object-key-name`

如果您要建立註冊位置為儲存貯體的存取授權，您可以在 `Subprefix` 欄位中傳遞下列其中一項來縮小授權範圍：
+ 儲存貯體內的字首：`prefix*`
+ 字首內的字首：`prefixA/prefixB*`
+ 物件：`/object-key-name`

建立授予之後，Amazon S3 主控台中顯示的授予範圍或 API 或 AWS Command Line Interface (AWS CLI) 回應中傳回`GrantScope`的 ，是將位置路徑與 串連的結果`Subprefix`。請確定此串連路徑正確映射至您要授予存取權的 S3 儲存貯體、字首或物件。

**注意**  
如果您需要建立存取授權，僅授予一個物件的存取權，您必須將授權類型指定為一個物件。若要在 API 呼叫或 CLI 命令中執行這項操作，請傳遞 `s3PrefixType` 參數並提供值 `Object`。在 Amazon S3 主控台中，當您建立授權時，請在選取位置之後，於 **授權範圍** 下，選取 **授權範圍是一個物件** 核取方塊。
如果儲存貯體不存在，則您無法建立該儲存貯體的授權。不過，您可以為尚未存在的字首建立授權。
如需可在 S3 存取授權執行個體中建立的授權數目上限，請參閱[S3 Access Grants 的限制](access-grants-limitations.md)。

您可以使用 Amazon S3 主控台 AWS CLI、Amazon S3 REST API 和 AWS SDKs 來建立存取授權。

## 使用 S3 主控台
<a name="access-grants-grant-create-console"></a>

**建立存取授權**

1. 登入 AWS 管理主控台 ，並在 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/)：// 開啟 Amazon S3 主控台。

1. 在左側導覽窗格中，選擇 **Access Grants**。

1. 在 **S3 Access Grants** 頁面上，選擇包含您要使用之 S3 Access Grants 執行個體的區域。

   如果您是第一次使用 S3 Access Grants 執行個體，請確定您已完成[步驟 2 - 註冊位置](https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-grants-location.html)，並瀏覽至**設定 Access Grants 執行個體**精靈的**步驟 3**。如果您已有 S3 Access Grants 執行個體，請選擇**檢視詳細資訊**，然後從**授權**索引標籤選擇**建立授權**。

   1. 在**授權範圍**區段中，選取或輸入註冊位置。

      如果您選取預設的 `s3://` 位置，請使用**子字首**方塊縮小存取授權的範圍。如需詳細資訊，請參閱[子字首](https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-grants-grant.html#subprefix)。如果您只授予一個物件的存取權，請選取**授權範圍是物件**。

   1. 在**權限和存取**底下，選取**許可**層級**讀取**或**寫入**，或兩者都選取。

      然後選擇**承授者類型**。如果您已將公司目錄新增至 IAM Identity Center，並將此 IAM Identity Center 執行個體與 S3 Access Grants 執行個體建立關聯，則可以選擇 **IAM Identity Center 的目錄身分**。如果您選擇此選項，請從 IAM Identity Center 取得使用者或群組的 ID，然後在此區段中輸入。

      如果**承授者類型**是 IAM 使用者或角色，請選擇 **IAM 主體**。在 **IAM 主體類型**下，選擇**使用者**或**角色**。然後，在 **IAM 主體使用者**下，從清單中選擇或輸入身分 ID。

   1. 若要建立 S3 Access Grants 授權，請選擇**下一步**或**建立授權**。

1. 如果**下一步**或**建立授權**為停用狀態：

**無法建立授權**
   + 您可能需要先在 S3 Access Grants 執行個體中[註冊位置](https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-grants-location.html)。
   + 您可能沒有建立存取授權的 `s3:CreateAccessGrant` 許可。請聯絡您的帳戶管理員。

## 使用 AWS CLI
<a name="access-grants-grant-create-cli"></a>

若要安裝 AWS CLI，請參閱*AWS Command Line Interface 《 使用者指南*》中的[安裝 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) 。

下列範例說明如何建立 IAM 主體的存取授權請求，以及如何為公司目錄使用者或群組建立存取授權請求。

若要使用下列範例命令，請將 `user input placeholders` 取代為您自己的資訊。

**注意**  
如果您要建立的存取授權僅授予一個物件的存取權，請包含必要的參數 `--s3-prefix-type Object`。

**Example 建立 IAM 主體的存取授權請求**  

```
aws s3control create-access-grant \
--account-id 111122223333 \
--access-grants-location-id a1b2c3d4-5678-90ab-cdef-EXAMPLE22222 \
--access-grants-location-configuration S3SubPrefix=prefixB* \
--permission READ \
--grantee GranteeType=IAM,GranteeIdentifier=arn:aws:iam::123456789012:user/data-consumer-3
```

**Example 建立存取授權回應**  

```
{"CreatedAt": "2023-05-31T18:41:34.663000+00:00",
    "AccessGrantId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
    "AccessGrantArn": "arn:aws:s3:us-east-2:111122223333:access-grants/default/grant/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
    "Grantee": {
        "GranteeType": "IAM",
        "GranteeIdentifier": "arn:aws:iam::111122223333:user/data-consumer-3"
    },
    "AccessGrantsLocationId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE22222",
    "AccessGrantsLocationConfiguration": {
        "S3SubPrefix": "prefixB*"
    },
    "GrantScope": "s3://amzn-s3-demo-bucket/prefix*",
    "Permission": "READ"
}
```

**建立目錄使用者或群組的存取授權請求**  
若要建立目錄使用者或群組的存取授權請求，您必須先執行下列其中一個命令，取得目錄使用者或群組的 GUID。

**Example 取得目錄使用者或群組的 GUID**  
您可以透過 IAM Identity Center 主控台或使用 AWS CLI AWS SDKs 來尋找 IAM Identity Center 使用者的 GUID。下列命令會列出所指定 IAM Identity Center 執行個體中的使用者，包括其名稱和識別符。  

```
aws identitystore list-users --identity-store-id d-1a2b3c4d1234 
```
此命令會列出所指定 IAM Identity Center 執行個體中的群組。  

```
aws identitystore list-groups --identity-store-id d-1a2b3c4d1234
```

**Example 建立目錄使用者或群組的存取授權**  
此命令類似於為 IAM 使用者或角色建立授權，但承授者類型為 `DIRECTORY_USER` 或 `DIRECTORY_GROUP`，且承授者識別符是目錄使用者或群組的 GUID。  

```
aws s3control create-access-grant \
--account-id 123456789012 \
--access-grants-location-id default \
--access-grants-location-configuration S3SubPrefix="amzn-s3-demo-bucket/rafael/*" \
--permission READWRITE \
--grantee GranteeType=DIRECTORY_USER,GranteeIdentifier=83d43802-00b1-7054-db02-f1d683aacba5 \
```

## 使用 REST API
<a name="access-grants-grant-create-rest-api"></a>

如需有關管理存取授權的 Amazon S3 REST API 支援資訊，請參閱《Amazon Simple Storage Service API 參考》**中的下列各節：
+  [CreateAccessGrant](https://docs.aws.amazon.com/AmazonS3/latest/API/API_control_CreateAccessGrant.html) 
+  [DeleteAccessGrant](https://docs.aws.amazon.com/AmazonS3/latest/API/API_control_DeleteAccessGrant.html) 
+  [GetAccessGrant](https://docs.aws.amazon.com/AmazonS3/latest/API/API_control_GetAccessGrant.html) 
+  [ListAccessGrants](https://docs.aws.amazon.com/AmazonS3/latest/API/API_control_ListAccessGrants.html)

## 使用 AWS SDKs
<a name="access-grants-grant-create-using-sdk"></a>

本節提供如何使用 AWS SDK 建立存取授權的範例。

------
#### [ Java ]

若要使用下列範例，請以您自己的資訊取代 `user input placeholders`：

**注意**  
如果您要建立的存取授權僅授予一個物件的存取權，請包含必要的參數 `.s3PrefixType(S3PrefixType.Object)`。

**Example 建立存取授權請求**  

```
public void createAccessGrant() {
CreateAccessGrantRequest createRequest = CreateAccessGrantRequest.builder()
.accountId("111122223333")
.accessGrantsLocationId("a1b2c3d4-5678-90ab-cdef-EXAMPLEaaaaa")
.permission("READ")
.accessGrantsLocationConfiguration(AccessGrantsLocationConfiguration.builder().s3SubPrefix("prefixB*").build())
.grantee(Grantee.builder().granteeType("IAM").granteeIdentifier("arn:aws:iam::111122223333:user/data-consumer-3").build())
.build();
CreateAccessGrantResponse createResponse = s3Control.createAccessGrant(createRequest);
LOGGER.info("CreateAccessGrantResponse: " + createResponse);
}
```

**Example 建立存取授權回應**  

```
CreateAccessGrantResponse(
CreatedAt=2023-06-07T05:20:26.330Z,
AccessGrantId=a1b2c3d4-5678-90ab-cdef-EXAMPLE33333,
AccessGrantArn=arn:aws:s3:us-east-2:444455556666:access-grants/default/grant/a1b2c3d4-5678-90ab-cdef-EXAMPLE33333,
Grantee=Grantee(
GranteeType=IAM,
GranteeIdentifier=arn:aws:iam::111122223333:user/data-consumer-3
),
AccessGrantsLocationId=a1b2c3d4-5678-90ab-cdef-EXAMPLEaaaaa,
AccessGrantsLocationConfiguration=AccessGrantsLocationConfiguration(
S3SubPrefix=prefixB*
),
GrantScope=s3://amzn-s3-demo-bucket/prefixB,
Permission=READ
)
```

------