-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3-listobjectsv2.py
70 lines (57 loc) · 1.73 KB
/
s3-listobjectsv2.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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 = []
TOKEN = ''
while KEY_LAST != KEY_FIRST:
print('++++++++++++++++++++++++++++++++++++')
print(time.time())
TIME_LIST.append(time.time())
if len(TOKEN) !=0:
try:
response = s3_client.list_objects_v2(
Bucket=bucket,
Delimiter=',',
EncodingType='url',
ContinuationToken=TOKEN,
MaxKeys=1000
)
except ClientError as e:
logging.error(e)
else:
try:
response = s3_client.list_objects_v2(
Bucket=bucket,
Delimiter=',',
EncodingType='url',
MaxKeys=1000
)
except ClientError as e:
logging.error(e)
CONTENTS = response.get('Contents')
METADATA = response.get('ResponseMetadata')
print(METADATA)
TOKEN = response.get('NextContinuationToken')
if TOKEN is None:
TIME_DELTA = TIME_LIST[-1] - TIME_LIST[0]
print('We got last File, total time taken: ' + str(TIME_DELTA))
quit()
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('-----------------------')