boto3
You must generate an Access Key before getting started. All examples will utilize access_key_id and access_key_secret variables which represent the Access Key ID and Secret Access Key values you generated.
boto3を使用するには、事前に構築されたendpoint_urlの値を設定する必要があります。これは、接続引数を受け入れる任意のboto3の使用を通じて行うことができます。例えば:
import boto3
s3 = boto3.resource('s3', endpoint_url = 'https://<accountid>.r2.cloudflarestorage.com', aws_access_key_id = '<access_key_id>', aws_secret_access_key = '<access_key_secret>')ただし、aws_access_key_idおよびaws_secret_access_keyの引数を省略し、boto3がAWS_ACCESS_KEY_IDおよびAWS_SECRET_ACCESS_KEYの環境変数 ↗に依存することを許可することもできます。
例として、スクリプトは以下のようになります:
import boto3
s3 = boto3.client( service_name ="s3", endpoint_url = 'https://<accountid>.r2.cloudflarestorage.com', aws_access_key_id = '<access_key_id>', aws_secret_access_key = '<access_key_secret>', region_name="<location>", # 必ず次のいずれかでなければなりません: wnam, enam, weur, eeur, apac, auto)
# オブジェクト情報を取得object_information = s3.head_object(Bucket=<R2_BUCKET_NAME>, Key=<FILE_KEY_NAME>)
# 単一ファイルをアップロード/更新s3.upload_fileobj(io.BytesIO(file_content), <R2_BUCKET_NAME>, <FILE_KEY_NAME>)
# オブジェクトを削除s3.delete_object(Bucket=<R2_BUCKET_NAME>, Key=<FILE_KEY_NAME>)python main.pyBuckets: - user-uploads - my-bucket-nameObjects: - cat.png - todos.txt