Get a File through Http from a server

So Recently I was trying to upload Files specifically images to Amazon s3 which using Http and File modules seemed fairly straight forward and worked as expected. What I was not expecting is that I don’t seem to grasp how to achive the opossite which is to Get the file back from the S3 and present it using an img Html element. The img bucket in s3 is private So I guess I need to download it first using credentials but I can’t understend what is the proper way to do that and if tooling exist to make it easier such as in the Post case.

Do I get bytes from the body and then whith some decoder transform it to a file ? if so anybody has some recomendation of how to do that or any guide and or blog post would be really helpfull.

Or is there any way to just get a File record directly out of the Post body ? again any recommendation on the process to follow or any blog post would be really really helpfull.

Thank you all in advance.

1 Like

In theory you should be able to use File.decoder to get the File from the HTTP response and then use File.toUrl in order to get the base64 representation that you should be able to use directly in the src field of the img.

I haven’t tried this but it should work.

Problem is that there is not expectFile Method in the Http module and no Decoder to decode Bytes to File.

How about https://package.elm-lang.org/packages/elm/http/latest/Http#expectBytes ?

When searching expectBytes language:elm on github I have found this: https://github.com/jxxcarlson/filegrabber/blob/7c0430e6f5bfaf1a0beaf0b6f25c4030f875c14c/src/Main.elm#L87

If you’re using it an an image tag, you probably don’t want the actual bytes but instead some URL to the image. Since the bucket is private, you’ll need to generate some kind of pre-signed URL.

There is an API for this but the S3 documentation is really bad :sob:. Here’s the best I could find:

Ah, looks like this was answered here:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.