参考
AWS SDK を使用したリクエストの実行 - Amazon Simple Storage Service
→AWS SDK説明
Python | チュートリアル、API、SDK、ドキュメント | AWS 開発者センター
→AWS SDK Python概要
aws-doc-sdk-examples/python/example_code at master · awsdocs/aws-doc-sdk-examples · GitHub
→AWS SDK Pythonサンプルコード
Amazon S3 Examples — Boto 3 Docs 1.9.62 documentation
S3 — Boto 3 Docs 1.9.62 documentation
→AWS SDK Python S3解説
サンプルコード
import boto3 import botocore import random, string # Create an S3 client s3 = boto3.client('s3') # Call S3 to list current buckets response = s3.list_buckets() # Get a list of all bucket names from the response buckets = [bucket['Name'] for bucket in response['Buckets']] # Create bucket name bn = ''.join([random.choice(string.ascii_letters + string.digits).lower() for i in range(16)]) bn = 'vz6g2zdx3ry3dpiv' # Check whether my-bucket exists if bn in buckets: print(bn + 'already exists!') else: s3.create_bucket(Bucket=bn) # Upload file to bucket fn = '/etc/hosts' fn_k = fn.split('/')[-1] s3.upload_file(fn, bn, fn_k) # Download file from bucket try: s3.download_file(bn, fn_k, 'downloaded_file') except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == "404": print("The object does not exist.") else: raise
API種類
低レベルAPIと高レベルAPIがあり。
Clientは低レベルAPI
Resourcesは高レベルAPI
Boto3 で S3 のオブジェクトを操作する(高レベルAPIと低レベルAPI) - Qiita
CORS
Cross-Origin Resource Sharing (CORS) の略
別Webサイトからの呼び出しの場合でも、許可する設定。
Boto3でコンテンツに対し設定可能。コードは以下参照。
Configuring Amazon S3 Buckets — Boto 3 Docs 1.9.62 documentation
呼び出し元URLやメソッドなどを定義
CORS設定の取得コード
import boto3 # Create an S3 client s3 = boto3.client('s3') # Call S3 to get CORS configuration for selected bucket result = s3.get_bucket_cors(Bucket='my-bucket')
設定がない場合、以下エラーになる。
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/site-packages/botocore/client.py", line 320, in _api_call return self._make_api_call(operation_name, kwargs) File "/usr/local/lib/python2.7/site-packages/botocore/client.py", line 623, in _make_api_call raise error_class(parsed_response, operation_name) botocore.exceptions.ClientError: An error occurred (NoSuchCORSConfiguration) when calling the GetBucketCors operation: The CORS configuration does not exist
CORS設定の取得コード
import boto3 # Create an S3 client s3 = boto3.client('s3') # Create the CORS configuration cors_configuration = { 'CORSRules': [{ 'AllowedHeaders': ['Authorization'], 'AllowedMethods': ['GET', 'PUT'], 'AllowedOrigins': ['*'], 'ExposeHeaders': ['GET', 'PUT'], 'MaxAgeSeconds': 3000 }] } # Set the new CORS configuration on the selected bucket s3.put_bucket_cors(Bucket='my-bucket', CORSConfiguration=cors_configuration)
ACL設定の取得コード
import boto3 # Create an S3 client s3 = boto3.client('s3') # Call to S3 to retrieve the policy for the given bucket result = s3.get_bucket_acl(Bucket='my-bucket') print(result)
続き
Amazon S3 Examples — Boto 3 Docs 1.9.62 documentation
のサンプルコードの
Working with Amazon S3 Bucket Policies
や
アクセスコントロールリスト (ACL) の概要 - Amazon Simple Storage Service
を見て、ACL詳細を調査する。