

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

# 整合 RDS for SQL Server 與 S3 的先決條件
<a name="Appendix.SQLServer.Options.S3-integration.preparing"></a>

開始之前，請尋找或建立您要使用的 S3 儲存貯體。另外，新增許可，以允許 RDS DB 執行個體存取 S3 儲存貯體。若要設定此存取，請建立 IAM 政策和 IAM 角色。

## 主控台
<a name="Appendix.SQLServer.Options.S3-integration.preparing.console"></a>

**建立 IAM 政策以存取 Amazon S3**

1. 在 [IAM 管理主控台](https://console.aws.amazon.com/iam/home?#home)的導覽窗格中，選擇 **Policies (政策)**。

1. 建立新政策，並使用 **Visual editor (視覺化編輯器)** 索引標籤執行下列步驟。

1. 針對 **Service (服務)**，輸入 **S3**，然後選擇 **S3** 服務。

1. 針對 **Actions (動作)**，選擇下列選項，以授予資料庫執行個體所需的存取：
   + `ListAllMyBuckets` – 必要
   + `ListBucket` – 必要
   + `GetBucketAcl` – 必要
   + `GetBucketLocation` – 必要
   + `GetObject` – 將 S3 中的檔案下載至 時需要`D:\S3\`
   + `PutObject` – 將 `D:\S3\` 中的檔案上傳至 S3 時需要
   + `ListMultipartUploadParts` – 將 `D:\S3\` 中的檔案上傳至 S3 時需要
   + `AbortMultipartUpload` – 將 `D:\S3\` 中的檔案上傳至 S3 時需要

1. 針對 **Resources (資源)**，顯示的選項取決於您在上一步選擇的動作。您看到的選項可能是 **bucket (儲存貯體)**、**object (物件)** 或兩者都有。對於每一項，新增適當的 Amazon Resource Name (ARN)。

   針對 **bucket (儲存貯體)**，新增您要使用的儲存貯體的 ARN。例如，如果儲存貯體名為 *amzn-s3-demo-bucket*，請將 ARN 設為 `arn:aws:s3:::amzn-s3-demo-bucket`。

   針對 **object (物件)**，輸入儲存貯體的 ARN，然後選擇下列其中一個：
   + 若要允許存取指定儲存貯體中的所有檔案，對於 **Bucket name (儲存貯體)** 和 **Object name (物件名稱)**，請都選擇 **Any (任何)**。
   + 若要允許存取儲存貯體中的特定檔案或資料夾，請提供您要讓 SQL Server 存取的特定儲存貯體和物件的 ARN。

1. 遵循主控台的指示操作，直到完成建立政策。

   以上為設定政策的簡略指南。如需建立 IAM 政策的詳細指示，請參閱 *IAM 使用者指南*中的[建立 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html)。

**建立 IAM 角色以使用前一個程序中的 IAM 政策**

1. 在 [IAM 管理主控台](https://console.aws.amazon.com/iam/home?#home)的導覽窗格中，選擇 **Roles (角色)**。

1. 建立新的 IAM 角色，並選擇以下出現在主控台的選項：
   + **AWS服務**
   + **RDS**
   + **RDS – Add Role to Database (RDS - 將角色新增至資料庫)**

   然後在底部選擇 **Next:Permissions (下一步：許可)**。

1. 針對 **Attach permissions policies (連接許可政策)**，輸入您先前建立的 IAM 政策的名稱。然後從清單中選擇該政策。

1. 遵循主控台的指示操作，直到完成建立角色。

   以上為設定角色的簡略指南。如需有關建立角色的詳細指示，請參閱*《IAM 使用者指南》*中的 [IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html)。

## AWS CLI
<a name="Appendix.SQLServer.Options.S3-integration.preparing.CLI"></a>

使用以下程序，為 Amazon RDS 授予 Amazon S3 儲存貯體的存取權：

1. 建立 IAM 政策以允許 Amazon RDS 存取 S3 儲存貯體。

1. 建立 IAM 角色，讓 Amazon RDS 可代表您存取 S3 儲存貯體。

   如需更多詳細資訊，請參閱《*IAM 使用者指南*》中的[建立角色以將許可委派給 IAM 使用者](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user.html)。

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

**若要建立 IAM 政策**

包含適當動作，以授予資料庫執行個體所需的存取：
+ `ListAllMyBuckets` – 必要
+ `ListBucket` – 必要
+ `GetBucketAcl` – 必要
+ `GetBucketLocation` – 必要
+ `GetObject` – 將 S3 中的檔案下載至 時需要`D:\S3\`
+ `PutObject` – 將 `D:\S3\` 中的檔案上傳至 S3 時需要
+ `ListMultipartUploadParts` – 將 `D:\S3\` 中的檔案上傳至 S3 時需要
+ `AbortMultipartUpload` – 將 `D:\S3\` 中的檔案上傳至 S3 時需要

1. 下列AWS CLI命令會使用這些選項建立名為 `rds-s3-integration-policy`的 IAM 政策。其將存取權授予名為 *amzn-s3-demo-bucket* 的儲存貯體。  
**Example**  

   對於 Linux、macOS 或 Unix：

   ```
   aws iam create-policy \
   	 --policy-name rds-s3-integration-policy \
   	 --policy-document '{
   	        "Version": "2012-10-17",		 	 	 
   	        "Statement": [
   	            {
   	                "Effect": "Allow",
   	                "Action": "s3:ListAllMyBuckets",
   	                "Resource": "*"
   	            },
   	            {
   	                "Effect": "Allow",
   	                "Action": [
   	                    "s3:ListBucket",
   	                    "s3:GetBucketAcl",
   	                    "s3:GetBucketLocation"
   	                ],
   	                "Resource": "arn:aws:s3:::amzn-s3-demo-bucket"
   	            },
   	            {
   	                "Effect": "Allow",
   	                "Action": [
   	                    "s3:GetObject",
   	                    "s3:PutObject",
   	                    "s3:ListMultipartUploadParts",
   	                    "s3:AbortMultipartUpload"
   	                ],
   	                "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/key_prefix/*"
   	            }
   	        ]
   	    }'
   ```

   在 Windows 中：

   務必將行尾改成您的界面所支援的字元 (`^` 而不是 `\`)。另外，在 Windows 中，您必須以 `\` 逸出所有雙引號。為了避免需要在 JSON 中逸出雙引號，您可以將 JSON 儲存為檔案，然後當作參數傳遞。

   首先，建立包含下列許可政策的 `policy.json` 檔案：

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Action": "s3:ListAllMyBuckets",
               "Resource": "*"
           },
           {
               "Effect": "Allow",
               "Action": [
                   "s3:ListBucket",
                   "s3:GetBucketACL",
                   "s3:GetBucketLocation"
               ],
               "Resource": "arn:aws:s3:::amzn-s3-demo-bucket"
           },
           {
               "Effect": "Allow",
               "Action": [
                   "s3:GetObject",
                   "s3:PutObject",
                   "s3:ListMultipartUploadParts",
                   "s3:AbortMultipartUpload"
               ],
               "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/key_prefix/*"
           }
       ]
   }
   ```

------

   然後，使用以下命令建立政策：

   ```
   aws iam create-policy ^
        --policy-name rds-s3-integration-policy ^
        --policy-document file://file_path/assume_role_policy.json
   ```

1. 政策建立後，請記下政策的 Amazon Resource Name (ARN)。後續步驟需要此 ARN。

**建立 IAM 角色**
+ 下列AWS CLI命令會為此目的建立 `rds-s3-integration-role` IAM 角色。  
**Example**  

  針對 Linux、macOS 或 Unix：

  ```
  aws iam create-role \
  	   --role-name rds-s3-integration-role \
  	   --assume-role-policy-document '{
  	     "Version": "2012-10-17",		 	 	 
  	     "Statement": [
  	       {
  	         "Effect": "Allow",
  	         "Principal": {
  	            "Service": "rds.amazonaws.com"
  	          },
  	         "Action": "sts:AssumeRole"
  	       }
  	     ]
  	   }'
  ```

  在 Windows 中：

  務必將行尾改成您的界面所支援的字元 (`^` 而不是 `\`)。另外，在 Windows 中，您必須以 `\` 逸出所有雙引號。為了避免需要在 JSON 中逸出雙引號，您可以將 JSON 儲存為檔案，然後當作參數傳遞。

  首先，建立包含下列政策的 `assume_role_policy.json` 檔案：

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Principal": {
                  "Service": [
                      "rds.amazonaws.com"
                  ]
              },
              "Action": "sts:AssumeRole"
          }
      ]
  }
  ```

------

  使用以下命令建立 IAM 角色：

  ```
  aws iam create-role ^
       --role-name rds-s3-integration-role ^
       --assume-role-policy-document file://file_path/assume_role_policy.json
  ```  
**Example 使用全域條件內容索引鍵建立 IAM 角色**  

  建議您在資源型政策中使用 [https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-sourcearn](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-sourcearn) 和 [https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-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)最有效的方式。

  您可以同時使用全域條件內容索引鍵和包含帳號 ID 的 `aws:SourceArn` 值。在此情況下，當在相同政策陳述式中使用 `aws:SourceAccount` 值和 `aws:SourceArn` 裡的帳户時，兩者必須使用同樣的帳户 ID。
  + 如果您想要跨服務存取單一資源，請使用 `aws:SourceArn`。
  + 如果您想要允許該帳戶中的任何資源與跨服務使用相關聯，請使用 `aws:SourceAccount`。

  在政策中，請務必使用 `aws:SourceArn` 全域條件內容索引鍵，其中包含存取角色的資源之完整 Amazon 資源名稱 (ARN)。為進行 S3 整合，請務必包含資料庫執行個體 ARN，如下列範例所示。

  對於 Linux、macOS 或 Unix：

  ```
  aws iam create-role \
  	   --role-name rds-s3-integration-role \
  	   --assume-role-policy-document '{
  	     "Version": "2012-10-17",		 	 	 
  	     "Statement": [
  	       {
  	         "Effect": "Allow",
  	         "Principal": {
  	            "Service": "rds.amazonaws.com"
  	          },
  	         "Action": "sts:AssumeRole",
                  "Condition": {
                      "StringEquals": {
                          "aws:SourceArn":"arn:aws:rds:Region:my_account_ID:db:db_instance_identifier"
                      }
                  }
  	       }
  	     ]
  	   }'
  ```

  在 Windows 中：

  將全域條件內容索引鍵新增至 `assume_role_policy.json`。

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Principal": {
                  "Service": [
                      "rds.amazonaws.com"
                  ]
              },
              "Action": "sts:AssumeRole",
              "Condition": {
                  "StringEquals": {
                      "aws:SourceArn":"arn:aws:rds:Region:my_account_ID:db:db_instance_identifier"
                  }
              }
          }
      ]
  }
  ```

------

**將 IAM 政策連接至 IAM 角色**
+ 下列AWS CLI命令會將政策連接至名為 的角色`rds-s3-integration-role`。將 `your-policy-arn` 替換為您前個步驟記下的政策 ARN。  
**Example**  

  對於 Linux、macOS 或 Unix：

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

  在 Windows 中：

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