Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeClose MenuSkip to content
Boto3 1.39.8 documentation
Light LogoDark Logo
Boto3 1.39.8 documentation

Feedback

Do you have a suggestion to improve this website or boto3?Give us feedback.

Back to top

Bucket CORS configuration

Cross Origin Resource Sharing (CORS) enables client web applications in onedomain to access resources in another domain. An S3 bucket can be configuredto enable cross-origin requests. The configuration defines rules that specifythe allowed origins, HTTP methods (GET, PUT, etc.), and other elements.

Retrieve a bucket CORS configuration

Retrieve a bucket’s CORS configuration by calling the AWS SDK for Pythonget_bucket_cors method.

importloggingimportboto3frombotocore.exceptionsimportClientErrordefget_bucket_cors(bucket_name):"""Retrieve the CORS configuration rules of an Amazon S3 bucket    :param bucket_name: string    :return: List of the bucket's CORS configuration rules. If no CORS    configuration exists, return empty list. If error, return None.    """# Retrieve the CORS configurations3=boto3.client('s3')try:response=s3.get_bucket_cors(Bucket=bucket_name)exceptClientErrorase:ife.response['Error']['Code']=='NoSuchCORSConfiguration':return[]else:# AllAccessDisabled error == bucket not foundlogging.error(e)returnNonereturnresponse['CORSRules']

Set a bucket CORS configuration

A bucket’s CORS configuration can be set by calling theput_bucket_corsmethod.

# Define the configuration rulescors_configuration={'CORSRules':[{'AllowedHeaders':['Authorization'],'AllowedMethods':['GET','PUT'],'AllowedOrigins':['*'],'ExposeHeaders':['ETag','x-amz-request-id'],'MaxAgeSeconds':3000}]}# Set the CORS configurations3=boto3.client('s3')s3.put_bucket_cors(Bucket='amzn-s3-demo-bucket',CORSConfiguration=cors_configuration)
On this page

[8]ページ先頭

©2009-2025 Movatter.jp