-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3-listobjectversions.py
54 lines (44 loc) · 1.31 KB
/
s3-listobjectversions.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
import boto3
import time
import pprint
from botocore.exceptions import ClientError
'''
This script will list all object version 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_object_versions(
Bucket=bucket,
Delimiter=',',
EncodingType='url',
KeyMarker=KEY_LAST,
MaxKeys=1000,
)
except ClientError as e:
logging.error(e)
print('KeyMarker: ' + KEY_LAST)
#pprint.pprint(response)
VERSIONS = response.get('Versions')
METADATA = response.get('ResponseMetadata')
#print("Matadeta: " + str(METADATA))
#print('Contents: ' + str(CONTENTS))
if not VERSIONS:
TIME_DELTA = TIME_LIST[-1] - TIME_LIST[0]
print('We got last file, total time taken: ' + str(TIME_DELTA))
break
else:
OBJ_FIRST = VERSIONS[0]
KEY_FIRST = OBJ_FIRST.get('Key')
OBJ_LAST = VERSIONS[-1]
KEY_LAST = OBJ_LAST.get('Key')
print('First File : ' + KEY_FIRST)
print('Last File : '+ KEY_LAST)
print('-----------------------')