How to Upload Files To Bunnynet Storage

Posted by Dmitry

Updated on

pythonbunny_netstoragesnippet

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:

The Latest

Quick Python Intro to OpenAI Chat Completion Functions

Quick Python Intro to OpenAI Chat Completion Functions
pythonopenaichatgpt

In this brief article, I will underline the key points and present an illustrative code snippet demonstrating how to make an API call to Wikipedia using user input.

recv() failed (104: Connection reset by peer) while reading response header from upstream

uWSGI. recv() failed (104: Connection reset by peer) while reading response header from upstream
pythonuwsgierrorsnippet

Geolocate the Location of an IP Address With Cloudflare Workers and JavaScript

Geolocate the Location of an IP Address With Cloudflare Workers and JavaScript
javascriptcloudflaregeolocationworkers

Detect a visitor's country and city by IP address with Cloudflare Workers and JavaScript.

JavaScript Document Onload + TypeScript version

JavaScript Document Onload + TypeScript version
javascriptDOMsnippetbasicstypescript
🗃JavaScript Basics

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

JavaScript addEventListener

JavaScript addEventListener
javascriptDOMsnippetbasics
🗃JavaScript Basics

How to Create Jinja2 Filters in Flask

How to Create Jinja2 Filters in Flask.
pythonflaskjinja2snippet

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

How to Copy Text to Clipboard With Javascript

How to Copy Text to Clipboard With Javascript.
简体中文javascriptDOMbrowser

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.

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

Flask Boilerplate and Your Guide to Flask in 2023. With SQLAlchemy.
boilerplateopen sourceflask

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 Import CSV Files to PostgreSQL with Pgfutter

How to Import CSV Files to PostgreSQL with Pgfutter.
csvpostgresql

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

How to Upload Files to DigitalOcean Spaces with Python
digitaloceanpython

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