AWS Object Lambda 使用 PyPDF2 发送回加密的 PDF

AWS Object Lambda using PyPDF2 to send back encrypted PDF

我的 AWS OBJECT Lambda 函数通过 Object Lambda inputS3Url 获取未加密的 PDF。我想使用 PyPDF2 将其转换为加密的 PDF,然后通过 s3.write_get_object_response 发回。我该怎么做?

s3_url = object_get_context["inputS3Url"]

url=s3_url


response = requests.get(url)
my_raw_data = response.content

[将加密的 my_raw_data 保存到变量,以便它可以通过 S3.write_get_object_response 返回 - 如何?]

s3 = boto3.client('s3')
s3.write_get_object_response(
Body= [WHAT WOULD GO HERE?]
RequestRoute=request_route,
RequestToken=request_token)

文档找到你了! Encrypting PDFs and Streaming Data 是您所需要的(至少如果我没猜错的话;如果您想实现除在 S3 上获取 password-protected PDF 之外的其他目标,请告诉我)

未测试,但类似这样

from PyPDF2 import PdfReader, PdfWriter
from io import BytesIO

reader = PdfReader(BytesIO(my_raw_data))
writer = PdfWriter()

# Add all pages to the writer
for page in reader.pages:
    writer.add_page(page)

# Add a password to the new PDF
writer.encrypt("my-secret-password")

# Save the new PDF to a file
with BytesIO() as bytes_stream:
    writer.write(bytes_stream)
    bytes_stream.seek(0)
    s3 = boto3.client('s3')
    s3.write_get_object_response(
        Body=bytes_stream,
        RequestRoute=request_route,
        RequestToken=request_token
    )