Process DigitalOcean Spaces Bucket Images to Generate Thumbnails
Tags: serverless, digitalocean, tutorial, productivity
Published: 2020-10-22 15:40:29

## Introduction

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](//www.digitalocean.com/products/spaces/).

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.

## Requirements

> To successfully follow and implement this tutorial, you need:
> - A [Koyeb account](//app.koyeb.com)
> - A [DigitalOcean account](//cloud.digitalocean.com/login) and an existing DigitalOcean Spaces bucket

## Steps

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:

1. [**Generate DigitalOcean Spaces Access Keys**](#generate-spaces-access-keys)
2. [**Connect your DigitalOcean Spaces Bucket to Koyeb**](#connect-your-digitalocean-spaces-bucket-to-koyeb)
3. [**Create a Stack and Deploy the Thumbnail Generator Function**](#create-a-stack-and-deploy-the-thumbnail-generator-function)
4. [**Upload a Few Images to the Bucket and View Results**](#upload-a-few-images-to-the-bucket-and-view-results)

## Generate Spaces Access Keys

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.

![DigitalOcean control panel](//assets.koyeb.com/tutorials/do-panel-api-hl.png)

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.

![DigitalOcean control panel](//assets.koyeb.com/tutorials/do-gen-new-spaces-token.png)

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.

![DigitalOcean control panel](//assets.koyeb.com/tutorials/do-spaces-token-generated.png)


## 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.

![Koyeb control panel](//assets.koyeb.com/tutorials/koyeb-panel-new-store-do.png)

A form appears that asks for the information about the Spaces bucket you want to connect:

![Koyeb control panel](//assets.koyeb.com/tutorials/koyeb-panel-top-menu-new-store-hl.png)


- Fill the **bucket** input with your DigitalOcean Spaces bucket name
- Enter the region your **bucket** is located
- Under Access_key, click the **Create new secret** button to securely add the access key you previously created with the name `doaccesskey` and the access key you previously saved as the value
- Select the **Secret** name you just created as value for the access key
- Under Secret_key, click the **Create new Secret** button to securely add the private key you previously created with the name `doprivatekey` and the private key you previously saved as value
- Select the **Secret** name you just created as value for the secret key
- Give the Koyeb Store a name, in my case `do-spaces-koyeb`
- Click the **Create Store** button

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.

> Note: To trigger your function when an event occurs in the DigitalOCean Spaces bucket, you need to use the Koyeb S3 API endpoint to upload your content instead of the DigitalOcean one.

## Create a Stack and Deploy the Image Resize Function

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:

- Event bus: To receive events from Store or different sources
- Functions: Python, Node.js, Docker containers, or ready-to-use catalog apps triggered on specific events

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 stack `do-image-processing` and 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.

```yaml
functions:
  - name: image-resize
    use: image-resize@1.0.0
    with:
      STORE: #The store to watch to trigger the function and save the resized image. This parameter is required.
        do-spaces-koyeb
      IMAGE_RESIZE_WIDTH: #The desired image width. Set to auto if not defined. This parameter is optional.
        150
      IMAGE_RESIZE_PATH: #The path to store the new generated image. This parameter is optional.
        resized-images/
      IMAGE_RESIZE_FORMAT: #The format of the new generated image. This parameter is optional.
        jpeg
```

This configuration uses the `image-resize@1.0.0` catalog app to convert all images to a width of 150px in the JPEG format and store the result in the `resized-images/` folder.

The function will be triggered each time an image is uploaded to your DigitalOcean Store via the Koyeb S3-Compatible API.

## Upload a Few Images to the Bucket and View Results

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 folder `resized-images` will appear. For each image uploaded, you will receive the associated thumbnail in this folder.

In the folder `resized-images`, 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.

# Conclusion

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.

Feel free to reach out to us over [our Community Slack](//slack.koyeb.com/) or [here](//koyeb.com/contact) to let us know if you have questions about this feature or suggestions for new features.