abstractkitchen.com logo

How to Upload Files To Bunny.net Storage

Posted by Dmitry

Updated on

python bunny_net storage snippet

Bunny.net is a well-known CDN and storage among developers. It provides you with a lot of handy features like, for example, auto image resizing. It's fast and reliable. Here is my python snippet on how to upload a file to the storage.

You can use just requests, but I prefer unofficial Python library. So let's install it first.

pip install bunnycdnpython

Here is the entire code. I will describe it in details below.

from BunnyCDN.Storage import Storage


def upload_file(client, filename, storage_path, local_upload_file_path=None):
    return client.PutFile(
        filename,
        storage_path=storage_path,
        local_upload_file_path=local_upload_file_path
    )


# Configuration from the BunnyNet dashboard
# 'api_key'. It's your password from bunny.net storage API. Read section below.
# 'zone_name'. Name of your storage zone
# 'zone_region'. Region name. You can find it in bunny.net page "FTP & API Access" > Hostname.
# For example in "ny.storage.bunnycdn.com" "ny" is your zone_region.
bunny_storage_config = {
    "api_key": "",
    "zone_name": "",
    "zone_region": ""
}

bunny_storage_client = Storage(
    bunny_storage_config["api_key"],
    bunny_storage_config["zone_name"],
    bunny_storage_config["zone_region"]
)

# Let's upload image to the Storage
upload_file(
    bunny_storage_client,
    "article-og-image.jpeg",
    "/articles/bunny-net-file-upload/article-og-image.jpeg",
    "/home/abstractkitchen/images/"
)

As you can see, the function upload_file takes four arguments:

Here is another approach to organizing your code. I like it more. You can then add more wrappers to work with Bunny.net.

from BunnyCDN.Storage import Storage


class BunnyStorage:
    def __init__(self, api_key, zone_name, zone_region):
        self.storage = Storage(api_key, zone_name, zone_region)

    def upload_file(self, filename, storage_path, local_upload_file_path=None):
        return self.storage.PutFile(
            filename,
            storage_path=storage_path,
            local_upload_file_path=local_upload_file_path
        )

Then you can use it anywhere in your code just like that.

bunny_storage_client = BunnyStorage("api-key", "zone-name", "zone-region")

bunny_storage_client.upload_file(
    "article-og-image.jpeg",
    "/articles/bunny-net-file-upload/article-og-image.jpeg",
    "/home/abstractkitchen/images/")

How To Obtain BunnyNet Storage API-key

1. Login to your bunny.net account.

2. Go to Edge Storage page.

3. Open your storage zone page.

4. On the left sidebar click «FTP & API Access». You'll see something like this.

Bunny.net Storage API UI

Here «Password» is your api_key. «ny» is zone_region.

Handy links:

A Short Guide To The Chinese Coordinate System

Have your ever searched google maps china offset? Most people who was in China yes. Here is my story behind this question.

The Definitive Guide To Sitemaps With Python

open source python

Sitemaps are important. Especially for big websites. It is always a good idea to develop your website with SEO in mind. Unfortunately, most developers ignore this part.

How to Import CSV Files to PostgreSQL with Pgfutter

csv postgresql

Sometimes I need to upload large CSV files to PostgreSQL. CSV file might have hundreds of columns, that's why i want a tool that can do some magic for me.

How to Upload Files to DigitalOcean Spaces with Python

digitalocean python

Snippet on how to upload files to DigitalOcean Spaces with boto3 and python.

Flask Boilerplate and Your Guide to Flask in 2022. With SQLAlchemy.

boilerplate open source flask

Flask-Backbone is my take to create an initial structure and a set of rules, so most of my flask projects are easy to support.

How to Copy Text to Clipboard With Javascript

javascript DOM browser

Here is a short snippet on how to copy text to the clipboard with Javascript. Your visitors will thank you. Most likely not, but here we are.

How to Create Jinja2 Filters in Flask

python flask jinja2 snippet

In this post, I'll talk about filters. Jinja2 has a list of built-in filters, and Flask leverages them.

JavaScript Document Onload + TypeScript version

javascript DOM snippet basics typescript
🗃 JavaScript Basics

Universal code-snippet to know when HTML is ready for modern browsers.