This repository contains a sample serverless function written in Python that converts a WAV audio file to MP3 format. The function performs the following actions:
- Downloads a WAV file from a specified DigitalOcean Spaces bucket.
- Converts the WAV file to MP3 format using
audioread
andlameenc
. - Uploads the converted MP3 file back to the Spaces bucket.
- Sends a notification to a specified endpoint (
config-proxy
) with details of the converted file.
You can deploy this function on DigitalOcean's App Platform as a Serverless Function component. For more information, refer to the DigitalOcean Functions Documentation.
- A DigitalOcean account.
- The DigitalOcean
doctl
CLI installed on your local machine. - Access to a DigitalOcean Spaces bucket.
- The following environment variables set up:
DO_SPACES_REGION
: Your Spaces region (e.g.,nyc3
,ams3
).DO_SPACES_BUCKET
: Your Spaces bucket name.DO_ACCESS_KEY
: Your Spaces access key ID.DO_SECRET_KEY
: Your Spaces secret access key.- Optional: Any additional environment variables required by your
config-proxy
endpoint.
# Clone this repository
git clone <your-repo-url>
cd <your-repo-directory>
Ensure that your requirements.txt
file includes the necessary dependencies:
audioread==2.1.9
boto3==1.26.60
lameenc==1.7.0
requests==2.28.2
Use the doctl
CLI to deploy your function. The --remote-build
flag ensures that the build and runtime environments match.
doctl serverless deploy <your-directory> --remote-build
Example:
doctl serverless deploy wav-to-mp3-converter --remote-build
Deployment Output:
Deploying 'wav-to-mp3-converter'
to namespace 'fn-...'
on host 'https://faas-...'
Submitted action 'convert' for remote building and deployment in runtime python:default
Processing of 'convert' is still running remotely ...
...
Deployed functions ('doctl sbx fn get <funcName> --url' for URL):
- convert
You can invoke the function by sending an HTTP POST
request. Replace <FUNCTION_URL>
with the URL of your deployed function. You can obtain the function URL using the doctl
command:
doctl serverless functions get convert --url
Sample curl
Command:
curl -X POST "<FUNCTION_URL>" \
-H "Content-Type: application/json" \
-H "Authorization: Basic <YOUR_BASE64_ENCODED_CREDENTIALS>" \
-d '{
"trackId": "your-track-id",
"audioFileWAVKey": "path/to/your/audio.wav"
}'
trackId
: (string, required) The unique identifier for the track.audioFileWAVKey
: (string, required) The key (path) to the WAV file in your Spaces bucket.
On successful execution, the function will:
- Convert the WAV file to MP3 format.
- Upload the MP3 file to your Spaces bucket.
- Notify the
config-proxy
endpoint with thetrackId
andmp3Key
.
Sample Successful Response:
{
"statusCode": 200,
"body": {
"message": "Conversion successful",
"mp3Key": "path/to/your/audio.mp3"
}
}
If an error occurs during execution, the function will return a 500
status code with an error message.
Sample Error Response:
{
"statusCode": 500,
"body": "Error message detailing what went wrong"
}
The function performs the following steps:
- Input Validation: Checks for the required
trackId
andaudioFileWAVKey
parameters. - Download WAV File: Uses
boto3
to download the WAV file from your Spaces bucket to a temporary location. - Convert to MP3: Utilizes
audioread
andlameenc
to convert the WAV file to MP3 format. - Upload MP3 File: Uploads the converted MP3 file back to the Spaces bucket.
- Cleanup: Deletes temporary files to free up resources.
- Notify
config-proxy
: Sends aPUT
request to the specified endpoint with thetrackId
andmp3Key
.
Ensure the following environment variables are set in your project.yml
or function configuration:
environment:
DO_SPACES_REGION: <your-spaces-region>
DO_SPACES_BUCKET: <your-spaces-bucket-name>
DO_ACCESS_KEY: <your-spaces-access-key>
DO_SECRET_KEY: <your-spaces-secret-key>
List the dependencies in your requirements.txt
file:
audioread==2.1.9
boto3==1.26.60
lameenc==1.7.0
requests==2.28.2
Your project.yml
should define the function and its runtime:
packages:
- name: wav-to-mp3
actions:
- name: convert
runtime: 'python:3.9'
main: __main__.main
environment:
DO_SPACES_REGION: ${DO_SPACES_REGION}
DO_SPACES_BUCKET: ${DO_SPACES_BUCKET}
DO_ACCESS_KEY: ${DO_ACCESS_KEY}
DO_SECRET_KEY: ${DO_SECRET_KEY}
Include a build.sh
script if you need to handle any build steps, especially for dependencies that require compilation:
#!/bin/bash
set -e
virtualenv --without-pip virtualenv
pip install -r requirements.txt --target virtualenv/lib/python3.9/site-packages
Invoke the function with test parameters:
doctl serverless functions invoke convert \
--param trackId=your-track-id \
--param audioFileWAVKey=path/to/your/audio.wav
- Create a new POST request to
<FUNCTION_URL>
. - Headers:
Content-Type
:application/json
Authorization
:Basic <YOUR_BASE64_ENCODED_CREDENTIALS>
- Body:
{ "trackId": "your-track-id", "audioFileWAVKey": "path/to/your/audio.wav" }
- Send the request and verify the response.
- Authentication: The function requires Basic Authentication using credentials provided by DigitalOcean. Ensure you include the correct
Authorization
header in your requests. - Timeouts: If processing large audio files, be mindful of function execution time limits. Optimize your code or adjust timeout settings as needed.
- Error Logging: Use
print
statements or logging to output debug information during development.
Contributions are welcome! Please submit a pull request or open an issue to discuss improvements or bug fixes.
Feel free to customize this README to suit your specific repository details, such as adding a link to your repository, adjusting the function name, or including additional information relevant to your use case.