我想從 AWS Glue、Amazon EMR 或 Amazon Athena 存取 Amazon Simple Storage Service (Amazon S3) 請求者付費儲存貯體。
簡短說明
若要存取已啟用](https://docs.aws.amazon.com/AmazonS3/latest/dev/RequesterPaysBuckets.html)請求者付費[的 S3 儲存貯體,所有針對儲存貯體的請求都必須具有請求者付費的標頭。
解決方法
AWS Glue
根據預設,向 Amazon S3 發出的 AWS Glue 請求不包含請求者付費的標頭。如果沒有這個標頭,請求者付費儲存貯體的 API 呼叫會失敗,並出現 AccessDenied 的例外狀況。若要將請求者付費標頭新增至 ETL 指令碼,請使用 hadoopConfiguration().set() 以在 GlueContext 變數或 Apache Spark 工作階段變數上啟用 fs.s3.useRequesterPaysHeader。
GlueContext:
glueContext._jsc.hadoopConfiguration().set("fs.s3.useRequesterPaysHeader","true")
Spark 工作階段:
spark._jsc.hadoopConfiguration().set("fs.s3.useRequesterPaysHeader","true")
以下是如何在 ETL 指令碼中使用標頭的範例。將以下值取代為如下所示:
database_name:您的資料庫名稱 your_table_name:您的表格名稱 s3://awsdoc-example-bucket/path-to-source-location/:至來源儲存貯體的路徑 s3://awsdoc-example-bucket/path-to-target-location/:至目標儲存貯體的路徑
import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
from awsglue.dynamicframe import DynamicFrame
## @params: [JOB_NAME]
args = getResolvedOptions(sys.argv, ['JOB_NAME'])
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)
spark._jsc.hadoopConfiguration().set("fs.s3.useRequesterPaysHeader","true")
# glueContext._jsc.hadoopConfiguration().set("fs.s3.useRequesterPaysHeader","true")
##AWS Glue DynamicFrame read and write
datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "your_database_name", table_name = "your_table_name", transformation_ctx = "datasource0")
datasource0.show()
datasink = glueContext.write_dynamic_frame.from_options(frame = datasource0, connection_type = "s3", connection_options = {"path":"s3://awsdoc-example-bucket/path-to-source-location/"}, format = "csv")
##Spark DataFrame read and write
df = spark.read.csv("s3://awsdoc-example-bucket/path-to-source-location/")
df.show()
df.write.csv("s3://awsdoc-example-bucket/path-to-target-location/")
job.commit()
Amazon EMR
在 /usr/share/aws/emr/emrfs/conf/emrfs-site.xml 中設定下列屬性:
<property>
<name>fs.s3.useRequesterPaysHeader</name>
<value>true</value>
</property>
Athena
若要允許工作群組成員查詢請求者付費儲存貯體,請在建立工作群組時選擇在 Amazon S3 中啟用請求者付費儲存貯體查詢。如需詳細資訊,請參閱建立工作群組。
相關資訊
下載請求者付費儲存貯體中的物件
如何對來自 Amazon S3 的「403 存取遭拒」錯誤進行疑難排解?