我正在寻找一种简单的方法来将s3存储桶中存在的zip / gzip提取到同一存储桶位置,并在提取后删除父zip / gzip文件。
我目前无法使用任何API实现这一目标。
尝试了原生boto,pyfilesystem(fs),s3fs。 源链接和目标链接似乎是这些功能的问题。
(使用Python 2.x / 3.x& Boto 2.x)
我看到有一个用于执行此工作的node.js(unzip-to-s3)的API,但没有用于python的API。
我能想到的几个实现:
选项4将是最不受欢迎的选项,以最小化ec2插件的架构开销。
需要支持才能实现此功能,并在稍后阶段与lambda集成。非常感谢任何指向这些实现的指针。
先谢谢,
孙大信。
答案 0 :(得分:0)
您可以尝试https://www.cloudzipinc.com/将几种不同格式的存档从S3解压缩/扩展到存储桶中的目标位置。我用它将数字目录的组件解压缩到S3中。
答案 1 :(得分:0)
使用ec2实例解决了。 将s3文件复制到ec2中的本地目录 并将该目录复制回S3存储桶。
答案 2 :(得分:0)
def s3Unzip(srcBucket,dst_dir):
'''
function to decompress the s3 bucket contents to local machine
Args:
srcBucket (string): source bucket name
dst_dir (string): destination location in the local/ec2 local file system
Returns:
None
'''
#bucket = s3.lookup(bucket)
s3=s3Conn
path=''
bucket = s3.lookup(bucket_name)
for key in bucket:
path = os.path.join(dst_dir, key.name)
key.get_contents_to_filename(path)
if path.endswith('.zip'):
opener, mode = zipfile.ZipFile, 'r'
elif path.endswith('.tar.gz') or path.endswith('.tgz'):
opener, mode = tarfile.open, 'r:gz'
elif path.endswith('.tar.bz2') or path.endswith('.tbz'):
opener, mode = tarfile.open, 'r:bz2'
else:
raise ValueError ('unsuppported format')
try:
os.mkdir(dst_dir)
print ("local directories created")
except Exception:
logger_s3.warning ("Exception in creating local directories to extract zip file/ folder already existing")
cwd = os.getcwd()
os.chdir(dst_dir)
try:
file = opener(path, mode)
try: file.extractall()
finally: file.close()
logger_s3.info('(%s) extracted successfully to %s'%(key ,dst_dir))
except Exception as e:
logger_s3.error('failed to extract (%s) to %s'%(key ,dst_dir))
os.chdir(cwd)
s3.close
使用" LOAD DATA LOCAL INFILE"查询直接上传到mysql
def upload(file_path,timeformat):
'''
function to upload a csv file data to mysql rds
Args:
file_path (string): local file path
timeformat (string): destination bucket to copy data
Returns:
None
'''
for file in file_path:
try:
con = connect()
cursor = con.cursor()
qry="""LOAD DATA LOCAL INFILE '%s' INTO TABLE xxxx FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (col1 , col2 ,col3, @datetime , col4 ) set datetime = str_to_date(@datetime,'%s');""" %(file,timeformat)
cursor.execute(qry)
con.commit()
logger_rds.info ("Loading file:"+file)
except Exception:
logger_rds.error ("Exception in uploading "+file)
##Rollback in case there is any error
con.rollback()
cursor.close()
# disconnect from server
con.close()