DEV Community

Cover image for Watermark Images Uploaded to your Backblaze B2 Bucket Automatically
Edouard Bonlieu for Koyeb

Posted on • Originally published at koyeb.com

Watermark Images Uploaded to your Backblaze B2 Bucket Automatically

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.

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 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:

Steps

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

  1. Generate Backblaze B2 Access Keys
  2. Connect your Backblaze B2 Bucket to Koyeb
  3. Create a Stack and Deploy the Watermark Function
  4. 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.
Click App Keys in the left-side menu.

Backblaze control panel

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

Backblaze control panel

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

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

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

In the list of cloud service providers, select Backblaze.

Koyeb control panel

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

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

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

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
Enter fullscreen mode Exit fullscreen mode

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

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.

Feel free to reach out to us over our Community Slack or here to let us know if you have questions about this feature or would like to suggest a new feature.

Top comments (0)