Watermark Images Uploaded to your Backblaze B2 Bucket Automatically
Tags: backblaze, serverless, tutorial, productivity
Published: 2020-10-27 15:25:29


## Introduction

Backblaze B2 Cloud Storage is an S3-compatible cloud storage provider. At ¼ the price of AWS S3, Backblaze is a cost-effective option to store and manage data at scale.

You can learn more about Backblaze B2 Storage [here](//www.backblaze.com/b2/cloud-storage.html).

Koyeb is a serverless data-processing platform that offers a simple, robust, and scalable way to deploy and process functions to perform data operations and manipulations in minutes. Regardless of the cloud service provider you use, you can connect the data hosted by that provider to Koyeb and deploy functions that process your data quickly and at scale.
Combining Koyeb with Backblaze lets you enjoy one of the most affordable object storage platforms on the market while leveraging the most powerful platform to process data.

This guide demonstrates how to deploy a processing function on Koyeb that [generates a watermark](//koyeb.com/catalog/function/watermark-image) each time a new image file is uploaded on your Backblaze B2 bucket.

## Requirements

> To generate a watermark on your image files from your Backblaze B2 buckets, you need:
> - A [Koyeb account](//app.koyeb.com)
> - A [Backblaze account](//secure.backblaze.com/user_signin.htm) and an existing Backblaze B2 bucket
> - A JPEG or PNG image to be used as the watermark

## Steps

To  generate a watermark on each image file uploaded to your Backblaze B2 bucket, you need to:

1. [**Generate Backblaze B2 Access Keys**](#generate-backblaze-access-keys)
2. [**Connect your Backblaze B2 Bucket to Koyeb**](#connect-your-backblaze-b2-bucket-to-koyeb)
3. [**Create a Stack and Deploy the Watermark Function**](#create-a-stack-and-deploy-the-watermark-function)
4. [**Upload Images to the Bucket and View Results**](#upload-images-to-the-bucket-and-view-results)

## Generate Backblaze Acccess Keys
To be able to connect your Backblaze B2 bucket to Koyeb, you first need to generate a `Key ID` and an `Application Key`.

You can generate this Access Key in your [Backblaze Account](//secure.backblaze.com/user_signin.htm).
Click **App Keys** in the left-side menu.

![Backblaze control panel](//assets.koyeb.com/tutorials/backblaze-console-menu-app-keys.png)

Scroll down the page and click **Add a New Application Key**.

![Backblaze control panel](//assets.koyeb.com/tutorials/backblaze-click-add-application-key.png)

A modal appears:
- Enter the name for your Application Key.
- Decide which buckets this Application Key can access.
- Determine what level of access this Application Key will grant.

![Backblaze control panel](//assets.koyeb.com/tutorials/backblaze-add-application-key.png)

Once you've finished entering the relevant information for this Application Key, hit **Create New Key**.

You will now see your new Application Key, which is composed of your *keyID*, *keyName*, and *applicationKey*. Copy and save this information in a safe place. You will use these values in the next step: [connect your Backblaze B2 bucket to Koyeb.](#connect-your-backblaze-b2-bucket-to-koyeb)

## Connect your Backblaze B2 Bucket to Koyeb

Now that you have generated a Backblaze B2 key pair, you need to connect your Backblaze B2 bucket to Koyeb. This operation allows Koyeb to access your Backblaze B2 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 **Backblaze**.

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

In the list of cloud service providers, select **Backblaze**.

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

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

- Fill the **bucket** input with the name of your Backblaze bucket
- Enter the **region** your Backblaze bucket is located
- Click the **Create a new Secret** button under "Keyid" to securely add the keyID you previously created.
- Choose a **Name** for this Secret (such as `bbaccesskey`) and enter the *keyID* you previously saved as the **Value**
- Select the **Secret** name you just created as value for the access key
- Click the **Create a new Secret** button under the "Applicationkey" to securely add the private key you previously created.
- Choose a **Name** for this Secret (such as `bbprivatekey`) and enter the private *applicationKey* you previously saved as **Value**
- Select the **Secret** name you just created as value for the secret key
- Give the Koyeb Store a name, such as `bb-bucket-koyeb`
- Click the **Create Store** button

You have just successfully created a Koyeb Store that mirrors the content of your Backblaze B2 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 a function when an event occurs in your Backblaze bucket, you need to use the Koyeb endpoint to upload your content instead of the Backblaze one.

## Create a Stack and Deploy the Watermark Function

Once your Store configuration is done, you can create a Stack and deploy the function to generate a watermark each time a new image is uploaded to your Backblaze B2 bucket.

Look at the top navigation bar, click the **Create** button, and select **Stack**.

You land on the Stack creation page. A **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 a Store or different sources
- Functions: Python, Node.js, Docker containers, or ready-to-use catalog apps triggered on specific events

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

![Koyeb control panel](//assets.koyeb.com/tutorials/koyeb-panel-simple-versioning.png)

Give your Stack a name, for example: `bb-watermark-image`. Then click the **Create Stack** button.

You are now prompted 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 **Watermark image** and select this function.

![Koyeb control panel](//assets.koyeb.com/tutorials/koyeb-panel-watermark-deploy-revision.png)

A snippet appears. Replace the configuration variables with your settings.

```yaml
functions:
  - name: watermark-image
    use: watermark-image@1.0.1
    with:
      STORE: #The store to watch to trigger the function and save the generated watermark image. This parameter is required.
        bb-bucket-koyeb
      WATERMARK_IMAGE_URL: #The URL of the image to be used as the watermark. This parameter is required.
        https://assets.koyeb.com/illustrations/koyeb-logo-square.png
      WATERMARK_IMAGE_LOGO_WIDTH: #The size in percentage of the watermark compared to the source image. This parameter is optional.
        10
      WATERMARK_IMAGE_LOGO_MARGIN: #The margin applied to the watermark image. This parameter is optional.
        5
      WATERMARK_IMAGE_PATH: #The file where the watermarked image is stored. This parameter is optional.
        watermark-images/
      WATERMARK_IMAGE_LOGO_POSITION: #The placement of the watermark on the image. This parameter is optional.
        bottom-right
      WATERMARK_IMAGE_FORMAT: #The format of the watermarked image generated. This parameter is optional.
        jpeg
```

 In this example configuration, the `watermark-image@1.0.1` catalog function generates a watermark that:

- Is 10% the size of the image that the watermark is placed on
- Has a margin of 5 pixels

The generated watermarked image is stored in the `watermark-images/` folder of the `bb-bucket-koyeb` and is saved in the JPEG format.

When you are ready, write a revision message and click **Deploy Revision**.

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

## Upload 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, i.e. `bb-bucket-koyeb`.

Click the **Upload** button and upload some images. In a few seconds, a folder `watermark-images/` will appear. For each image uploaded, you will receive a watermarked version of that image in this folder.

In the folder `watermark-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 watermarked image.

An example image watermarked with the function:

![Koyeb Watermark](//assets.koyeb.com/tutorials/watermark-example-image.png)

## Conclusion

In this guide, you saw how to implement a processing function to generate a watermarked image for each image uploaded to a Koyeb Store that is connected to a Backblaze bucket. 
If you would like to follow this tutorial and use a different third-party cloud service provider than Backblaze B2, please read our documentation about how to [connect a third-party store](//koyeb.com/docs/stores/advanced).

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 would like to suggest a new feature.