forked from piskvorky/smart_open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_keys.py
49 lines (38 loc) · 1.15 KB
/
check_keys.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
"""Check that the environment variables contain valid boto3 credentials."""
import logging
import os
import boto3
import boto3.session
def check(session):
client = session.client('s3')
try:
response = client.list_buckets()
except Exception as e:
logging.exception(e)
return None
else:
return [b['Name'] for b in response['Buckets']]
def check_implicit():
session = boto3.session.Session()
buckets = check(session)
if buckets:
print('implicit check OK: %r' % buckets)
else:
print('implicit check failed')
def check_explicit():
key_id = os.environ.get('AWS_ACCESS_KEY_ID')
secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
if not (key_id and secret_key):
print('no credentials found in os.environ, skipping explicit check')
return
session = boto3.session.Session(aws_access_key_id=key_id, aws_secret_access_key=secret_key)
buckets = check(session)
if buckets:
print('explicit check OK: %r' % buckets)
else:
print('explicit check failed')
def main():
check_implicit()
check_explicit()
if __name__ == '__main__':
main()