简体   繁体   English

AWS Lambda s3文件解压缩python中的文件名问题

[英]File name issue in AWS lambda s3 file unzip python

I have lambda function as below 我有如下的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

It takes zip file from s3 bucket and extracts it to another s3 bucket 它将s3存储桶中的zip文件提取到另一个s3存储桶中

the function runs successfully, but the extracted filename has the zip file name as prefix, see below pictures for reference 该函数运行成功,但是提取的文件名以zip文件名作为前缀,请参见下面的图片以供参考

Source zip file : package-1542108930.zip 原始zip文件: package-1542108930.zip

Source zip contents: source zip files 源zip内容: 源zip文件

Extracted folder contents: extracted files 提取的文件夹内容: 提取的文件

I'm unable to find the bug in python script, any help will appreciated. 我无法在python脚本中找到错误,我们将不胜感激。 Thanks in advance. 提前致谢。

I suspect your problem is this line: 我怀疑您的问题是此行:

filePath = "playable/staging/" + key.replace("package.zip", "") + fileName

Note that you're removing the string package.zip but (as you can see from the "prefixes"), the string is actually package-1542108930.zip . 请注意,您正在删除字符串package.zip但是(如您从“前缀”中看到的)字符串实际上是package-1542108930.zip

Try: 尝试:

filePath = "playable/staging/" + fileName

If you simply do not want any name. 如果您根本不想要任何名称。

If you want to maintain the timestamp, then: 如果要保留时间戳,请执行以下操作:

filePath = "playable/staging/" + key.replace("package-", "").replace(".zip", "") + fileName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM