简体   繁体   中英

To upload a file to amazon s3 using lambda

I used this python code to upload file to s3 bucket using lambda. Getting an error:

{
  "errorMessage": "Syntax error in module 'lambda_function': (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXX escape (lambda_function.py, line 10)",
  "errorType": "Runtime.UserCodeSyntaxError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\" Line 10\n        file_name= \"C:\\Users\\smanthriprag\\Pictures\\Screenshots\\s.jpeg\"\n"
  ]
}
import json
import boto3


from botocore.exceptions import ClientError


def lambda_handler(file_name, bucket, object_name=None):
    
    file_name= "C:\Users\smanthriprag\Pictures\Screenshots\s.jpeg"
    bucket= "serverlesswebapp0406"

    # If S3 object_name was not specified, use file_name
    if object_name is None:
        object_name = file_name

    # Upload the file
    s3_client = boto3.client('s3')
    try:
        response = s3_client.upload_file(file_name, bucket, object_name)
        print('Step 3: upload done')
    except ClientError as e:
        logging.error(e)
        return False
    return True['response']

An AWS Lambda function written in Python should use the following handler:

def lambda_handler(event, context):

If information is being passed into the Lambda function, it will be made available via the event . The contents of event depends upon how the Lambda function is triggered (eg triggered by S3, triggered by SQS, or run via an Invoke() command).

Your program has an incorrect definition for the handler function.

See: Lambda function handler in Python - AWS Lambda

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM