-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3_utils.py
37 lines (27 loc) · 1.28 KB
/
s3_utils.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
import pickle, boto3, traceback, os
class s3_utils():
def __init__( self ):
self.bucket_name_ = os.getenv('NETWORKX_S3')
self.s3_client = boto3.client('s3', region_name='ap-south-1')
def shipToS3( self, file_nm, contents, pickle=False ):
print('Shipping to bucket ->', self.bucket_name_)
if pickle == True:
pickled_file_ = pickle.dumps( contents )
self.s3_client.put_object(Bucket=self.bucket_name_, Key=file_nm, Body=pickled_file_ )
else:
self.s3_client.put_object(Bucket=self.bucket_name_, Key=file_nm, Body=contents )
def readFromS3( self, file_nm ):
try:
response = self.s3_client.get_object( Bucket=self.bucket_name_, Key=file_nm )
except:
print('S3 retreival failure->', traceback.format_exc())
return None
return response['Body'].read()
def relevantFiles( self, pattern ):
response = self.s3_client.list_objects_v2( Bucket=self.bucket_name_ )
matching_objects = [ obj['Key'] for obj in response.get('Contents', []) \
if pattern in obj['Key'] ]
return matching_objects
if __name__ == "__main__":
s3_ = s3_utils()
print( s3_.readFromS3( 'graph_store.pickle' ) )