DEV Community

kode eazy
kode eazy

Posted on • Originally published at kodeazy.com

How to access private s3 files using URI

In AWS S3 bucket is mainly used to store files.
To access S3 bucket files follow below steps.
Provide access configuration using AWS.config() method as below.

AWS.config.update({
region: "us-east-2",
signatureVersion: 'v4',
credentials: new AWS.Credentials(AccessKey, AccesSecreteKey, token)
});
Enter fullscreen mode Exit fullscreen mode

Later intitialize s3 variable using new AWS.S3()

var s3 = new AWS.S3();
Enter fullscreen mode Exit fullscreen mode

To get access URI of the file we use getSignedUrl() method of s3 bucket Here we have three parameters

Bucket:Name of the Bucket
Key: Path inside the Bucket
Expires: No of seconds the required to enable the URI(maximum 1 week)

var presignedURI = s3.getSignedUrl('getObject', {
   Bucket: "Bucket_Name",
   Key: "FIle_Path",
   Expires: seconds
});
console.log(presignedURI);
Enter fullscreen mode Exit fullscreen mode

Here presignedURI variable provides the URL,we can access the file using this URL

Top comments (0)