sergio

sergio

DropzoneJS direct uploads to Backblaze B2 in Phoenix Liveview

Wrote this guide on how to integrate DropzoneJS with Phoenix Liveview. Hope it helps someone out!

https://sergiotapia.com/dropzonejs-direct-uploads-to-backblaze-b2-in-phoenix-liveview


Woah read that title again, what a mouthful.

I’ve spent the last five evenings after work trying to get this to work. I’ve cobbled together blog posts, forums posts, github issues, hell I’ve spelunked chinese stackoverflow content cloner websites trying to figure this out.

It was a pain in the ass!

I’m going to teach you how to presign a Backblaze B2 upload url using the b2_get_upload_url API method. We’re then going to use that upload url to directly upload a file to your Backblaze B2 bucket from the browser.

Sounds simple right? Christ…

I hope this guide saves a collective thousands of hours out there. If this article helped you leave a comment please, I always enjoy reading those.


Create your Backblaze B2 bucket and set up CORS.

I’m not going to teach you how to create a bucket. Once you have your bucket, you need to use the Backblaze CLI to set up CORS for it. You cannot set up the right CORS rules in the Backblaze UI.

I’m using Linux, so I downloaded the Backblaze CLI and here’s how I run it.

Make sure you have the right environment variables set before running the b2-linux binary.

export B2_APPLICATION_KEY_ID=""
export B2_APPLICATION_KEY=""
export B2_APPLICATION_KEY_NAME="my-awesome-bucket-name"

Then update the CORS rules. Note that you cannot pass in a filename like foobar.json you must pass in the content itself. Lots of people trip with this one.

# I'm assuming you have the `b2-linux` binary in your folder...
./b2-linux update-bucket --corsRules '[
    {
        "corsRuleName": "downloadFromAnyOriginWithUpload",
        "allowedOrigins": [
            "*"
        ],
        "allowedHeaders": [
            "*"
        ],
        "allowedOperations": [
            "b2_download_file_by_id",
            "b2_download_file_by_name",
            "b2_upload_file",
            "b2_upload_part"
        ],
        "maxAgeSeconds": 3600
    }
]' my-awesome-bucket-name allPublic

Now your bucket is ready to receive XHR from the browser.

Presigning URLs.

We’re going to create a backblaze.ex module to do the presigning. I’m using req you can use whatever HTTP library you want.

defmodule MyApp.Backblaze do
  @b2_application_key System.get_env("B2_APPLICATION_KEY")
  @b2_application_key_id System.get_env("B2_APPLICATION_KEY_ID")
  @b2_application_key_name System.get_env("B2_APPLICATION_KEY_NAME")
  @b2_bucket_id System.get_env("B2_BUCKET_ID")

  def get_upload_url() do
    %{api_url: api_url, authorization_token: authorization_token} =
      get_api_url_and_authorization_token()

    request =
      Req.post!("#{api_url}/b2api/v2/b2_get_upload_url",
        headers: [{"authorization", "#{authorization_token}"}],
        json: %{bucketId: @b2_bucket_id}
      )

    request.body
  end

  def get_api_url_and_authorization_token() do
    auth_base_64 =
      "#{@b2_application_key_id}:#{@b2_application_key}"
      |> Base.encode64()

    response =
      Req.get!("https://api.backblazeb2.com/b2api/v2/b2_authorize_account",
        headers: [{"authorization", "Basic #{auth_base_64}"}]
      )

    api_url = response.body["apiUrl"]
    authorization_token = response.body["authorizationToken"]

    %{api_url: api_url, authorization_token: authorization_token}
  end
end

We also need an endpoint we can hit from the frontend.

scope "/api", MyAppWeb do
  pipe_through :api

  post "/presign-upload-url", PresignController, :presign
end

And the controller.

defmodule MyAppWeb.PresignController do
  use MyAppWeb, :controller
  alias MyApp.Backblaze

  def presign(conn, _params) do
    upload_url = Backblaze.get_upload_url()
    json(conn, %{upload_url: upload_url})
  end
end

Install DropzoneJS

Add dropzone to your package.json and npm i from inside of your /assets folder.

{
  "devDependencies": {
    "@tailwindcss/forms": "^0.5.2"
  },
  "dependencies": {
    "alpinejs": "^3.10.3",
    "dropzone": "^6.0.0-beta.2"
  }
}

Install the DropzoneJS css

Go to app.css and the import for Dropzone’s styles.

@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";

 /* ADD THIS */
@import "dropzone/dist/dropzone.css";

Add DropzoneJS to your hooks.

In app.js you want to import a file we are going to create in the next step.

// We're going to create this file in the next step!
import dropzone from "./dropzone";

And add it to your hooks object.

let hooks = {};

// Add this...
hooks.Dropzone = dropzone;

let liveSocket = new LiveSocket("/live", Socket, {
  params: { _csrf_token: csrfToken },
  hooks: hooks,
  dom: {
    onBeforeElUpdated(from, to) {
      if (from._x_dataStack) {
        window.Alpine.clone(from, to);
      }
    },
  },
});

Prepare your DropzoneJS form

In whatever heex template you have, add a form.

<form
  class="dropzone dz-clickable"
  id="dropzone"
  phx-hook="Dropzone"
  phx-update="ignore"
  enctype="multipart/form-data"
>
</form>

Alright we’re done with all of the ceremony, now it’s time to actually upload. Are you ready? We’re almost there. Celebrate!

rambo.gif

Create your dropzone.js file

This should live in assets/js/dropzone.js.

import Dropzone from "dropzone";

export default {
  mounted() {
    let csrfToken = document
      .querySelector("meta[name='csrf-token']")
      .getAttribute("content");

    function initUpload(file) {
      return new Promise(function (resolve, reject) {
        fetch("/api/presign-upload-url", {
          headers: {
            "Content-Type": "application/json",
            "Accept": "application/json",
            "X-Requested-With": "XMLHttpRequest",
            "X-CSRF-Token": csrfToken
          },
          method: "post",
          credentials: "same-origin",
          body: JSON.stringify({
            key: file.name
          })
        })
          .then(function (response) {
            resolve(response.json());
          });
      });
    }


    let myDropzone = new Dropzone(this.el, {
      url: "#",
      method: "post",
      acceptedFiles: "image/*",
      autoProcessQueue: true,
      parallelUploads: 1,
      maxFilesize: 25, // Megabytes
      maxFiles: 5,
      uploadMultiple: true,
      transformFile: async function (file, done) {
        let initData = await initUpload(file);
        file.uploadUrl = initData.upload_url.uploadUrl;
        file.authorizationToken = initData.upload_url.authorizationToken;

        done(file);
      },
      init: function () {
        this.on("sending", function (file, xhr, formData) {
          xhr.open(myDropzone.options.method, file.uploadUrl);

          xhr.setRequestHeader("Content-Type", "b2/x-auto");
          xhr.setRequestHeader("Authorization", file.authorizationToken);
          // If you want to upload to a "folder" you just set it as part of the X-Bz-File-Name
          // for example you can set the username from a `window.username` variable you set.
          // xhr.setRequestHeader("X-Bz-File-Name", `yeyo/${Date.now()}-${encodeURI(file.name)}`);
          xhr.setRequestHeader("X-Bz-File-Name", `${Date.now()}-${encodeURI(file.name)}`);
          xhr.setRequestHeader("X-Bz-Content-Sha1", "do_not_verify");

          let _send = xhr.send
          xhr.send = function () {
            _send.call(xhr, file);
          }
        });

        this.on("success", function (file, response) {
          // The response has all the info you need to build the URL for your uploaded
          // file. Use it to set values on a hidden field for your `changeset` for example 
          console.log(response.fileId);
        });
      }
    });
  },
};

Guys… Dropzone is so vast and literally thousands of hours have gone into it by many different people but the documentation is very lacking. Herculean effort by the team and I owe them a lot of gratitude. It was painful for me to get this working. It’s very hard to discern what to do and many of the options contradict other options and make it behave strange.

Even the example this article has some weirdness: you can’t set parallelUploads: true otherwise only the last image actually gets uploaded. Weird right? But I’m happy with where this is today.

Hope this saved you at least an hour. If this didn’t work for you, please leave a comment and I’ll try to help out.

As next steps, throw up an imgproxy in front of your bucket for on the fly transformations. Then… throw up Bunny CDN in front of that for speed and lower your costs even more.

rambo.gif

Now get to uploading and save big bucks by using Backblaze B2.

Most Liked

mcrumm

mcrumm

Phoenix Core Team

Congrats on getting to the end on file uploads! :smiley: It can be a lot of work, which is why we designed LiveView uploads to remove as much complexity as possible–

  • allow_upload/3 enforces simple validations for accepted file types, max files/sizes, auto-upload, multiple uploads, etc. For direct-to-cloud uploads, the :external option is where you define a callback to generate a presigned URL– no additional controller necessary, and parallel uploads work out-of-the-box. :slight_smile:
  • live_file_input/2 can be wrapped in a container with a phx-drop-target attribute to handle drag and drop automatically.

I think the most custom code you need on the client-side is a custom external uploader. The guides are currently lacking an example of how to write your own uploader, so I linked to the S3 example which I think is closest to your needs. For clarity, the following covers a good portion of what you would need:

In your LiveView module:

def mount(_params, _session, socket) do
  {:ok,
   socket
   |> allow_upload(:files, accept: "image/*", max_entries: 5, max_file_size: 250000000, external: &presign_upload/2)}
end

defp presign_upload(entry, socket) do
  %{"uploadUrl" => url, "authorizationToken" => token} = MyApp.Backblaze.get_upload_url()

  {:ok, %{uploader: "Backblaze", url: url, token: token}, socket}
end

# phx-change: validate is a no-op but it is required for `live_file_input` validation.
def handle_event("validate", _, socket) do
  {:noreply, socket}
end

# phx-submit: save consumes the completed entries.
def handle_event("save", _, socket) do
  _results = 
    consume_uploaded_entries(socket, :files, fn %{} = _info, _entry ->
      # return {:ok, term()} to consume (complete) the entry.
      {:ok, nil}
    end)

  {:noreply, socket}
end

def render(assigns) do
  ~H"""
    <div class="container" phx-drop-target={@uploads.files.ref}>
      ...
      <form phx-change="validate">
        <%= live_file_input @uploads.files %>
        <button type="submit">Upload</button>
      </form>
    </div>
  """
end

In your Javascript, for instance in app.js:

let Uploaders = {}

Uploaders.Backblaze = function(entries, onViewError){
  entries.forEach(entry => {
    let { file, meta: { url, token } } = entry

    let xhr = new XMLHttpRequest()
    xhr.open("POST", url);

    xhr.setRequestHeader("Content-Type", "b2/x-auto");
    xhr.setRequestHeader("Authorization", token);
    // If you want to upload to a "folder" you just set it as part of the X-Bz-File-Name
    // for example you can set the username from a `window.username` variable you set.
    // xhr.setRequestHeader("X-Bz-File-Name", `yeyo/${Date.now()}-${encodeURI(file.name)}`);
    xhr.setRequestHeader("X-Bz-File-Name", `${Date.now()}-${encodeURI(file.name)}`);
    xhr.setRequestHeader("X-Bz-Content-Sha1", "do_not_verify");

    onViewError(() => xhr.abort())
    xhr.onload = () => xhr.status === 204 ? entry.progress(100) : entry.error()
    xhr.onerror = () => entry.error()
    xhr.upload.addEventListener("progress", (event) => {
      if(event.lengthComputable){
        let percent = Math.round((event.loaded / event.total) * 100)
        if(percent < 100){ entry.progress(percent) }
      }
    })

    let _send = xhr.send
    xhr.send = function () {
      _send.call(xhr, file);
    }
  })
}

let liveSocket = new LiveSocket("/live", Socket, {
  uploaders: Uploaders,
  params: {_csrf_token: csrfToken}
})

Let me know what you think of this approach, and if you have any questions I will do my best to answer them :slight_smile: Thanks for reading!

Where Next?

Popular in Guides/Tuts Top

kuon
I have a page with a large state that is loaded from DB, let’s call that “data”. I have a root live view that load “data” on mount and r...
New
anuragg
Hi everyone! I’m the founder of Render, a new cloud provider with native support for Elixir. When we launched Elixir support the most po...
New
nelsonic
Complete beginners Todo List Tutorial in Phoenix 1.5.3 (latest and greatest): It’s a feature complete TodoMVC clone with 0% JavaScri...
New
hauleth
Some time ago someone suggested me to write article about how I have configured my Vim to work with Elixir, now there it is: https://med...
New
jshprentz
Geoffrey Lessel’s 2019 book, Phoenix in Action, was written for Phoenix 1.4. I found that the book’s code examples did not match the cur...
New
caspg
Hi everyone, I recently implemented a real-time search feature in a Phoenix application using LiveView and Tailwind, and I wanted to sha...
New
stryrckt
I’m excited about the new LiveComponents feature in LiveView, but I haven’t seen much written on it, so I decided to write a couple of ar...
New
fuelen
Hi all! Just want to share a small code snippet which allows writing CASE expressions using macro which is similar to cond. Here is an ...
New
anuragg
We finally have a Mix clustering guide to go with Phoenix deployment with Mix Releases. Deploy an Elixir Cluster with Mix Releases and l...
New
AstonJ
With a few questions relating to this recently, I wonder if it might help having a thread where we share how we’ve set up Elixir/Erlang/P...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

We're in Beta

About us Mission Statement