Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 调整大小后,Lambda将图像从一个存储桶复制到另一个存储桶_Amazon Web Services_Amazon S3_Aws Lambda - Fatal编程技术网

Amazon web services 调整大小后,Lambda将图像从一个存储桶复制到另一个存储桶

Amazon web services 调整大小后,Lambda将图像从一个存储桶复制到另一个存储桶,amazon-web-services,amazon-s3,aws-lambda,Amazon Web Services,Amazon S3,Aws Lambda,我在S3存储桶上接收图像。使用lambda函数,我想将图像调整为缩略图,并将缩略图复制到另一个s3存储桶中。代码如下: import json import boto3 import ast from urllib.request import urlopen import time from boto3.dynamodb.conditions import Key, Attr from PIL import Image s3_client=boto3.client('s3') s3_res

我在S3存储桶上接收图像。使用lambda函数,我想将图像调整为缩略图,并将缩略图复制到另一个s3存储桶中。代码如下:

import json
import boto3
import ast
from urllib.request import urlopen
import time
from boto3.dynamodb.conditions import Key, Attr
from PIL import Image

s3_client=boto3.client('s3')
s3_res = boto3.resource('s3')
def lambda_handler(event, context):
    client = boto3.resource("dynamodb")
    tnlBuck = s3_res.Bucket('aivuthumbnail')
    for record in event['Records']:
        bucket=record['s3']['bucket']['name']
        ikey = record['s3']['object']['key']
        params = {'Bucket': bucket, 'Key': ikey}
        proj = ikey.split('_')[0]
        outlet = ikey.split('_')[1]
        parameter = ikey.split('_')[2]
        dat = ikey.split('_')[3]
        table = client.Table("telescopeImageReceipt")
        table.put_item(Item={'image':ikey,'project':proj,'outlet':outlet,'parameter':parameter,'date':dat})
        url = s3_client.generate_presigned_url(ClientMethod='get_object', Params=params)
        with urlopen(url) as conn:
            image = Image.open(conn)
            MAX_SIZE = (100, 100) 
            image.thumbnail(MAX_SIZE)
            image.copy("Bucket":tnlBuck)
我已将最后一行更改为各种组合。但什么都不管用。lambda函数可以完全访问S3、Dynamodb和Cloudwatch日志

以下是我尝试的一些选项,并收到了错误消息:

Option Tried:  tnlBuck.copy(image, ikey)
Error : Expecting dictionary formatted: {"Bucket": bucket_name, "Key": key} but got <PIL.JpegImagePlugin.JpegImageFile image

Option Tried: s3_client.copy({"Bucket":tnlBuck, "Key":ikey})
Error:  TypeError: copy() missing 2 required positional arguments: 'Bucket' and ‘Key'

Option tried:  image.copy({"Bucket":tnlBuck, "Key":ikey})
Error:  TypeError: copy() takes 1 positional argument but 2 were given
尝试了选项:tnlBuck.copy(图像,ikey)
错误:应为字典格式:{“Bucket”:Bucket_name,“Key”:Key}但得到您需要使用S3 Bucket将图像复制到它,而不是PIL图像对象。 您的代码应更改为:

import json
import io
import boto3
import ast
from urllib.request import urlopen
import time
from boto3.dynamodb.conditions import Key, Attr
from PIL import Image

s3_client=boto3.client('s3')
s3_res = boto3.resource('s3')
def lambda_handler(event, context):
    client = boto3.resource("dynamodb")
    tnlBuck = s3_res.Bucket('aivuthumbnail')
    for record in event['Records']:
        bucket=record['s3']['bucket']['name']
        ikey = record['s3']['object']['key']
        params = {'Bucket': bucket, 'Key': ikey}
        proj = ikey.split('_')[0]
        outlet = ikey.split('_')[1]
        parameter = ikey.split('_')[2]
        dat = ikey.split('_')[3]
        table = client.Table("telescopeImageReceipt")
        table.put_item(Item={'image':ikey,'project':proj,'outlet':outlet,'parameter':parameter,'date':dat})
        url = s3_client.generate_presigned_url(ClientMethod='get_object', Params=params)
        with urlopen(url) as conn:
            image = Image.open(conn)
            MAX_SIZE = (100, 100) 
            image.thumbnail(MAX_SIZE)
            img_bytes = io.BytesIO()
            image.save(img_bytes, format='JPEG')
            img_bytes.seek(0)
            tnl_bucket.Object(ikey).put(Body=img_bytes.read())
您应该使用tnl_bucket从缩略图图像字节创建新对象

img_bytes = io.BytesIO()
image.save(img_bytes, format='JPEG')
img_bytes.seek(0)
tnl_bucket.Object(ikey).put(Body=img_bytes.read())

PIL可以保存到path或BytesIO上的文件。您需要返回以.seek(0)开头的流,以便可以从开始读取它以获取put方法的字节。

错误是什么?添加了错误消息:如果没有帮助,您可以在twitter上与我联系,我可以帮助您。@JanGiacomelli非常感谢……代码工作得非常好。我做了一些改变。添加了
导入io
库和2。将0的第一个索引传递到
img\u字节中。seek()
。再次感谢你。我可以接受答案。太好了。我今天晚些时候会写一个答案