Search code examples
amazon-s3asp.net-core-webapiasp.net-core-2.1

Getting a file from Amazon S3 to client web app - should it happen via the Web API?


I'm creating an ASP .Net Core 2.1 Web API. The front end (being written in Angular) will consume this API, which is used for a number of things, one of which is saving and retrieving files (pictures, PDF and Word docs, etc.)

We are storing all these files on Amazon S3. I was watching a tutorial video (https://www.youtube.com/watch?v=eRUjPrMMhCc) where the guy shows how to create a bucket, as well as upload and download a file from Amazon S3 from an ASP .Net Core 2.0 Web API, which I thought was fantastic as it's exactly what I needed.

But then I realized that, although the uploading functionality could be useful, the downloading might not be. The reason being that, if the user requests a file (stored on Amazon S3) via the client web app, and this request goes to the API (as was my original intention), then the API would have to first download this file from S3 (which might take a few seconds) and then send it to the client (another few seconds). So the file is being transmitted twice, and therefore unnecessarily slowing down the process of getting a file from S3 to the client.

Is my thinking correct here? Would it be better if the Angular client retrieved the file directly from S3 instead of going via the API? In terms of speed?


Solution

  • Amazon SDK has a methods to handle all you scenarios the principe here is to get a signed URL from Amazon S3 using SDK and then passe it to your front end

      import * as AWS from "aws-sdk/global";
    
          AWS.config.update({
            region: env.bucketRegion,
        });
    
        let clientParams:any = {
            region: env.bucketRegion,
            apiVersion: '2006-03-01',
            params: {Bucket: env.rekognitionBucket}
        };
        if (environment.s3_endpoint) {
            clientParams.endpoint = env.s3_endpoint;
        }
    
        let s3 = new S3(clientParams);
    
       let url = s3.getSignedUrl('getObject', {
          Bucket: env.rekognitionBucket,
          Key: '1234.txt',
      });