Home Posts Post Search Tag Search

LiveView 14 - Chapter 5: AWS S3 Part 2
Published on: 2025-12-19 Tags: elixir, Blog, Side Project, LiveView, Html/CSS, Phoenix, S3
  1. Make sure that new images have the same names as the previous ones to avoid orphaned images

                        This required that I add in some logic to the upload_s3_file so that it checks for the existence of a image_url before we upload.
                                defp upload_s3_file(%{"temp_image_path" => path} = params) do
                                    key =
                                    case Map.get(params, "image_url") do
                                        nil ->
                                        "#{s3_prefix()}/#{Ecto.UUID.generate()}/image"
    
                                        existing ->
                                        existing
                                    end
    
                                    {:ok, _resp} =
                                    ExAws.S3.put_object("pento-images", key, File.read!(path))
                                    |> ExAws.request()
    
                                    {:ok, key}
                                end
                        There was some work that was needed to get the right data to this function but as long as you have the image_url populated in the params before you get here it will work fine. 
    
                    12. Populate the .env variables with Dotenvy
                        add
                                {:dotenvy, "~> 1.0.0"}, to the deps
                                mix deps.get
                        then within runtime.exs add these lines
                                env_dir = Path.expand("./envs")

                                source!([
                                Path.absname(".env", env_dir),
                                Path.absname(".#{config_env()}.env", env_dir),
                                Path.absname(".#{config_env()}.overrides.env", env_dir)
                                ])

                                # Configure pento uploads
                                # See pento/lib/pento/uploads.ex for more information.
                                # It's recommended to use environment variables for sensitive information.
                                config :pento, Pento.Uploads,
                                bucket: "pento-images",
                                region: Dotenvy.env!("AWS_REGION", :string),
                                access_key_id: Dotenvy.env!("AWS_ACCESS_KEY_ID", :string),
                                secret_access_key: Dotenvy.env!("AWS_SECRET_ACCESS_KEY", :string)

                                # Configure ExAws
                                # See https://hexdocs.pm/ex_aws/ExAws.html for more information.
                                # Configure via environment variables or hardcoding below.
                                # It's recommended to use environment variables for sensitive information.
                                config :ex_aws,
                                access_key_id: env!("AWS_ACCESS_KEY_ID"),
                                secret_access_key: env!("AWS_SECRET_ACCESS_KEY"),
                                region: env!("AWS_REGION")