yanniskatsaros

yanniskatsaros

Hello!

I am working on a project and was hoping to use Backblaze B2 (as an alternative to S3) due to its low cost and generous free egress. I am hoping to get recommendations from anyone that has similar experience using B2 in Elixir. Which client library did you use? Did you use one of the dedicated Elixir clients, or did you use the S3-compatible API with one of the Elixir AWS clients? If so, how did you make your decision and what has your experience been?

Most of the dedicated B2 Elixir clients that I could find appear to be relatively old and it concerns me adopting them in case I run into any issues. These are the ones I found:

Conversely, if you recommend the S3-compatible API, which Elixir S3 client would you recommend? Here are two that I found, but are they are any specific libraries you have experience using and recommend?

Thanks in advance!

Showing Posts 1 to 8

D4no0

D4no0

For file upload, I would definitely use ex_aws_s3, I have used it in the past with backblaze successfully on a project, you really don’t want to deal with things like large file uploads on your own and the s3 library works very well.

As for other backblaze specific endpoints, what I personally did was to just use a customized HTTPoison client and implement authentication and the few APIs I needed from their official documentation, it is very fast and easy to do as their API documentation is pretty good.

ragamuf

ragamuf

Also recommend rolling your own for the parts that are simple if you are concern about outdated libraries. Check out this article on Small Development Kits (SDK) from Dashbit if you need inspiration. SDKs with Req: Stripe - Dashbit Blog

wojtekmach

wojtekmach

Hex Core Team

Thanks for the mention. I tried Req/ReqS3 with a few S3-compatible services but not yet B2. If anyone runs into any problems please open up an issue!

yanniskatsaros

yanniskatsaros OP

Thank you for the recommendations! It’s comforting to hear that you’ve successfully used Backblaze B2 with the ex_aws_s3 client. I may give the recommendation from @ragamuf and @wojtekmach below a shot as well since the functionality I currently need is only limited to get_object and put_object. However if that doesn’t pan out, ex_aws_s3 will be my first choice. Thanks!

yanniskatsaros

yanniskatsaros OP

Thanks for the recommendation! I actually saw this article but didn’t realize part II had been published specifically about S3-compatible services. I will definitely give this a shot since my current requirements are only limited to put_object and get_object.

yanniskatsaros

yanniskatsaros OP

I am planning on following your recommendations in the article specifically for Backblaze B2 - will definitely let you know how it goes. Thanks for writing the article series!

wojtekmach

wojtekmach

Hex Core Team

Oh, there is one consideration for Backblaze in particular. If you’d upload huge files, instead of doing it in a single put_object (an HTTP PUT) you may consider doing multipart uploads, ExAws.S3.initiate_multipart_upload/2 and friends. These are not available in Req/ReqS3 and there are no plans at the moment.

yanniskatsaros

yanniskatsaros OP

@ragamuf and @wojtekmach I ended up implementing a small and lightweight B2 client using the advice in the article. This is only for my own use, so there is room for improvement (feel free to add any suggestions/feedback!) but here is what the end result looks like:

# in Livebook (or your `mix.exs`)
Mix.install([
  {:req, "~> 0.5.6"},
  {:typed_struct, "~> 0.3.0"}
])

defmodule B2.Client do
  use TypedStruct

  typedstruct do
    field :app_key_id, String.t(), enforce: true
    field :app_key, String.t(), enforce: true
    field :endpoint_url, String.t(), enforce: true
    field :bucket, String.t(), enforce: true
  end
end

defmodule B2 do
  defp new(%B2.Client{} = client, options) when is_list(options) do
    Req.new(
      # https://hexdocs.pm/req/Req.Steps.html#put_aws_sigv4/1
      aws_sigv4: [
        service: :s3,
        access_key_id: client.app_key_id,
        secret_access_key: client.app_key
      ],
      base_url: "#{client.endpoint_url}/#{client.bucket}"
      # retry: :transient
    )
    |> Req.merge(options)
  end

  def client(app_key_id, app_key, endpoint_url, bucket) do
    %B2.Client{
      app_key_id: app_key_id,
      app_key: app_key,
      endpoint_url: endpoint_url,
      bucket: bucket
    }
  end

  def get_object(%B2.Client{} = client, key, options \\ []) when is_binary(key) do
    req = new(client, options)
      
    case Req.get(req, url: key) do
      {:ok, %{status: 200, body: body}} -> body
      {:ok, %{status: 404}} -> nil
      error -> error
    end
  end

  def put_object(%B2.Client{} = client, key, object, options \\ [])
    when is_binary(key) and is_binary(object) do
    req =
      new(client, options)
      # TODO: properly set the content MIME type
      # https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
      # |> Req.Request.put_header("Content-Type", "text/csv")
    
    case Req.put(req, url: key, body: object) do
      {:ok, %{status: 200}} -> :ok
      {:ok, %{status: n} = resp} when n > 300 -> {:error, resp}
      error -> error
    end
  end
end

And here is some example usage:

# configure the client using the Application environment or environment variables etc

# in this example I am using LiveBook secrets
b2_app_key_id = System.get_env("LB_B2_APP_KEY_ID")
b2_app_key = System.get_env("LB_B2_APP_KEY")

# make sure to replace your <region>
endpoint_url = "https://s3.<region>.backblazeb2.com"
# make sure to replace your <bucket>
bucket = "<bucket>"

client = B2.client(b2_app_key_id, b2_app_key, endpoint_url, bucket)

# write a file
contents = """
col_1,col_2,col_3
A1,B1,C1
A2,B2,C2
A3,B3,C3
A4,B4,C3
"""
:ok = client |> B2.put_object("example.csv", contents)

# read a file
object = client |> B2.get_object("example.csv")

Thanks again to everyone that provided recommendations, I really appreciate the feedback :pray:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews