Skip to main content

ExAWS Elixir SDK

This guide assumes that you have followed the steps in the Getting Started guide, and have the access keys available.

You may continue to use the ExAWS SDK as you normally would, but with the endpoint set to https://fly.storage.tigris.dev.

This example reads the credentials from the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

ExAWS configuration

Dependencies

Add the dependencies to your mix.exs file:

  defp deps do
[
{:ex_aws, "~> 2.0"},
{:ex_aws_s3, "~> 2.0"},
{:poison, "~> 3.0"},
{:hackney, "~> 1.9"},
{:sweet_xml, "~> 0.6.6"},
{:jason, "~> 1.1"},
]
end

Development configuration

Now setup the configuration for ex_aws and ex_aws_s3 in your dev.exs file (or config/config.exs file):

import Config

# Configure S3 client for access to Tigris
config :ex_aws,
debug_requests: true,
json_codec: Jason,
access_key_id: {:system, "AWS_ACCESS_KEY_ID"},
secret_access_key: {:system, "AWS_SECRET_ACCESS_KEY"}

config :ex_aws, :s3,
scheme: "https://",
host: "fly.storage.tigris.dev",
region: "auto"

In the first config we configure :ex_aws, by setting the access_key_id and secret_access_key. In this case we use AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables to store the access keys we will use to access Tigris.

Then we configure the S3 API endpoint, which is "fly.storage.tigris.dev".

Runtime configuration

Now similar to above, let's add the configuration in runtime.exs file:

import Config

if config_env() == :prod do

# ....
# Configure S3 client for access to Tigris
config :ex_aws,
debug_requests: true,
json_codec: Jason,
access_key_id: {:system, "AWS_ACCESS_KEY_ID"},
secret_access_key: {:system, "AWS_SECRET_ACCESS_KEY"}

config :ex_aws, :s3,
scheme: "https://",
host: "fly.storage.tigris.dev",
region: "auto"

end

Example

# List all buckets
all_buckets = ExAws.S3.list_buckets() |> ExAws.request!()

IO.puts("Buckets:")

for bucket <- all_buckets.body.buckets do
IO.puts(bucket.name)
end

# List all objects in bucket tigris-example
bucket_name = "tigris-example"
all_objects = ExAws.S3.list_objects(bucket_name) |> ExAws.request!() |> get_in([:body, :contents])

IO.puts("Objects:")

for object <- all_objects do
IO.puts(object.key)
end

# Put bar.txt
key = "bar.txt"
body = "Hello, world!"

response = ExAws.S3.put_object(bucket_name, key, body) |> ExAws.request!()
assert response.status_code == 200

# Read back bar.txt
response = ExAws.S3.get_object(bucket_name, key) |> ExAws.request!()
assert response.status_code == 200
assert response.body == body

Using presigned URLs

Presigned URLs can be used with the ExAWS SDK as follows:

bucket_name = "tigris-example"
key = "bar_ex.txt"

{:ok, presigned_url} =
ExAws.Config.new(:s3) |> ExAws.S3.presigned_url(:get, bucket_name, key, expires_in: 300)

IO.puts("Presigned URL for GET:")
IO.puts(presigned_url)

{:ok, presigned_url} =
ExAws.Config.new(:s3) |> ExAws.S3.presigned_url(:put, bucket_name, key, expires_in: 300)

IO.puts("Presigned URL for PUT:")
IO.puts(presigned_url)