

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

# 將資料從 Amazon S3 匯入 Aurora PostgreSQL 資料庫叢集
<a name="USER_PostgreSQL.S3Import.FileFormats"></a>

您可以使用 aws\$1s3 擴充功能的 `table_import_from_s3` 函數，由 Amazon S3 儲存貯體匯入資料。如需參考資訊，請參閱 [aws\$1s3.table\$1import\$1from\$1s3](USER_PostgreSQL.S3Import.Reference.md#aws_s3.table_import_from_s3)。

**注意**  
下列範例使用 IAM 角色方法，允許對 Amazon S3 儲存貯體的存取。因此，`aws_s3.table_import_from_s3` 函數呼叫不包含登入資料參數。

以下顯示一個典型範例。

```
postgres=> SELECT aws_s3.table_import_from_s3(
   't1',
   '', 
   '(format csv)',
   :'s3_uri'
);
```

參數如下：
+ `t1` – PostgreSQL 資料庫叢集的表格名稱，資料會複製到此表格。
+ `''` – 資料庫表格之中選用的欄清單。您可使用此參數指出哪些 S3 資料欄要置於哪些表格欄。如果沒有指定欄，所有欄都會複製到表格中。如需使用欄清單的範例，請參閱 [匯入使用自訂分隔符號的 Amazon S3 檔案](#USER_PostgreSQL.S3Import.FileFormats.CustomDelimiter)。
+ `(format csv)` – PostgreSQL COPY 引數。複製程序使用 [PostgreSQL COPY](https://www.postgresql.org/docs/current/sql-copy.html) 命令的引數及格式匯入資料。格式的選擇包括逗號分隔值 (CSV)、文字和二進位。預設為文字。
+  `s3_uri` – 包含識別 Amazon S3 檔案資訊的結構。如需使用 [aws\$1commons.create\$1s3\$1uri](USER_PostgreSQL.S3Import.Reference.md#USER_PostgreSQL.S3Import.create_s3_uri) 函數來建立 `s3_uri` 結構的範例，請參閱 [從 Amazon S3 資料匯入資料的概觀](USER_PostgreSQL.S3Import.Overview.md)。

如需此函數狀態的詳細資訊，請參閱 [aws\$1s3.table\$1import\$1from\$1s3](USER_PostgreSQL.S3Import.Reference.md#aws_s3.table_import_from_s3)。

`aws_s3.table_import_from_s3` 函數傳回文字。若要指定從 Amazon S3 儲存貯體匯入的其他檔案類型，請參閱下列其中一個範例。

**注意**  
匯入 0 位元組檔案將導致錯誤。

**Topics**
+ [

## 匯入使用自訂分隔符號的 Amazon S3 檔案
](#USER_PostgreSQL.S3Import.FileFormats.CustomDelimiter)
+ [

## 匯入 Amazon S3 壓縮 (gzip) 檔案
](#USER_PostgreSQL.S3Import.FileFormats.gzip)
+ [

## 匯入編碼的 Amazon S3 檔案
](#USER_PostgreSQL.S3Import.FileFormats.Encoded)

## 匯入使用自訂分隔符號的 Amazon S3 檔案
<a name="USER_PostgreSQL.S3Import.FileFormats.CustomDelimiter"></a>

下列範例顯示如何匯入使用自訂分隔符號的檔案。其中也顯示如何使用 `column_list` 函數的 [aws\$1s3.table\$1import\$1from\$1s3](USER_PostgreSQL.S3Import.Reference.md#aws_s3.table_import_from_s3) 參數，控制資料放置於資料庫表格的位置。

我們在此範例假設下列資訊整理至 Amazon S3 檔案之中的縱線分隔欄。

```
1|foo1|bar1|elephant1
2|foo2|bar2|elephant2
3|foo3|bar3|elephant3
4|foo4|bar4|elephant4
...
```

**匯入使用自訂分隔符號的檔案**

1. 在資料庫為匯入資料建立表格。

   ```
   postgres=> CREATE TABLE test (a text, b text, c text, d text, e text);
   ```

1. 使用以下的 [aws\$1s3.table\$1import\$1from\$1s3](USER_PostgreSQL.S3Import.Reference.md#aws_s3.table_import_from_s3) 函數格式由 Amazon S3 檔案匯入資料。

   您可在 [aws\$1commons.create\$1s3\$1uri](USER_PostgreSQL.S3Import.Reference.md#USER_PostgreSQL.S3Import.create_s3_uri) 函數呼叫之中內嵌 `aws_s3.table_import_from_s3` 函數呼叫以指定檔案。

   ```
   postgres=> SELECT aws_s3.table_import_from_s3(
      'test',
      'a,b,d,e',
      'DELIMITER ''|''', 
      aws_commons.create_s3_uri('amzn-s3-demo-bucket', 'pipeDelimitedSampleFile', 'us-east-2')
   );
   ```

資料目前位於下列欄的表格中。

```
postgres=> SELECT * FROM test;
a | b | c | d | e 
---+------+---+---+------+-----------
1 | foo1 | | bar1 | elephant1
2 | foo2 | | bar2 | elephant2
3 | foo3 | | bar3 | elephant3
4 | foo4 | | bar4 | elephant4
```

## 匯入 Amazon S3 壓縮 (gzip) 檔案
<a name="USER_PostgreSQL.S3Import.FileFormats.gzip"></a>

下列範例顯示如何由以 gzip 壓縮的 Amazon S3 匯入檔案。匯入的檔案需具有以下 Amazon S3 中繼資料：
+ 索引鍵：`Content-Encoding`
+ 值：`gzip`

如果您使用 上傳檔案 AWS 管理主控台，系統通常會套用中繼資料。如需有關使用 AWS 管理主控台、 AWS CLI或 API 將檔案上傳至 Amazon S3 的資訊，請參閱《*Amazon Simple Storage Service 使用者指南*》中的[上傳物件](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html)。

如需 Amazon S3 中繼資料的詳細資訊以及系統所提供中繼資料的詳細資訊，請參閱《*Amazon Simple Storage Service 使用者指南*》中的[在 Amazon S3 主控台中編輯物件中繼資料](https://docs.aws.amazon.com/AmazonS3/latest/userguide/add-object-metadata.html)。

請依據以下所示內容，將 gzip 檔案匯入 Aurora PostgreSQL 資料庫叢集 。

```
postgres=> CREATE TABLE test_gzip(id int, a text, b text, c text, d text);
postgres=> SELECT aws_s3.table_import_from_s3(
 'test_gzip', '', '(format csv)',
 'amzn-s3-demo-bucket', 'test-data.gz', 'us-east-2'
);
```

## 匯入編碼的 Amazon S3 檔案
<a name="USER_PostgreSQL.S3Import.FileFormats.Encoded"></a>

下列範例顯示如何由採用 Windows-1252 編碼的 Amazon S3 匯入檔案。

```
postgres=> SELECT aws_s3.table_import_from_s3(
 'test_table', '', 'encoding ''WIN1252''',
 aws_commons.create_s3_uri('amzn-s3-demo-bucket', 'SampleFile', 'us-east-2')
);
```