This is my complete newb intro to Boto and AWS. At the moment my only goal is to be able to access an external agency's S3 bucket, so I want to understand how to use them in particular. This minimal code does what I want it to do, but I haven't figured out how to use this by only declaring s3r as a resource and avoiding having to use s3 as a client. It seems like it would be better to access the bucket from the S3 resource and then work exclusively with the bucket, i.e., bucket.new_key('testdir/')
or bucket.put_object(Key=('testdir/))
. Is this possible, or is there alternately a good reason to reframe how I'm approaching this? Thanks!
import boto3
bucket_name = 'my-bucket-name'
region_name = 'my-region-name'
print('Acquiring s3 service')
s3 = boto3.client('s3', region_name=region_name)
s3r = boto3.resource('s3', region_name=region_name)OB
print('Accessing bucket')
bucket = s3r.Bucket(bucket_name)
print('Emptying bucket')
bucket.objects.all().delete()
print('Uploading folder structures')
s3.put_object(Bucket=bucket_name, Key=('testdir/'))
s3.put_object(Bucket=bucket_name, Key=('testdir/subdir1/'))
s3.put_object(Bucket=bucket_name, Key=('testdir/subdir2/'))