AWS lambda s3文件解压缩python中的文件名问题
我有lambda函数,如下所示AWS lambda s3文件解压缩python中的文件名问题,python,python-2.7,amazon-web-services,amazon-s3,aws-lambda,Python,Python 2.7,Amazon Web Services,Amazon S3,Aws Lambda,我有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
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内容:
提取的文件夹内容:
我在python脚本中找不到bug,
任何帮助都将不胜感激。提前谢谢 我怀疑您的问题在于这一行:
filePath = "playable/staging/" + key.replace("package.zip", "") + fileName
请注意,您正在删除字符串package.zip
,但是(从“前缀”中可以看到),该字符串实际上是package-1542108930.zip
尝试:
如果你只是不想要任何名字
如果要维护时间戳,则:
filePath = "playable/staging/" + key.replace("package-", "").replace(".zip", "") + fileName
您的代码只适用于一个zip文件,问题是每个zip都有不同的名称(时间戳)是的-您必须以编程方式使用正确的字符串创建替换字符串;但出现此问题的原因是,当前的“package.zip”字符串与需要替换的字符串不匹配。您的问题中没有足够的上下文让我知道您如何做到这一点,但我将从S3事件中猜一猜。如果这仍然不是您想要的,那么您能否提供一个正确路径的示例。使用您的代码
filePath=“playable/staging/”+fileName
zip没有被提取。我有许多包-{timestamp}。zip文件具有不同的时间戳,我想提取它们在不同的bucket中的内容。
filePath = "playable/staging/" + key.replace("package-", "").replace(".zip", "") + fileName