在 AWS Lambda 上,Openpyxl 不跟踪图像
On AWS Lambda, Openpyxl doesn't keep track of the image
当我有一个带有图像的 model.xlsx
并且此代码在 windows 上运行完美时。 (将图像保留在 output.xlsx
中)
import openpyxl
wb = openpyxl.load_workbook('model.xlsx')
#doing some stuff on the wb
wb.save('output.xlsx')
现在,当我在我的 AWS Lambda 上执行此操作时,一切正常,但我在 output.xlsx
上没有图像。
没有出现错误消息。
import json
import openpyxl
from tempfile import NamedTemporaryFile
import boto3
import botocore
def lambda_handler(event, context):
s3_client = boto3.client('s3', aws_access_key_id='*****', aws_secret_access_key='*****')
wb = openpyxl.load_workbook('model.xlsx')
#doing some stuff on the wb
with NamedTemporaryFile() as tmp:
wb.save(tmp.name)
tmp.seek(0)
s3_client.upload_file(tmp.name, "my-bucket-name", "filename-in-the-bucket")
return {
'statusCode': 200,
'body': json.dumps("Hello World")
}
我应该为 AWS 买票吗?打开pyxl?为什么没有错误信息?
好的,我不确定 100%,但我假设像 pip3 install --target ./theholypath openpyxl
这样的导入可以工作,但会因棘手的依赖项(你没有并存储在其他地方的文件)而失败,就像它一样今天用 Pillow 失败了。
所以一些英雄为我们完成了工作,并将库完美地打包成我们称之为 layer 的东西。对于您需要的每个库,您可能会发现相应的层已经由好人构建。 (如果你幸运的话,并不是每个图书馆都有一层)。
以我为例,我添加了 2 层:
- arn:aws:lambda:eu-west-3:770693421928:layer:Klayers-python38-Pillow:8
- arn:aws:lambda:eu-west-3:770693421928:layer:Klayers-python38-openpyxl:5
而且...它有效!我的 output.xlsx
现在有来自 model.xlsx
的图像,您甚至不需要执行 pip install --target my/path my_lib_name
现在,您可以找到自己需要的层和实现方式here。
警告:
Some layer versions will have a expiry_date field. This is the date for when the layers > will be deleted.
In general, layers are scheduled for deletion 60 days after a new layer version has been > published for that package. If you use that latest version of a layer, you're guaranteed > at least 60 days before the layer is deleted.
All functions deployed with a layer will still work indefinitely, but you won't be able > to deploy new functions referencing a deleted layer version.
当我有一个带有图像的 model.xlsx
并且此代码在 windows 上运行完美时。 (将图像保留在 output.xlsx
中)
import openpyxl
wb = openpyxl.load_workbook('model.xlsx')
#doing some stuff on the wb
wb.save('output.xlsx')
现在,当我在我的 AWS Lambda 上执行此操作时,一切正常,但我在 output.xlsx
上没有图像。
没有出现错误消息。
import json
import openpyxl
from tempfile import NamedTemporaryFile
import boto3
import botocore
def lambda_handler(event, context):
s3_client = boto3.client('s3', aws_access_key_id='*****', aws_secret_access_key='*****')
wb = openpyxl.load_workbook('model.xlsx')
#doing some stuff on the wb
with NamedTemporaryFile() as tmp:
wb.save(tmp.name)
tmp.seek(0)
s3_client.upload_file(tmp.name, "my-bucket-name", "filename-in-the-bucket")
return {
'statusCode': 200,
'body': json.dumps("Hello World")
}
我应该为 AWS 买票吗?打开pyxl?为什么没有错误信息?
好的,我不确定 100%,但我假设像 pip3 install --target ./theholypath openpyxl
这样的导入可以工作,但会因棘手的依赖项(你没有并存储在其他地方的文件)而失败,就像它一样今天用 Pillow 失败了。
所以一些英雄为我们完成了工作,并将库完美地打包成我们称之为 layer 的东西。对于您需要的每个库,您可能会发现相应的层已经由好人构建。 (如果你幸运的话,并不是每个图书馆都有一层)。
以我为例,我添加了 2 层:
- arn:aws:lambda:eu-west-3:770693421928:layer:Klayers-python38-Pillow:8
- arn:aws:lambda:eu-west-3:770693421928:layer:Klayers-python38-openpyxl:5
而且...它有效!我的 output.xlsx
现在有来自 model.xlsx
的图像,您甚至不需要执行 pip install --target my/path my_lib_name
现在,您可以找到自己需要的层和实现方式here。
警告:
Some layer versions will have a expiry_date field. This is the date for when the layers > will be deleted.
In general, layers are scheduled for deletion 60 days after a new layer version has been > published for that package. If you use that latest version of a layer, you're guaranteed > at least 60 days before the layer is deleted.
All functions deployed with a layer will still work indefinitely, but you won't be able > to deploy new functions referencing a deleted layer version.