![](/img/trans.png)
[英]How can I use a Lambda function to call a Glue function (ETL) when a text file is loaded to an S3 bucket
[英]How can I scrape PDFs within a Lambda function and save them to an S3 bucket?
I'm trying to develop a simple lambda function that will scrape a pdf and save it to an s3 bucket given the url and the desired filename as input data. 我不断收到错误“只读文件系统”,我不确定是否必须更改存储桶权限,或者我是否缺少其他东西。我是 S3 和 Lambda 的新手,希望能提供任何帮助。
这是我的代码:
import urllib.request
import json
import boto3
def lambda_handler(event, context):
s3 = boto3.client('s3')
url = event['url']
filename = event['filename'] + ".pdf"
response = urllib.request.urlopen(url)
file = open(filename, 'w')
file.write(response.read())
s3.upload_fileobj(response.read(), 'sasbreports', filename)
file.close()
这是我的事件文件:
{
"url": "https://purpose-cms-preprod01.s3.amazonaws.com/wp-content/uploads/2022/03/09205150/FY21-NIKE-Impact-Report_SASB-Summary.pdf",
"filename": "nike"
}
当我测试 function 时,我收到了这个错误:
{
"errorMessage": "[Errno 30] Read-only file system: 'nike.pdf.pdf'",
"errorType": "OSError",
"requestId": "de0b23d3-1e62-482c-bdf8-e27e82251941",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 15, in lambda_handler\n file = open(filename + \".pdf\", 'w')\n"
]
}
AWS Lambda 函数只能写入/tmp/
目录。 所有其他目录都是只读的。
此外,在/tmp/
中存储的默认限制为 512MB,因此请确保在将文件上传到 S3 后删除文件,以防 Lambda 环境被重新用于未来执行的情况。
AWS Lambda 在/tmp
中的空间有限,这是唯一的可写位置。 如果没有适当的磁盘管理,写入此空间可能会很危险,因为此存储在多次执行中保持活动状态。 它可能导致与先前请求的文件共享饱和或意外。 与其在本地保存 PDF,不如直接将其写入 S3,这样不涉及文件系统:
import urllib.request
import json
import boto3
def lambda_handler(event, context):
s3 = boto3.client('s3')
url = event['url']
filename = event['filename']
response = urllib.request.urlopen(url)
s3.upload_fileobj(response.read(), 'sasbreports', filename)
顺便说一句:应根据您的用例删除.pdf
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.