Skip to content

First tests - upload to B2 from the server seems ok with ExAws.S3#142

Description

@ndrean

You can set up a bucket in B2 quite simply. I did not do anything special. And it seems directly compatible with ExAws.S3馃帀

Screenshot 2023-10-13 at 16 26 52 Screenshot 2023-10-13 at 16 27 42 Screenshot 2023-10-13 at 16 28 17

In place of the S3 config, I did:

config :ex_aws, :s3,
  debug_requests: true,
  access_key_id: System.get_env("B2_KEY_ID"),
  secret_access_key: System.get_env("B2_KEY_SECRET"),
  host: s3.eu-central-003.backblazeb2.com,
  bucket: up-image

The key point is to use host and not pass the region in the config. Then ExAws will automatically read this config for you.

host = s3.<b2_region><dns_suffix>

To use this, I have a browser tab opened serving a form with a file input (but you could curl -F ....). On submit, I POST the form to a controller. In the Phoenix code, you can use directly ExAws.S3.Upload and this can do multipart with zero efforts, the "standard" code (without XML parsing).

def handle_b2(conn, params) do
...

path
|> ExAws.S3.Upload.stream_file()
|> ExAws.S3.upload("up-image"), URI.encode(filename),
  acl: :public_read,
  content_type: mime,
  content_disposition: "inline"
)
|> ExAws.request()
Screenshot 2023-10-13 at 16 31 19

This uploads the file. The details are:

Screenshot 2023-10-13 at 16 24 13

and it serves the uploaded file at: https://f003.backblazeb2.com/file/up-image/5ooil3d759vq8ng8hg0c.png

Seems slower than S3 but so far so good! 鉁岋笍

Next step: B2 -> Cloudfare CDN. For this, I need to create a domain on Cloudfare to cache the B2 bucket on the CDN.

https://www.backblaze.com/docs/cloud-storage-deliver-private-backblaze-b2-content-through-cloudflare-cdn

Public bucket: https://www.backblaze.com/docs/cloud-storage-deliver-public-backblaze-b2-content-through-cloudflare-cdn

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions