

# 在 AWS Glue 中使用 Amazon S3 Express One Zone 存储类
<a name="aws-glue-programming-etl-s3-express"></a>

使用 AWS Glue 5.1 及更高版本，您可以通过 ETL 作业在 [Amazon S3 Express One Zone](https://docs.aws.amazon.com/AmazonS3/latest/userguide/directory-bucket-high-performance.html#s3-express-one-zone) 存储类目录存储桶中读取和写入数据。S3 Express One Zone 是一种高性能的单区 Amazon S3 存储类别，可为大多数延迟敏感型应用程序提供一致的个位数毫秒级数据访问。

## 先决条件
<a name="aws-glue-programming-etl-s3-express-prereqs"></a>

在将 S3 Express One Zone 与 AWS Glue 结合使用之前，必须满足以下条件：
+ 运行版本 5.1 或更高版本的 AWS Glue 作业。
+ 在 AWS Glue 作业所在的同一区域中创建一个 S3 目录存储桶。目录存储桶不支持跨区域访问。有关更多信息，请参阅*《Amazon S3 用户指南》*中的[创建目录存储桶](https://docs.aws.amazon.com/AmazonS3/latest/userguide/directory-bucket-create.html)。
+ 您 IAM 角色中的 `s3express:CreateSession` 权限。当 S3 Express One Zone 对目录存储桶执行操作时，它会代表您调用 `CreateSession`。

## IAM 权限
<a name="aws-glue-programming-etl-s3-express-iam"></a>

向 AWS Glue 作业的 IAM 角色添加以下权限以允许访问 S3 Express One Zone 目录存储桶：

```
{
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3express:CreateSession",
            "Resource": "arn:aws:s3express:*:*:bucket/{{EXAMPLE-BUCKET}}--{{az-id}}--x-s3"
        }
    ]
}
```

请将 {{EXAMPLE-BUCKET}} 替换为您自己的目录存储桶名称，并将 {{az-id}} 替换为可用区 ID（例如，`use1-az4`）。

## 读取和写入数据
<a name="aws-glue-programming-etl-s3-express-usage"></a>

AWS Glue 5.1 及以上版本支持使用 `s3://` 和 `s3a://` 两种 URI 方案访问 S3 Express One Zone 目录存储桶。无需其他配置。

以下示例说明了如何在 AWS Glue ETL 作业中从 S3 Express One Zone 目录存储桶中读取和写入数据：

```
import sys
from pyspark.context import SparkContext
from awsglue.context import GlueContext

sc = SparkContext.getOrCreate()
glueContext = GlueContext(sc)
spark = glueContext.spark_session

# S3 Express One Zone directory bucket path
express_path = "s3://EXAMPLE-BUCKET--use1-az4--x-s3/my-data/"

# Read data from S3 Express One Zone
df = spark.read.parquet(express_path)

# Write data to S3 Express One Zone
df.write.mode("overwrite").parquet(express_path + "output/")
```

您也可以在 S3 Express One Zone 中使用 DynamicFrames：

```
# Read with DynamicFrame
dynamicFrame = glueContext.create_dynamic_frame.from_options(
    connection_type="s3",
    connection_options={"paths": [express_path]},
    format="parquet"
)

# Write with DynamicFrame
glueContext.write_dynamic_frame.from_options(
    frame=dynamicFrame,
    connection_type="s3",
    connection_options={"path": express_path + "output/"},
    format="parquet"
)
```