All Projects → mattwelke → upload-file-to-backblaze-b2-from-browser-example

mattwelke / upload-file-to-backblaze-b2-from-browser-example

Licence: Unlicense license
Demonstrates calling the b2_upload_file Backblaze B2 Cloud Storage API from a web browser using AJAX.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to upload-file-to-backblaze-b2-from-browser-example

angular-progress-http
[DEPRECATED] Use @angular/common/http instead
Stars: ✭ 43 (+53.57%)
Mutual labels:  xhr, ajax
Thwack
A tiny modern data fetching solution
Stars: ✭ 268 (+857.14%)
Mutual labels:  xhr, ajax
http
Tiny, embeddable HTTP client with simple API for the browser
Stars: ✭ 21 (-25%)
Mutual labels:  xhr, ajax
Xdomain
A pure JavaScript CORS alternative
Stars: ✭ 3,051 (+10796.43%)
Mutual labels:  xhr, cors
Ironwing
universal, framework agnostic, transport layer
Stars: ✭ 17 (-39.29%)
Mutual labels:  xhr, ajax
Rext
🎈A lightweight (< 5kb gzipped) and Promise-supported HTTP request library, for all browsers.
Stars: ✭ 14 (-50%)
Mutual labels:  xhr, ajax
electron-request
Zero-dependency, Lightweight HTTP request client for Electron or Node.js
Stars: ✭ 45 (+60.71%)
Mutual labels:  xhr, ajax
Resource Loader
A middleware-style generic resource loader built with web games in mind.
Stars: ✭ 364 (+1200%)
Mutual labels:  xhr, ajax
Atomic
A tiny, Promise-based vanilla JS Ajax/HTTP plugin with great browser support.
Stars: ✭ 526 (+1778.57%)
Mutual labels:  xhr, ajax
Xhr.js
🌎 xhr.js is a library(< 2Kb) to make AJAX/HTTP requests with XMLHttpRequest.
Stars: ✭ 12 (-57.14%)
Mutual labels:  xhr, ajax
Axios Tutorial
axios实例应用及源码剖析 - xhr篇 (走心教程)
Stars: ✭ 219 (+682.14%)
Mutual labels:  xhr, ajax
laravel-cors
Laravel cors
Stars: ✭ 19 (-32.14%)
Mutual labels:  cors
spring-demo-ajax
Example of AJAX with Spring Boot
Stars: ✭ 38 (+35.71%)
Mutual labels:  ajax
PageQueryBoss
A ProcessWire Module to build complex nested queries containing multipple fields and pages and return an array that can be parsed to JSON. This is usefull to fetch data for SPA and PWA.
Stars: ✭ 18 (-35.71%)
Mutual labels:  ajax
realestate
A simple real estate app build with MEAN( Angular, Node and mongoDb ) and MERN( React, Node and mongoDb )
Stars: ✭ 33 (+17.86%)
Mutual labels:  cors
earthquakes-mapper
"Earthquakes Mapper" was created using React with no backend persistence. Users can view earthquakes from around the world on a Google Map and filter based on specific times and earthquake magnitude. Earthquake data from USGS.
Stars: ✭ 14 (-50%)
Mutual labels:  ajax
aws
Object Pascal implementation for Amazon S3
Stars: ✭ 67 (+139.29%)
Mutual labels:  bucket
m4q
Small library for DOM manipulation and animation. This library similar to jQuery, but written more simply.
Stars: ✭ 30 (+7.14%)
Mutual labels:  ajax
qscamel
qscamel is a command line tool to migrate data between different endpoint efficiently.
Stars: ✭ 34 (+21.43%)
Mutual labels:  object-storage
ossperf
A lightweight tool for analyzing the performance and data integrity of object-based storage services
Stars: ✭ 67 (+139.29%)
Mutual labels:  object-storage

upload-file-to-backblaze-b2-from-browser-example

This example demonstrates using the "b2_upload_file" Backblaze B2 Cloud Storage API from a web browser to upload a file directly from a web browser to the B2 bucket, without the file contents having to go through an intermediate web server you control.

This is similar to using techniques with other cloud storage providers such as AWS's presigned URLs and GCP's signed URLs.

An intermediate web server is used, but only to authenticate the client, not to transmit the file contents to the B2 bucket. If the example were expanded to a real world use case, your own IAM system would control whether or not the web browser would be allowed to obtain the B2 token and perform the file upload, based on your own logic.

Components

The example has two components:

  1. An Express back end (in backend) to call the b2_get_upload_url without exposing B2 credentials to the browser.
  2. A front end JS app (in the root directory) to call the back end with XHR and then use the temporary upload credentials to upload a selected file from the browser. Uses webpack to bundle in crypto-js to perform SHA1 hashing. Builds into a JavaScript bundle in public.

Preparing Backblaze B2 bucket

Create a bucket and use the B2 CLI to apply custom CORS rules. The contents of the b2CorsRules.json file in this repo can be used as an example. The policy allows downloads and uploads from any origin. Note that the argument --corsRules must be the contents of the CORS policy, not a path to a JSON file on disk with the CORS policy:

b2 update-bucket --corsRules '[
    {
        "corsRuleName": "downloadFromAnyOriginWithUpload",
        "allowedOrigins": [
            "*"
        ],
        "allowedHeaders": [
            "authorization",
            "content-type",
            "x-bz-file-name",
            "x-bz-content-sha1"
        ],
        "allowedOperations": [
            "b2_download_file_by_id",
            "b2_download_file_by_name",
            "b2_upload_file",
            "b2_upload_part"
        ],
        "maxAgeSeconds": 3600
    }
]' yourBucketName allPublic

You'll know you've successfully applied your CORS policy if the B2 web UI shows the option "There are 'custom' rules in place" selected:

custom CORS rules for bucket screenshot

Running back end

Run npm install from the repo's backend directory.

Set the following environment variables:

  • B2_KEY_ID - The keyId for your B2 application key
  • B2_APPLICATION_KEY - Your B2 application key
  • B2_BUCKET_ID - The ID of the B2 bucket you're uploading files into.

If you're following this tutorial as a new Backblaze B2 user, and you created a bucket just for this test, you can get the bucket ID by looking at your list of buckets after logging in(https://secure.backblaze.com/b2_buckets.htm):

get_bucket_id

Run the app with npm start from the backend directory.

It can be helpful to step through an example line by line with a debugger instead of running the app this way. If you want to do this with VS Code, you can use a launch config like this:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "pwa-node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/backend/bin/www",
            "env": {
                "B2_KEY_ID": "<your_key_id>",
                "B2_APPLICATION_KEY": "<your_application_key>",
                "B2_BUCKET_ID": "<your_bucket_id>"
            }
        }
    ]
}

Running front end

Run npm install from the repo's root directory.

Run npm start to compile and host the front end app on http://localhost:3001. It will launch in your default browser.

Choose file and upload:

loaded screenshot

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].