Skip to content

Instantly share code, notes, and snippets.

@emilianobilli
Created November 18, 2020 23:35
Show Gist options
  • Save emilianobilli/40d817df5b16707e9ebec3dff2323def to your computer and use it in GitHub Desktop.
Save emilianobilli/40d817df5b16707e9ebec3dff2323def to your computer and use it in GitHub Desktop.
class S3Bucket(object):
def __init__(self, bucket, aws_access_key_id, aws_secret_access_key):
self.client = boto3.client(
's3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
self.bucket = bucket
def listbucket(self, prefix=''):
items = []
response = self.client.list_objects_v2(
Bucket=self.bucket,
Prefix=prefix,
Delimiter='/'
)
if 'Contents' not in response:
return None
for content in response['Contents']:
if content['Key'] == prefix:
continue
items.append(
{
'name': content['Key'],
'size': content['Size'],
'created': 0,
'modified': 0,
'type': 'file'
}
)
if 'CommonPrefixes' in response:
for directory in response['CommonPrefixes']:
items.append(
{
'name': directory['Prefix'][len(prefix):],
'size': 0,
'created': 0,
'modified': 0,
'type': 'directory'
}
)
return items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment