Skip to content

Instantly share code, notes, and snippets.

@tomfa
Last active February 14, 2023 15:08
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save tomfa/7bb519a34262353087a83712539eb6b0 to your computer and use it in GitHub Desktop.
Save tomfa/7bb519a34262353087a83712539eb6b0 to your computer and use it in GitHub Desktop.
AWS Lambda: Python store to S3
# This file is your Lambda function
import json
import boto3
def save_to_bucket(event, context):
AWS_BUCKET_NAME = 'my-bucket-name'
s3 = boto3.resource('s3')
bucket = s3.Bucket(AWS_BUCKET_NAME)
path = 'my-path-name.txt'
data = b'Here we have some data'
bucket.put_object(
ACL='public-read',
ContentType='application/json',
Key=path,
Body=data,
)
body = {
"uploaded": "true",
"bucket": AWS_BUCKET_NAME,
"path": path,
}
return {
"statusCode": 200,
"body": json.dumps(body)
}
# This file will generate an API endpoint for your handler,
# using serverless (see www.serverless.com)
#
# For full config options, check the docs:
# docs.serverless.com
#
service: hello-world-functions
provider:
name: aws
runtime: python3.6
region: eu-central-1
iamRoleStatements:
- Effect: Allow
Action:
- s3:*
Resource: "*"
functions:
helloworld:
handler: handler.save_to_bucket
events:
- http:
path: helloworld/upload_to_S3
method: get
cors: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment