Hello there,
I want to upload the files to the Cloudflare R2 with the liveview standard form. Has anybody done it before and could help me with the setup? Currently if I try to upload new file, the url seems to be working correctly, but I get 400 response from cloudflare, saying Bad Request. It seems like some authorization issues. To setup the standard image upload I mostly followed Chris Mccord deep dive to uploads, and liveview documentation. Cloudflare say R2 is compatible with S3 API, although I can’t find any examples online of setuping this, so this is how I’m doing it:
def params_with_image(socket, params) do
path =
socket
|> consume_uploaded_entries(:image, fn _meta, entry ->
{:ok, Path.join(@r2_url, filename(entry))}
end)
path = List.first(path)
IO.inspect(path)
Map.put(params, "image_upload", path)
end
efp presign_upload(entry, socket) do
uploads = socket.assigns.upload
config = %{
region: "auto",
access_key_id: System.fetch_env!("AWS_ACCESS_KEY_ID"),
secret_access_key: System.fetch_env!("AWS_SECRET_ACCESS_KEY")
}
{:ok, fields} =
SimpleS3Upload.sign_form_upload(config, @r2_bucket,
key: filename(entry),
content_type: entry.client_type,
max_file_size: uploads[entry.upload_config].max_file_size,
expires_in: :timer.hours(1)
)
meta = %{
uploader: "S3",
key: filename(entry),
url: @r2_url,
fields: fields}
{:ok, meta,