

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

# 設定對 Amazon S3 儲存貯體的存取權
<a name="postgresql-s3-export-access-bucket"></a>

如果要將資料匯出至 Amazon S3，請為 PostgreSQL DB 執行個體提供許可，以便存取用來放置檔案的 Amazon S3 儲存貯體。

若要執行此操作，請使用下列程序。

**透過 IAM 角色授與 PostgreSQL 資料庫執行個體的 Amazon S3 存取權**

1. 建立 IAM 政策。

   此政策可提供儲存貯體及物件許可，讓 PostgreSQL 資料庫執行個體能夠存取 Amazon S3。

   在建立此原則的過程中，請採取下列步驟：

   1. 在政策中納入下列必要動作，以允許從 PostgreSQL 資料庫叢集執行個體傳輸檔案至 Amazon S3 儲存貯體：
      + `s3:PutObject`
      + `s3:AbortMultipartUpload`

   1. 包含用於識別 Amazon S3 儲存貯體和物件的 Amazon Resource Name (ARN)。存取 Amazon S3 的 ARN 格式為︰`arn:aws:s3:::amzn-s3-demo-bucket/*`

   如需如何建立 Amazon RDS for PostgreSQL IAM 政策的詳細資訊，請參閱[建立並使用 IAM 政策進行 IAM 資料庫存取](UsingWithRDS.IAMDBAuth.IAMPolicy.md)。另請參閱《*IAM 使用者指南*》中的[教學：建立和連接您的第一個客戶受管原則](https://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_managed-policies.html)。

   下列 AWS CLI 命令會使用這些選項建立名為 `rds-s3-export-policy`的 IAM 政策。其將存取權授予名為 *amzn-s3-demo-bucket* 的儲存貯體。
**警告**  
建議您在設有可存取特定儲存貯體之端點原則的私有 VPC 中設定資料庫。如需詳細資訊，請參閱《Amazon VPC 使用者指南》中的[對 Amazon S3 使用端點政策](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-endpoints-s3.html#vpc-endpoints-policies-s3)。  
強烈建議您不要建立具有全資源存取權的政策。這個存取權可能會對資料安全構成威脅。如果您建立的原則能為 `S3:PutObject` 提供使用 `"Resource":"*"` 存取所有資源的存取權，則具有匯出許可的使用者可將資料匯出至您帳戶中的所有儲存貯體。此外，使用者可以將資料匯出至*您 AWS 區域內可公開寫入的所有儲存貯體*。

   政策建立後，請記下政策的 Amazon Resource Name (ARN)。在後續步驟中將政策附加至 IAM 角色時，您會需要此 ARN。

   ```
   aws iam create-policy  --policy-name rds-s3-export-policy  --policy-document '{
        "Version": "2012-10-17",		 	 	 
        "Statement": [
          {
            "Sid": "s3export",
            "Action": [
              "s3:PutObject*",
              "s3:ListBucket",
              "s3:GetObject*",
              "s3:DeleteObject*",
              "s3:GetBucketLocation",
              "s3:AbortMultipartUpload"
            ],
            "Effect": "Allow",
            "Resource": [
              "arn:aws:s3:::amzn-s3-demo-bucket/*"
            ] 
          }
        ] 
      }'
   ```

1. 建立 IAM 角色。

   您會執行此動作，使得 Amazon RDS 可以代表您擔任此 IAM 角色，以存取您的 Amazon S3 儲存貯體。如需更多詳細資訊，請參閱《*IAM 使用者指南*》中的[建立角色以將許可委派給 IAM 使用者](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user.html)。

   建議您在資源型政策中使用 `[aws:SourceArn](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-sourcearn)` 和 `[aws:SourceAccount](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-sourceaccount)` 全域條件內容金鑰，將服務的許可限定於特定資源。這是防止[混淆代理人問題](https://docs.aws.amazon.com/IAM/latest/UserGuide/confused-deputy.html)最有效的方式。

   如果同時使用這兩個全域條件內容索引鍵，且 `aws:SourceArn` 值包含帳戶 ID，則在相同政策陳述式中使用 `aws:SourceAccount` 值和 `aws:SourceArn` 值中的帳戶時，必須使用相同的帳戶 ID。
   + 如果您想要跨服務存取單一資源，請使用 `aws:SourceArn`。
   + 如果您想要允許該帳戶中的任何資源與跨服務使用相關聯，請使用 `aws:SourceAccount`。

    在政策中，請務必搭配資源的完整 ARN 來使用 `aws:SourceArn` 全域條件內容金鑰。下列範例示範如何使用 AWS CLI 命令來建立名為 的角色`rds-s3-export-role`。  
**Example**  

   針對 Linux、macOS 或 Unix：

   ```
   aws iam create-role  \
       --role-name rds-s3-export-role  \
       --assume-role-policy-document '{
        "Version": "2012-10-17",		 	 	 
        "Statement": [
          {
            "Effect": "Allow",
            "Principal": {
               "Service": "rds.amazonaws.com"
             },
            "Action": "sts:AssumeRole",
            "Condition": {
                "StringEquals": {
                   "aws:SourceAccount": "111122223333",
                   "aws:SourceArn": "arn:aws:rds:us-east-1:111122223333:db:dbname"
                   }
                }
          }
        ] 
      }'
   ```

   在 Windows 中：

   ```
   aws iam create-role  ^
       --role-name rds-s3-export-role  ^
       --assume-role-policy-document '{
        "Version": "2012-10-17",		 	 	 
        "Statement": [
          {
            "Effect": "Allow",
            "Principal": {
               "Service": "rds.amazonaws.com"
             },
            "Action": "sts:AssumeRole",
            "Condition": {
                "StringEquals": {
                   "aws:SourceAccount": "111122223333",
                   "aws:SourceArn": "arn:aws:rds:us-east-1:111122223333:db:dbname"
                   }
                }
          }
        ] 
      }'
   ```

1. 將您建立的 IAM 政策附加至您建立的 IAM 角色。

   下列 AWS CLI 命令會將先前建立的政策連接至名為 `rds-s3-export-role.` Replace 的角色，`your-policy-arn`取代為您在先前步驟中記下的政策 ARN。

   ```
   aws iam attach-role-policy  --policy-arn your-policy-arn  --role-name rds-s3-export-role  
   ```

1. 將 IAM 角色新增至資料庫執行個體。您可以使用 AWS 管理主控台 或 來執行此操作 AWS CLI，如下所述。

## 主控台
<a name="collapsible-section-1"></a>

**使用主控台為 PostgreSQL 資料庫執行個體新增 IAM 角色**

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

1. 選擇 PostgreSQL 資料庫執行個體名稱以顯示其詳細資訊。

1. 在 **Connectivity & security (連線能力與安全性)** 標籤上的 **Manage IAM roles (管理 IAM 角色)** 區段中，選擇要在 **Add IAM roles to this instance (新增 IAM 角色到此執行個體)** 下新增的角色。

1. 請在 **Feature** (功能) 下，選擇 **s3Export**。

1. 選擇 **Add role (新增角色)**。

## AWS CLI
<a name="collapsible-section-2"></a>

**使用 CLI 為 PostgreSQL 資料庫執行個體新增 IAM 角色**
+ 使用下列命令將角色新增至名為 `my-db-instance` 的 PostgreSQL 資料庫執行個體。將 *`your-role-arn`* 替換為您前個步驟記下的角色 ARN。使用 `s3Export` 作為 `--feature-name` 選項的值。  
**Example**  

  針對 Linux、macOS 或 Unix：

  ```
  aws rds add-role-to-db-instance \
     --db-instance-identifier my-db-instance \
     --feature-name s3Export \
     --role-arn your-role-arn   \
     --region your-region
  ```

  在 Windows 中：

  ```
  aws rds add-role-to-db-instance ^
     --db-instance-identifier my-db-instance ^
     --feature-name s3Export ^
     --role-arn your-role-arn ^
     --region your-region
  ```