我当前的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设置配置中的问题。
答案 0 :(得分:0)
您正在呼叫im.save()
,它将图像保存到本地文件。您正在传递https://bucketname1.s3.eu-west-3.amazonaws.com/?encoding-type=url
作为要保存到的文件路径。这是S3中对象的URL,而不是库可以将文件保存到的本地路径。因此出现错误是因为它无法弄清楚如何将文件保存到该位置。
由于接下来要做的是使用boto3将文件上传到S3,并且您似乎试图将图像保留在内存中而不是将其写入磁盘,所以我敢肯定您可以删除{{ 1}}行。如果确实需要调用im.save()
,则必须在im.save()
文件夹中为其指定路径,因为该路径是Lambda执行环境中唯一具有写访问权限的文件夹。