-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3-listobjects.py
52 lines (40 loc) · 1.16 KB
/
s3-listobjects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import boto3
import time
from botocore.exceptions import ClientError
'''
This script will list all objects from the bucket 1000 keys at a time
'''
bucket = "<BucketName>"
s3_client = boto3.client('s3')
KEY_FIRST = ''
KEY_LAST = 'file'
TIME_LIST = []
while KEY_LAST != KEY_FIRST:
print('-----------------------')
print(time.time())
TIME_LIST.append(time.time())
try:
response = s3_client.list_objects(
Bucket=bucket,
Delimiter=',',
EncodingType='url',
Marker=KEY_LAST,
MaxKeys=1000,
)
except ClientError as e:
logging.error(e)
CONTENTS = response.get('Contents')
METADATA = response.get('ResponseMetadata')
print(METADATA)
if not CONTENTS:
TIME_DELTA = TIME_LIST[-1] - TIME_LIST[0]
print('We got last file, total time taken: ' + str(TIME_DELTA))
break
else:
OBJ_FIRST = CONTENTS[0]
KEY_FIRST = OBJ_FIRST.get('Key')
OBJ_LAST = CONTENTS[-1]
KEY_LAST = OBJ_LAST.get('Key')
print('First File : ' + KEY_FIRST)
print('Last File : '+ KEY_LAST)
print('-----------------------')