我有如下的lambda函数:
from __future__ import print_function
import urllib
import zipfile
import boto3
import io
import mimetypes
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
s3 = boto3.client('s3')
bucket = 'staging-bucket'
def lambda_handler(event, context):
try:
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
obj = s3.get_object(Bucket=bucket, Key=key)
with io.BytesIO(obj["Body"].read()) as tf:
# rewind the file
tf.seek(0)
# Read the file as a zipfile and process the members
with zipfile.ZipFile(tf, mode = 'r') as zipf:
for file in zipf.infolist():
fileName = file.filename
contentType, encoding = mimetypes.guess_type(fileName)
contentType = contentType or 'application/octet-stream'
filePath = "playable/staging/" + key.replace("package.zip", "") + fileName
putFile = s3.put_object(ACL = 'public-read', Bucket = "unzipped-bucket", Key = filePath, Body = zipf.read(file), ContentType = contentType)
except Exception as e:
logger.error('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e
return
该函数从s3存储桶中获取zip文件并将其提取到另一个s3存储桶中。
该函数成功运行,但提取的文件名具有zip文件名作为前缀,参见下面的图片。
源zip文件:package-1542108930.zip
源zip文件内容: source zip files
提取的文件夹内容: extracted files
我无法在Python脚本中找到错误,非常感谢您的帮助。
filePath = "playable/staging/" + fileName
,无法将zip文件解压缩。 - Jaydev Shiroya