Before start using boto3, we should set up an user on AWS IAM and grant S3 access permission.
import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
import boto3
s3 = boto3.resource('s3')
bucket = 'mybucket'
fname = 'test.txt'
with open(fname, 'rb') as f:
s3.Bucket(bucket).put_object(Key=fname, Body=f)
ref: How to create a directory in a bucket using boto3 ? #377
import boto3
from pathlib import Path
s3 = boto3.resource('s3')
bucket = 'mybucket'
directory = Path('.')
for p in directory.glob('**/*'):
if p.is_dir():
continue
path = str(p)
with open(path, 'rb') as f:
s3.Bucket(bucket).put_object(Key=path, Body=f)
ref: download_file
import boto3
from pathlib import Path
s3 = boto3.resource('s3')
bucket = 'mybucket'
fname = 'test.txt'
fpath = Path('.') / fname
s3.meta.client.download_file(bucket, fname, str(fpath))
import boto3
s3 = boto3.resource('s3')
bn = 'mybucket'
bucket = s3.Bucket(bn)
for l in bucket.objects.filter(Prefix='dir'):
print(l)
Futher reading: