-
Notifications
You must be signed in to change notification settings - Fork 11
feat(functions): add python s3 form data upload example #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Python function to upload files to S3 | ||
|
||
This function does the following steps: | ||
|
||
* Read a file from an HTTP request form | ||
* Send the file to long-term storage with Glacier for S3 | ||
|
||
## Requirements | ||
|
||
This example uses the [Python API Framework](https://github.com/scaleway/serverless-api-project) to deploy the function. | ||
|
||
If needed, create a bucket and provide the following variables in your environment: | ||
|
||
```env | ||
export SCW_ACCESS_KEY = | ||
export SCW_SECRET_KEY = | ||
export BUCKET_NAME = | ||
``` | ||
|
||
## Running | ||
|
||
### Running locally | ||
|
||
This examples uses [Serverless Functions Python Framework](https://github.com/scaleway/serverless-functions-python) and can be executed locally: | ||
|
||
```console | ||
pip install -r requirements-dev.txt | ||
|
||
python app.py | ||
``` | ||
|
||
The upload endpoint allows you to upload files to Glacier via the `file` form-data key: | ||
|
||
```console | ||
echo -e 'Hello world!\n My contents will be stored in a bunker!' > myfile.dat | ||
curl -F file=@myfile.dat localhost:8080 | ||
``` | ||
|
||
### Deploying with the API Framework | ||
|
||
Deployment can be done with `scw_serverless`: | ||
|
||
```console | ||
scw_serverless deploy app.py | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from typing import TYPE_CHECKING | ||
import logging | ||
import os | ||
|
||
from scw_serverless import Serverless | ||
if TYPE_CHECKING: | ||
from scaleway_functions_python.framework.v1.hints import Context, Event, Response | ||
|
||
import boto3 | ||
from streaming_form_data import StreamingFormDataParser | ||
from streaming_form_data.targets import ValueTarget | ||
|
||
SCW_ACCESS_KEY = os.environ["SCW_ACCESS_KEY"] | ||
SCW_SECRET_KEY = os.environ["SCW_SECRET_KEY"] | ||
BUCKET_NAME = os.environ["BUCKET_NAME"] | ||
|
||
# Files will be uploaded to cold storage | ||
# See: https://www.scaleway.com/en/glacier-cold-storage/ | ||
STORAGE_CLASS = "GLACIER" | ||
|
||
app = Serverless( | ||
"s3-utilities", | ||
secret={ | ||
"SCW_ACCESS_KEY": SCW_ACCESS_KEY, | ||
"SCW_SECRET_KEY": SCW_SECRET_KEY, | ||
}, | ||
env={ | ||
"BUCKET_NAME": BUCKET_NAME, | ||
"PYTHONUNBUFFERED": "1", | ||
}, | ||
) | ||
|
||
s3 = boto3.resource( | ||
"s3", | ||
region_name="fr-par", | ||
use_ssl=True, | ||
endpoint_url="https://s3.fr-par.scw.cloud", | ||
aws_access_key_id=SCW_ACCESS_KEY, | ||
aws_secret_access_key=SCW_SECRET_KEY, | ||
) | ||
|
||
bucket = s3.Bucket(BUCKET_NAME) | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
|
||
@app.func() | ||
def upload(event: "Event", _context: "Context") -> "Response": | ||
"""Upload form data to S3 Glacier.""" | ||
|
||
headers = event["headers"] | ||
parser = StreamingFormDataParser(headers=headers) | ||
|
||
# Defined a target to handle files uploaded with the "file" key | ||
target = ValueTarget() | ||
parser.register("file", target) | ||
|
||
body: str = event["body"] | ||
parser.data_received(body.encode("utf-8")) | ||
|
||
if not (len(target.value) > 0 and target.multipart_filename): | ||
return {"statusCode": 400} | ||
|
||
name = target.multipart_filename | ||
|
||
logging.info("Uploading file %s to Glacier on %s", name, bucket.name) | ||
bucket.put_object(Key=name, Body=target.value, StorageClass=STORAGE_CLASS) | ||
|
||
return {"statusCode": 200, "body": f"Successfully uploaded {name} to bucket!"} | ||
|
||
|
||
if __name__ == "__main__": | ||
from scaleway_functions_python import local | ||
|
||
local.serve_handler(upload) |
3 changes: 3 additions & 0 deletions
3
functions/python-upload-file-s3-multipart/requirements-dev.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-r requirements.txt | ||
|
||
scaleway-functions-python~=0.1.0 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
scw-serverless~=0.0.4 | ||
boto3~=1.26 | ||
streaming_form_data~=1.11.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Very useful example.