Search code examples
javascriptreact-nativeamazon-s3fetchaws-sdk-js

Amazon S3 serving file only to app user in react-native


I am building a social network in react-native / nodejs and using the S3 amazon service to handle the users personal photos (upload, serve)

But I can't seem to wrap my head around how to serve those images, my question is how do I serve those uploaded images to only the application users and not the whole world ?

At first I tried to explicitly fetch the image myself, this allowed me to directly put the S3 credentials, but it doesn't seem practical.

Is there a way to make every GET call made by an app authorized to fetch from my bucket ?


Solution

  • Thanks to @sergey I made it to what fits my needs the 'getSignedUrl' method.

    Here is the code that worked for me :

    import AWS from 'aws-sdk/dist/aws-sdk-react-native';
    const credentials = new AWS.Crendentials({ accessKeyId: '', secretAccessKey: ''})
    const s3 = new AWS.S3({ credentials, signatureVersion: 'v4', region: ''});
    // and there it is.
    const url = s3.getSignedUrl('getObject', { Bucket: 'your bucket name', Key: 'the filename'}).
    

    And now each time I loop through an array containing multiple references to my photos, during each loop I create for an item a single pre-signed url that I put in my component.