ryanwinchester

ryanwinchester

Greetings fellow humans,

First, I’ll tell you why I’m not using pre-signed URLs: because this is for sensitive files, that I want to control access to at a much more granular level than signed URLs allow.

I’ve been looking at aws-elixir and ex_aws_s3.

For now, I’m planning to use cloud functions, and want to keep memory limits as low as possible.

So, my objective is to stream a file from S3, to the client, while keeping memory usage constant and not running out.

It looks like with ex_aws_s3 I can do:

ExAws.S3.download_file(..., :memory)
|> ExAws.stream!()
|> Enum.reduce_while(Plug.Conn.send_chunked(), &()...)

but I’m worried about not being able to control the memory usage it could be a problem?

Another option @philss mentioned in aws-elixir in this issue is using a range option, which I guess would give more control.

What do you fine elixir folks think I should do?

Showing Posts 1 to 10

evadne

evadne

You can use a pre-signed URL with a signature that has an insanely short period of validity. Sign the URL every time. Should be much simpler. Having traffic go through your host can create a massive headache when the time comes to pay your bills!

ryanwinchester

ryanwinchester OP

Another issue with the pre-signed URL is when using SSE-C (custom keys for server-side encryption) requires headers to be sent to AWS with the request, so you can’t just redirect them to the URL or give the link out (and also exposes the key to the downloader, although that’s not the end of the world).

I haven’t found a good way to initiate that in the browser without some janky web workers acting as a proxy or something, but my JS expertise is severely lacking these days. I’ve fallen so far behind on frontend stuff :grimacing:

evadne

evadne

Another idea

  1. Generate pre-signed URL but keep it server-side
  2. Serve a stream to the client
  3. When the client wants more data, you go and download a few megs from AWS and serve that down the stream

This way you can use whatever that reads chunks off HTTP and you can have more control over how big each chunk sent to the user should be, etc.


Also it should be possible to specify the key even in a pre-signed GET request for S3.

ryanwinchester

ryanwinchester OP

I’ll pay $200 USD to anybody who can tell me how to get a working pre-signed GET URL with SSE-C that requires no headers. :folded_hands: :folded_hands: :folded_hands: :folded_hands: I’ve wasted so much of my spare time on this already :frowning:

evadne

evadne

TBF…

ExAws.S3.download_file/4: Defaults to a concurrency of 8, chunk size of 1MB, and a timeout of 1 minute.

So you should be ok. The stream isn’t started until the customer starts downloading anyway.

If you want to avoid the server-side round-trip, then you will have to expose the headers and start the download on the client with some JavaScript. You can use XMLHttpRequest to arrange the headers and get a blob back, then make a Data URI out of the blob, and serve it to the user… this works as long as the file is not large.

Can try GitHub - eligrey/FileSaver.js: An HTML5 saveAs() FileSaver implementation · GitHub for the saving part too

ryanwinchester

ryanwinchester OP

I’m probably going to start with a cap of 1GB filesize.

Related to FileSaver, I was looking at GitHub - jimmywarting/StreamSaver.js: StreamSaver writes stream to the filesystem directly asynchronous · GitHub but thought that a service worker seemed a bit janky. Maybe it’s better than streaming through my own server, though.

evadne

evadne

The bottom line IMO is whether you MUST use SSE-C and if it is acceptable in your use case to expose the headers to the client.

ryanwinchester

ryanwinchester OP

  • [:check_mark:] [Re: SSE-C] It’s a must.
  • [:check_mark:] [Re: headers] It’s not ideal, but I think it’s acceptable in this case.
hauleth

hauleth

Not an ideal solution, but use Nginx as a reverse proxy for S3 and extract header values from the URL params (or any other part of the query string).

ryanwinchester

ryanwinchester OP

Streaming 2.4GB zip from S3 through locally running app, through browser:

Using

ExAws.S3.download_file/3
|> ExAws.stream!/1
|> Enum.reduce_while(Plug.Conn.send_chunked/3, &(...))

The memory usage wasn’t frightening, so might be okay, for now.

This is on a 6-core 2019 iMac with 32GB RAM. Not sure if there are any significant tradeoffs happening with memory usage if I go to a 1-2 vcpu environment… :thinking:

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews