Last active
February 14, 2023 15:08
-
-
Save tomfa/7bb519a34262353087a83712539eb6b0 to your computer and use it in GitHub Desktop.
AWS Lambda: Python store to S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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