Coder Social home page Coder Social logo

b2cdn's Introduction

B2CDN

A simple CDN object uploader for Backblaze B2 and other S3-compatible storage.

Logic

Since the announcement of the Cloudflare Bandwidth Alliance, outgoing traffic is now free from Backblaze B2 if your domain passes through the Cloudflare network. Since uploading is already free (minus the storage costs, of course), this makes for a perfect CDN.

However, there is no standard Backblaze B2 PHP SDK and using most of them resulted in uncaught exceptions. Enter Minio, an S3-compatible object storage gateway written in Go. It supports B2 as a gateway and can run through a single binary.

B2CDN leverages S3-compatible storage to talk to Backblaze B2's API. For instance, to manage an object upload:

B2CDN -> Minio -> Backblaze B2 -> B2CDN (result)
  1. The file gets retrieved by B2CDN.
  2. The Amazon PHP SDK uploads it to Minio, an S3-compatible gateway.
  3. Backblaze B2 stores the object in a public bucket.
  4. B2CDN returns a JSON response containing the status of the request and the URL to your file.

Your URL must pass through Cloudflare's network for traffic to be free. For instructions on how to do that, this article from Backblaze can help you get started.

Sample Response

{
    "status":201,
    "message":"File created.",
    "url":"https:\/\/cdn.undernet.space\/file\/undernet-cdn\/testfile.zip"
}

The proper HTTP response codes are also sent.

Usage

B2CDN works as a web-accessible API endpoint or a CLI-based utility. If B2CDN is ran through a webserver, it returns a JSON response. If it is ran using the CLI, it displays the progress on-demand.

On tests, this consumes 5 Class C API calls on Backblaze B2 for every upload. Be mindful of your API calls, as this may cost you unexpected charges.

Web API Deployment

Simply download/clone this package, run composer install to grab the dependencies and edit config.sample.php to your liking and rename it to config.php.

Then for your API requests, simply submit a GET request to the endpoint with these two parameters:

  • source - The source file. For example, https://demo.com/test.js
  • dest - The destination. This is the path relative to your bucket's root. For example, libraries/jquery/jquery.min.js. There is no need to put a forward slash at the beginning. Directories are automatically created on the remote.

CLI Usage

Simply download/clone this package, run compoesr install to grab the dependencies and edit config.sample.php to your liking and rename it to config.php.

Then, on your terminal, run:

php index.php <source> <destination>

Where:

  • source - The source file. For example, https://demo.com/test.js
  • dest - The destination. This is the path relative to your bucket's root. For example, libraries/jquery/jquery.min.js. There is no need to put a forward slash at the beginning. Directories are automatically created on the remote.

Configuration Options

  • access_key - Your Minio access key.
  • secret_key - Your Minio secret key.
  • bucket - Your bucket name on Backblaze B2. Make sure Minio has access to this.
  • endpoint - The URL to your Minio installation.
  • proxy_url - The URL of your CDN that passes through Cloudflare.

Other usages

You can also use this script for other functions, like simple object dumping on S3 or S3-compatible object storage. This is simply a wrapper for aws-php-sdk with an API endpoint service capability.

Demo

A demo will be up very soon.

License & Contributing

This script is written and distributed under the MIT Open Source license.

For contributions, simply fork, patch and submit a pull request. Make sure to follow the PSRs.

b2cdn's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.