【问题标题】:Why my AWS lambda function testing is giving me SSL error?为什么我的 AWS lambda 函数测试给了我 SSL 错误?
【发布时间】:2020-10-23 09:33:18
【问题描述】:

我当前的 Lambda_function 代码是:(它的目的是在上传时将图像大小调整为 600x400,然后保存)

import boto3
import os
import pathlib
from io import BytesIO
from PIL import Image


s3 = boto3.resource('s3')

def delete_this_bucket(name):
    bucket = s3.Bucket(name)
    for key in bucket.objects.all():
        try:
            key.delete()
            bucket.delete()
        except Exception as e:
            print("SOMETHING IS BROKEN !!")

def create_this_bucket(name, location):
    try:
        s3.create_bucket(
            Bucket=name,
            CreateBucketConfiguration={
                'LocationConstraint': location
            }
        )
    except Exception as e:
        print(e)

def upload_test_images(name):
    for each in os.listdir('./testimage'):
        try:
            file = os.path.abspath(each)
            s3.Bucket(name).upload_file(file, each)
        except Exception as e:
            print(e)

def copy_to_other_bucket(src, des, key):
    try:
        copy_source = {
            'Bucket': src,
            'Key': key
        }
        bucket = s3.Bucket(des)
        bucket.copy(copy_source, key)
    except Exception as e:
        print(e)


def resize_image(src_bucket, des_bucket):
    size = 600, 400
    bucket = s3.Bucket(src_bucket)
    in_mem_file = BytesIO()
    client = boto3.client('s3')

    for obj in bucket.objects.all():
        file_byte_string = client.get_object(Bucket=src_bucket, Key=obj.key)['Body'].read()
        im = Image.open(BytesIO(file_byte_string))

        im.thumbnail(size, Image.ANTIALIAS)
        # ISSUE : https://stackoverflow.com/questions/4228530/pil-thumbnail-is-rotating-my-image
        im.save(in_mem_file, format=im.format)
        in_mem_file.seek(0)

        response = client.put_object(
            Body=in_mem_file,
            Bucket=des_bucket,
            Key='resized_' + obj.key
        )

def lambda_handler(event, context):
    bucket = s3.Bucket('bucketname1')

    for obj in bucket.objects.all():
        copy_to_other_bucket(bucket, 'bucketname1', obj.key)

    resize_image(bucket.name, 'bucketname1')


    print(bucket)

它说我的 ssl 验证有错误。

"errorMessage": "SSL validation failed for https://bucketname1.s3.eu-west-3.amazonaws.com/?encoding-type=url [Errno 2] No such file or directory",
  "errorType": "SSLError",

谁能帮我解决问题?我认为这可能是代码问题或某些 AWS lambda 设置配置问题。

【问题讨论】:

    标签: python amazon-web-services ssl amazon-s3 aws-lambda


    【解决方案1】:

    您正在调用im.save(),它将图像保存到本地文件。您将 https://bucketname1.s3.eu-west-3.amazonaws.com/?encoding-type=url 作为保存它的文件路径传递。这是 S3 中对象的 URL,而不是库可以将文件保存到的本地路径。所以它给了你这个错误,因为它不知道如何将文件保存到那个位置。

    由于您接下来要做的是使用 boto3 将文件上传到 S3,并且您似乎试图将图像保存在内存中而不是将其写入磁盘,所以我很确定您可以删除 @987654323 @ 线。如果您确实需要调用 im.save(),那么您必须在 /tmp 文件夹中为其指定一个路径,因为这是 Lambda 执行环境中您拥有写入权限的唯一文件夹。

    【讨论】:

    • 如果我使用第二个存储桶作为临时存储桶怎么办?
    • S3 存储桶不是本地文件系统。它实际上根本不是一个文件系统,它是一个对象存储。 im.save() 方法正在尝试写入文件系统,并且没有内置支持与 S3 API 交互。
    • 我以前从未使用过 Python 图像库,但就像我在回答中所说的那样,如果您尝试保存文件,您应该能够简单地删除该行。您没有对保存的文件做任何事情,您只是使用内存中的图像内容。请尝试删除该行,如果仍有错误,请返回此处。
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2018-02-18
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 2019-09-17
    • 2023-01-07
    • 2019-05-17
    相关资源
    最近更新 更多