簡體   English   中英

使用Lambda從base64上上傳s3

[英]s3 upload from base64 using Lambda

我有以下代碼:

import base64
imgdata = base64.b64decode(new_string)
filename = 'image.jpg'  # I assume you have a way of picking unique filenames
with open(filename, 'wb') as f:
    f.write(imgdata)

它將文件另存為jpg,我可以打開它。

如何將其上傳到s3存儲桶或任何其他服務,並返回URL安全性不是問題。

我試過了

    try:
    convertedFileString = fstring.replace('-', '+').replace('_','/').replace(',','=')
    imgdata = base64.b64decode(new_string)
     # I assume you have a way of picking unique filenames
    with open(filename, 'wb') as f:
        s3 = boto3.resource('s3')
        bucket = s3.Bucket('ag-grid')
        bucket.put_object(Key=filename, Body=f)
except Exception as e:
    return {
            'statusCode': 500,  
            'body': str(e)
    }

因此,我不確定是否了解您的問題。

我使用下面的代碼進行上傳,並且可以正常工作。

            # Filename
            new_name = '{}_{}_{}_{}_{}_{}x{}.{}'.format(cid, uid, id_service_order, id_question, uuid.uuid4(), 0, 0,
                                                        fileExtension)  # type: str
            key = "{}".format(new_name)
            # Let's use Amazon S3
            s3 = boto3.client("s3",
                              aws_access_key_id=aws_config.aws_access_key_id,
                              aws_secret_access_key=aws_config.aws_secret_access_key,
                              region_name=aws_config.aws_s3_region,
                              config=Config(signature_version='s3v4'))

            dec = base64.b64decode(img_base64)
            rs = s3.put_object(
                Bucket=aws_config.aws_s3_bucket,
                Key=key,
                ContentType=fileType,
                Body=dec,
                ACL='public-read'
            )
            print(rs)
            print(new_name)

這對您有幫助嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM