Prerequisites
- Ensure the Runpod Python library is installed:
pip install runpod. - Have an image file named
image.pngin the Docker container’s working directory.
Python Code for S3 Uploads
Let’s break down the steps to upload an image to an S3 bucket using Python:-
Handler Function for S3 Upload: Here’s an example of a handler function that uploads
image.pngto an S3 bucket and returns the image URL: - Packaging Your Code: Follow the guidelines in Worker Image Creation for packaging and deployment.
Setting Environment Variables for S3
Using environment variables securely passes the necessary credentials and configurations to your serverless function:- Accessing Environment Variables Setting: In the template creation/editing interface of your pod, navigate to the bottom section where you can set environment variables.
-
Configuring S3 Variables: Set the following key variables for your S3 bucket:
BUCKET_ENDPOINT_URLBUCKET_ACCESS_KEY_IDBUCKET_SECRET_ACCESS_KEY
BUCKET_ENDPOINT_URL includes the bucket name. For example: https://your-bucket-name.nyc3.digitaloceanspaces.com | https://your-bucket-name.nyc3.digitaloceanspaces.com
Testing your API
Finally, test the serverless function to confirm that it successfully uploads images to your S3 bucket:-
Making a Request: Make a POST request to your API endpoint with the necessary headers and input data. Remember, the input must be a JSON item:
-
Checking the Output: Make a GET request to retrieve the job status and output. Here’s an example of how to do it:
The response should include the URL of the uploaded image on completion: