【问题标题】:Error uploading PDF-like object to s3 bucket from Streamlit从 Streamlit 将类似 PDF 的对象上传到 s3 存储桶时出错
【发布时间】:2022-11-19 00:17:20
【问题描述】:

我已经构建了一个将 PDF 作为输入的 streamlit 应用程序。一切完成后,我想将初始 pdf 文件保存/上传到 s3 存储桶以供将来检查。

st.markdown =('# Imdocker pull mysql/mysql-server:latestport your PDF file')

pdf = st.file_uploader(label='Drag the PDF file here. Limit 100MB')
if pdf is not None:
    text = TextExtraction.extract_text(pdf)

bluh bluh 脚本对 pdf 没有任何作用。

最后我有:

s3 = boto3.resource(
    service_name='s3',
    region_name='ams3',
    aws_access_key_id='5LVOTUJBAAJ2IIMGVBJV',
    aws_secret_access_key='4SwvDZyDCbcmxoup6BPLImYc4aSeWuGLKECRTdgIn0Y',
)

bucket_name = 'mirai-pdf-private-stage'
print(pdf)
print(type(pdf))
pdf.seek(0)
name = 'pdf_' + str(id) + '.pdf'
print(name)
s3.Bucket(bucket_name).upload_fileobj(pdf, 'pdf_storage', name)

得到错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 563, in _run_script
    exec(code, module.__dict__)
  File "/app/main.py", line 124, in <module>
    s3.Bucket(bucket_name).upload_fileobj(pdf, 'pdf_storage', name)
  File "/usr/local/lib/python3.9/site-packages/boto3/s3/inject.py", line 678, in bucket_upload_fileobj
    return self.meta.client.upload_fileobj(
  File "/usr/local/lib/python3.9/site-packages/boto3/s3/inject.py", line 629, in upload_fileobj
    future = manager.upload(
  File "/usr/local/lib/python3.9/site-packages/s3transfer/manager.py", line 321, in upload
    self._validate_all_known_args(extra_args, self.ALLOWED_UPLOAD_ARGS)
  File "/usr/local/lib/python3.9/site-packages/s3transfer/manager.py", line 500, in _validate_all_known_args
    raise ValueError(
ValueError: Invalid extra_args key 'p', must be one of: ACL, CacheControl, ChecksumAlgorithm, ContentDisposition, ContentEncoding, ContentLanguage, ContentType, ExpectedBucketOwner, Expires, GrantFullControl, GrantRead, GrantReadACP, GrantWriteACP, Metadata, ObjectLockLegalHoldStatus, ObjectLockMode, ObjectLockRetainUntilDate, RequestPayer, ServerSideEncryption, StorageClass, SSECustomerAlgorithm, SSECustomerKey, SSECustomerKeyMD5, SSEKMSKeyId, SSEKMSEncryptionContext, Tagging, WebsiteRedirectLocation

我在网上找不到任何类似或解决我的问题的东西。

我试着改变:

s3.Bucket(bucket_name).upload_fileobj(pdf, 'pdf_storage', name)

s3.Bucket(bucket_name).upload_fileobj(pdf, 'pdf_storage', name, extra_arg=None)

并得到意外的参数错误。

提前致谢!

【问题讨论】:

    标签: python pdf amazon-s3 streamlit


    【解决方案1】:

    看起来你在 Streamlit 论坛上发布了这个并且在那里得到了回答。分享下面的回复。

    如果您使用 boto3.client 而不是资源,并且 s3.upload_fileobj 而不是 s3.Bucket.upload_fileobj,它应该可以工作。

    import boto3
    import streamlit as st
    
    pdf = st.file_uploader(label="Drag the PDF file here. Limit 100MB")
    if pdf is not None:
        s3 = boto3.client(
            service_name="s3",
            region_name="xxx",
            aws_access_key_id="xxx",
            aws_secret_access_key="xxx",
        )
    
    id = 123
    bucket_name = "xxx"
    print(pdf)
    print(type(pdf))
    pdf.seek(0)
    name = "pdf_" + str(id) + ".pdf"
    print(name)
    s3.upload_fileobj(pdf, "pdf_storage", name)
    

    【讨论】:

      猜你喜欢
      • 2018-08-19
      • 2019-02-01
      • 2018-08-31
      • 2020-10-17
      • 2015-05-05
      • 2017-10-04
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      相关资源
      最近更新 更多