使用 Python 解压多文件夹目录时出现 No Such Key 错误

No Such Key error when unzipping multi folder directory with Python

我正在使用 Lambda 使用 boto3、Zipfile(作为 zf)和 BytesIO 在 S3 中解压缩文件。当函数被触发时,下面的代码运行(此时我已经初始化了 s3 客户端。我使用 this example 作为我的代码的基础。bucketnamezip_key 值没有问题。s3_uri 也正确生成。:

    bucketname = event["Records"][0]['s3']['bucket']['name']
    zip_key = event["Records"][0]['s3']['object']['key']
    s3_uri = 's3://'+bucketname+'/'
    
    #get zip folder from S3
    zip_obj = s3_client.Object(bucket_name=bucketname, key=zip_key)

    #create IO buffer
    buffer = BytesIO(zip_obj.get()["Body"].read())
    
   
    z = zf.ZipFile(buffer)

    for filename in z.namelist():
        file_info = z.getinfo(filename)
        
        #Ignore folders generated by Mac zip operation
        if "__MACOSX" in str(file_info):
            print("")
        else:
            s3_client.meta.client.upload_fileobj(z.open(filename),Bucket=bucketname,Key=f'output/{filename}')

当运行代码时,创建IO缓冲区时出现以下错误:

NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.
Traceback (most recent call last):
  File "/var/task/app.py", line 22, in lambda_handler
    buffer = BytesIO(zip_obj.get()["Body"].read())

zip 文件有一个主文件夹,有 4 个子文件夹,每个子文件夹中有 Excel 个需要读取和处理的文件。我注意到这适用于其中包含单个 excel 文件的 zip 文件,但是它不适用于其中包含文件的多个文件夹。创建 IO 缓冲区时我是否遗漏了一些操作?感谢任何帮助

问题不在 zip 文件中,问题在 s3_client.Object,它没有密钥 Body。可能URI不正确?