envixo

envixo

ExAws: HTTP ERROR: :nxdomain for URL: s3.amazonaws.com/logo/original-xx.png ATTEMPT: 10

I have an application in Phoenix 1.6.2 that uploads files to Amazon S3 using ex_aws_s3.

On localhost the upload works fine for the bucket, but in production the upload doesn’t work.

This error happens:

15:49:33.135 [warn] ExAws: HTTP ERROR: :nxdomain for URL: "https:/prd-envixo-bucket.s3.amazonaws.com/1/logo/original-549bac34-7c85-42e6-b2cd-cb3ce7f71fab.png" ATTEMPT: 10
15:49:33.136 [error] GenServer #PID<0.2635.0> terminating
** (ExAws.Error) ExAws Request Error!

{:error, :nxdomain}

    (ex_aws 2.2.9) lib/ex_aws.ex:87: ExAws.request!/2
    (my_app 0.1.0) lib/my_app_web/live/register_wizard/register_wizard_step2_live.ex:87: anonymous fn/4 in MyAppWeb.RegisterWizardStep2Live.consume_logo/2
    (phoenix_live_view 0.17.2) lib/phoenix_live_view/upload_channel.ex:20: Phoenix.LiveView.UploadChannel.consume/3

I’ve tried several different settings, but nothing works.

Looks like the ex_aws_s3 has a bug and generates the wrong URL.

My prod.secret.exs file:

config :ex_aws,
  bucket: System.fetch_env!("AWS_BUCKET"),
  cloud_front: System.fetch_env!("AWS_CLOUD_FRONT"),
  region: System.fetch_env!("AWS_REGION"),
  access_key_id: [System.fetch_env!("AWS_ACCESS_KEY_ID"), :instance_role],
  secret_access_key: [System.fetch_env!("AWS_SECRET_ACCESS_KEY"), :instance_role]

config :ex_aws, :s3, scheme: "https://"

And this is my code that uploads the file:

  @s3_bucket Application.fetch_env!(:ex_aws, :bucket)
  defp s3_url, do: "#{@s3_bucket}.s3.amazonaws.com"

  def consume_logo(socket, current_enterprise) do
    consume_uploaded_entries(socket, :logo, fn meta, entry ->
      # Delete old file if exists on S3
      if current_enterprise.logo_original_url do
        logo_path = file_s3_path(current_enterprise.logo_original_url)
        ExAws.S3.delete_object(s3_url(), logo_path) |> ExAws.request!()
      end

      if current_enterprise.logo_thumbnail_url do
        logo_path = file_s3_path(current_enterprise.logo_thumbnail_url)
        ExAws.S3.delete_object(s3_url(), logo_path) |> ExAws.request!()
      end

      # Upload new original file to S3
      original_url = Path.join(s3_url(), filename(entry, socket, :original))
      {:ok, original_contents} = File.read(meta.path)
      ExAws.S3.put_object(s3_url(), file_s3_path(original_url), original_contents) |> ExAws.request!()

      # Resize and upload thumbnail file to S3
      thumbnail_url = Path.join(s3_url(), filename(entry, socket, :thumbnail))
      thumbnail = Image.resize(meta.path, 189, 50)
      {:ok, thumbnail_contents} = File.read(thumbnail.path)
      ExAws.S3.put_object(s3_url(), file_s3_path(thumbnail_url), thumbnail_contents) |> ExAws.request!()

      # Delete temp files
      File.rm!(meta.path)
      File.rm!(thumbnail.path)

      # Return a new url image for original and thumbnail
      {original_url, thumbnail_url}
    end)
  end
  
  defp file_s3_path(file_url) do
    file_url |> String.replace("#{s3_url()}/", "")
  end

  defp filename(entry, socket, type) do
    [ext | _] = MIME.extensions(entry.client_type)
    "#{socket.assigns.current_enterprise.id}/logo/#{type}-#{entry.uuid}.#{ext}"
  end

Does anyone know how I can solve this problem?

Thank you for your help.

Most Liked

al2o3cr

al2o3cr

https:/prd-envixo... That lone / seems very sus.

The code in ex_aws_s3 that builds a URL doesn’t add/remove any slashes from the configured value of scheme.

Where Next?

Popular in Questions Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
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
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
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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

Other popular topics Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement