best practice for streaming images in S3 to clients through a server

220

What you are doing will work, and it's probably the best option if you are optimising for getting something working quickly, w/o worrying too much about waste of server resources, unnecessary computation, and if you don't have scalability concerns.

However, if you're worrying about scalability and lower latency, as well as secure access to these image resources, you might want to improve your current architecture.

Once I get the paths, I use that to download all images to my server first and then send it to the client.

This part is the first part I would try to get rid of as you don't really need your backend to download these images, and stream them itself. However, it seems still necessary to control the access to resources based on who owns them. I would consider switching this to below setup to improve on latency, and spend less server resources to make this work:

  • Once I get the paths in your backend service, generate Presigned urls for s3 objects which will give your client temporary access to these resources (depending on your needs, you can adjust the time frame of how long you want a URL access to work).
  • Then, send these links to your client so that it can directly stream the URLs from S3, rather than your server becoming the middle man for this.

Once you have this setup working, I would try to consider using Amazon CloudFront to improve access to your objects though the CDN capabilities that CloudFront gives you, especially if your clients distributed in different geographical regions. AFA I can see, you can also make CloudFront work with presigned URLs.

Is this the right way of doing it? It seems that if the client accesses S3 directly, it'll be faster but I'm not sure if that is safe

Presigned URLs is your way of mitigating the uncontrolled access to your S3 objects. You probably need to worry about edge cases though (e.g. how the clients should act when their access to an S3 object has expired, so that users won't notice this, etc.). All of these are costs of making something working in scale, if you have that scalability concerns.

Share:
220
MoneyBall
Author by

MoneyBall

BY DAY: Alt-Rock Ninja Cowgirl at Verifidan Dynamics BY NIGHT: I write code and code rights for penalcoders.example.org, an awesome non-profit that will totally take your money at that link. My kids are cuter than yours. FOR FUN: C++ Jokes, Segway Roller Derby, NYT Sat. Crosswords (in Sharpie!), Ostrich Grooming. "If you see scary things, look for the helpers - you'll always see people helping." -Fred Rogers

Updated on December 20, 2022

Comments

  • MoneyBall
    MoneyBall over 1 year

    I am trying to find the best practice for streaming images from s3 to client's app.

    I created a grid-like layout using flutter on a mobile device (similar to instagram). How can my client access all its images?

    Here is my current setup: Client opens its profile screen (which contains the grid like layout for all images sorted by timestamp). This automatically requests all images from the server. My python3 backend server uses boto3 to access S3 and dynamodb tables. Dynamodb table has a list of all image paths client uploaded, sorted by timestamp. Once I get the paths, I use that to download all images to my server first and then send it to the client.

    Basically my server is the middleman downloading the sending the images back to the client. Is this the right way of doing it? It seems that if the client accesses S3 directly, it'll be faster but I'm not sure if that is safe. Plus I don't know how I can give clients access to S3 without giving them aws credentials...

    Any suggestions would be appreciated. Thank you in advance!

  • MoneyBall
    MoneyBall almost 4 years
    Wow thank you for such a detailed answer. I'll definitely look into presigned urls and cloudfront!