我具有如下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脚本中的错误, 任何帮助将不胜感激。提前致谢。
答案 0 :(得分:0)
我怀疑您的问题是此行:
filePath = "playable/staging/" + key.replace("package.zip", "") + fileName
请注意,您要删除字符串package.zip
,但是(如从“前缀”中可以看到的)字符串实际上是package-1542108930.zip
。
尝试:
filePath = "playable/staging/" + fileName
如果您根本不想使用任何名称。
如果要保留时间戳,则:
filePath = "playable/staging/" + key.replace("package-", "").replace(".zip", "") + fileName