공부/TIL(을 빙자한 기타)
python3 code for object copy using boto3
빛나는나무
2023. 7. 25. 21:07
import boto3
source_bucket = "source-bucket"
source_key = "path/to/source/file.ext"
destination_bucket = "destination-bucket"
destination_key = "path/to/destination/file.ext"
# AWS authentification info
session = boto3.Session(
aws_access_key_id="YOUR_ACCESS_KEY",
aws_secret_access_key="YOUR_SECRET_ACCESS_KEY",
region_name="YOUR_REGION"
)
# Create S3 client object
s3 = session.client("s3")
# Copy file
copy_source = {"Bucket": source_bucket, "Key": source_key}
s3.copy_object(CopySource=copy_source, Bucket=destination_bucket, Key=destination_key)
In the example code, "source_bucket" is a source bucket name for files to copy, "source_key" is a path and file name of the file. "destination_bucket" is a bucket name for copied file, "destination_key" is a path and name of copied file in "destination_bucket".
And "YOUR_ACCESS_KEY", "YOUR_SECRET_ACCESS_KEY", "YOUR_REGION" is an AWS access key, Secret access key, region for destination bucket.