Last updated 4 months ago
DigitalOcean Spaces is an object storage service offering a simple, scalable, and affordable way to store any data: audio, videos, images, backups, documents, and much more. It scales on-demand, and you pay only for what you use.
You can learn more about DigitalOcean Spaces here.
When using DigitalOcean Spaces, you may want to perform data-processing when an event occurs on your bucket to transform, analyze, or optimize your data. DigitalOcean does not provide the primitive to achieve this kind of operation.
Koyeb is a serverless data-processing platform that offers a simple, robust, and scalable way to deploy processing functions to perform data operations and manipulations in minutes regardless of the cloud service provide you use.
This guide demonstrates how to deploy a processing function on Koyeb that generates a thumbnail each time a new image file is uploaded on your DigitalOcean Spaces bucket.
To implement this guide and generate a thumbnail each time a file is uploaded to your DigitalOcean bucket, you need to follow these four steps:
To get started, you need to generate a new DigitalOcean Spaces access key to connect your DigitalOcean Spaces bucket to Koyeb. Connecting your DigitalOcean Spaces bucket to Koyeb is a mandatory step to benefit from the Koyeb processing engine.
To generate a new Spaces key pair, on the DigitalOcean control panel, click API in the left-side menu.
You land on the Applications & API page where you can manage your API tokens, and Spaces access keys. In the Space access key section, click the Generate New Key button.
A modal appears and prompts you to enter a Token Name. Give the Spaces access key a name you will remember what it is used for and click the check icon.
In the table, you now see your access key and secret key. Copy and save these values in a safe place. You will use them in the next step to connect your DigitalOcean Spaces bucket to Koyeb.
Now that you have generated a DigitalOcean Spaces key pair, you need to connect your DigitalOcean bucket to Koyeb. This operation allows Koyeb to access your DigitalOcean bucket and to store your images there.
On the Koyeb Control panel, click the Create button on the top right navigation bar and select Store. You will land on the Store creation page. In the list of cloud service providers, select DigitalOcean.
A form appears that asks for the information about the Spaces bucket you want to connect:
You created a Koyeb Store, which lets you access the content of your DigitalOcean Spaces bucket. Koyeb natively provides an S3-compatible API to let you interact with your Store using the S3 protocol to upload and manage your content.
Once your Store configuration is done; you can now create a Stack and deploy the function to generate a thumbnail each time a new image is uploaded to your DigitalOcean Spaces bucket.
On the top navigation bar, click the Create button and select Stack.
You land on the Stack creation page. Stack is a Koyeb resource used to implement data-processing logic. A Stack is composed of two core elements:
On the Stack creation view, click the Simple Koyeb Versioning button. This mode allows you to directly manage your Stack via the control panel and API instead of GitHub.
Then, give your Stack a name. Here I named my stackand click the Create Stack button.
You are now asked to create a new Stack revision. Stack revisions allow you to version your changes so you can rollback to a previous version in seconds and track your changes.
In the catalog function inputs, search for Image resize and select this function. A snippet appears. Replace the configuration variables with your settings.
1functions: 2 - name: image-resize 3 use: firstname.lastname@example.org 4 with: 5 STORE: #The store to watch to trigger the function and save the resized image. This parameter is required. 6 do-spaces-koyeb 7 IMAGE_RESIZE_WIDTH: #The desired image width. Set to auto if not defined. This parameter is optional. 8 150 9 IMAGE_RESIZE_PATH: #The path to store the new generated image. This parameter is optional. 10 resized-images/ 11 IMAGE_RESIZE_FORMAT: #The format of the new generated image. This parameter is optional. 12 jpeg 13
This configuration uses thecatalog app to convert all images to a width of 150px in the JPEG format and store the result in the folder.
The function will be triggered each time an image is uploaded to your DigitalOcean Store via the Koyeb S3-Compatible API.
It's now time to upload some images to your Store and see what happens.
On the Koyeb control panel, click Stores and select the Store you previously created.
Click the Upload button and upload some images. In a few seconds, a folderwill appear. For each image uploaded, you will receive the associated thumbnail in this folder.
In the folder, you should see your new file. Hover over the new file, and click Copy to copy the URL that appears. Enter this URL into a new tab to access and view your resized image.
In this guide, you saw how to implement a processing function to generate a thumbnail for each image uploaded to a Koyeb Store. This Store is connected to a DigitalOcean Spaces bucket and allows you to upload and manage content via the Koyeb S3-Compatible API to benefit from Koyeb's S3-Compatible event-driven processing features.
If you would like to follow this tutorial and use a different third-party cloud service provider than DigitalOcean Spaces, please read our documentation about how to connect a third-party store.