|
|
|
"""
|
|
|
|
Data Transport - 1.0
|
|
|
|
Steve L. Nyemba, The Phi Technology LLC
|
|
|
|
|
|
|
|
This file is a wrapper around s3 bucket provided by AWS for reading and writing content
|
|
|
|
"""
|
|
|
|
def buckets(self):
|
|
|
|
# def buckets(self):
|
|
|
|
pass
|
|
|
|
# """
|
|
|
|
# This function is a wrapper around the bucket list of buckets for s3
|
|
|
|
# """
|
|
|
|
# return self.s3.get_all_buckets()
|
|
|
|
|
|
|
|
|
|
|
|
class s3Reader(s3,Reader) :
|
|
|
|
"""
|
|
|
|
Because s3 contains buckets and files, reading becomes a tricky proposition :
|
|
|
|
- list files if file is None
|
|
|
|
- stream content if file is Not None
|
|
|
|
@TODO: support read from all buckets, think about it
|
|
|
|
"""
|
|
|
|
def __init__(self,args) :
|
|
|
|
s3.__init__(self,args)
|
|
|
|
def files(self):
|
|
|
|
r = []
|
|
|
|
try:
|
|
|
|
return [item.name for item in self.bucket if item.size > 0]
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
|
|
|
return r
|
|
|
|
def stream(self,limit=-1):
|
|
|
|
"""
|
|
|
|
At this point we should stream a file from a given bucket
|
|
|
|
"""
|
|
|
|
key = self.bucket.get_key(self.filename.strip())
|
|
|
|
if key is None :
|
|
|
|
yield None
|
|
|
|
else:
|
|
|
|
count = 0
|
|
|
|
with smart_open(key) as remote_file:
|
|
|
|
for line in remote_file:
|
|
|
|
if count == limit and limit > 0 :
|
|
|
|
break
|
|
|
|
yield line
|
|
|
|
count += 1
|
|
|
|
def read(self,limit=-1) :
|
|
|
|
if self.filename is None :
|
|
|
|
#
|
|
|
|
# returning the list of files because no one file was specified.
|
|
|
|
return self.files()
|
|
|
|
else:
|
|
|
|
return self.stream(10)
|
|
|
|
|
|
|
|
class s3Writer(s3,Writer) :
|
|
|
|
def __init__(self,args) :
|
|
|
|
s3.__init__(self,args)
|