Voiced by Amazon Polly |
Introduction
AWS S3 (Simple Storage Service) is an Amazon Web Service (AWS) storage service. AWS S3 offers high scalability, security, data availability, and performance. We can store any data in an AWS S3 bucket. Also, we can organize the data by creating a folder(s) in a bucket. This blog will teach us the best way to access AWS S3 Objects.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Why is a Pre-Signed URL required?
AWS S3 is the most used storage service to store files. Most developers are publicly making the AWS S3 bucket available, but this approach is not recommended for security reasons.
- Permissions: AWS S3 Pre-Signed URL has the permission of the AWS IAM User or AWS Services. Pre-Signed URLs get all the access that the IAM user or AWS Services has on a specific AWS S3 bucket.
- Expiration Time: We can define the expiration time of the AWS S3 Pre-Signed URL while creating the URL. Once the expiration time passes, the Pre-Signed URL will automatically be expired.
- Resources: You can specify a file or bucket name while creating the Pre-Signed URL.
Steps to create a Pre-Signed URL using AWS SDK
Step 1: Create an IAM user.
- Search IAM services on the AWS console and select IAM service
- Select Users options in Access Management
- Select Add Users
- Enter your Username and select Programmatic Access as an access type. You can give any username but ensure you give programmatic access to that user.
- On the next step, select Attach Existing policies directly and click on Create a policy
- On the Create Policy screen. Select Amazon S3 as a service, Get Object as an action, and Amazon S3 bucket as a specific resource.
- Keep everything else as default, give the policy name, and click on Create a policy
- Select the policy for the IAM user you created in the previous steps. And at the end, you will receive that user’s access key, the secret key.
Note: Download that access key and secret key because this is the last time these credentials will be available to see or download.
Step 2: Generate a Pre-Signed URL using AWS-SDK
- Install AWS SDK in your project to generate a pre-signed URL
1 |
npm install aws-sdk |
- Import downloaded SDK into your project
1 |
var AWS = require(‘aws-sdk’); |
- Add the following code snippet to get a pre-signed URL
In the following code snippet, initialize the AWS by passing the access key, secret key, and region. After that, initialize the Amazon S3 object. At last, call the getSignedUrl function of the Amazon S3 object by passing the bucket name, file name, and expiry time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
var AWS = require(‘aws-sdk’); var credentials = { accessKeyId: xxxxx, secretAccessKey: xxxxx, }; AWS.config.update({ region: 'ap-south-1', credentials: credentials }); var s3 = new AWS.S3(); var presignedURL = s3.getSignedUrl( ‘getObject’, { Bucket: ‘mayyur’, Key: ‘sampleImage.jpg’, Expires: 3600 } ); |
It will provide a single URL accessible for a specified period, determined by the expiry time you pass. In our case, it will be valid for 3600 seconds. You can use this pre-signed URL to access the file, whatever you passed as a key.
Conclusion
By generating a pre-signed URL, you can temporarily access a file without making it publicly available, offering a secure and controlled method for granting time-limited file access. A pre-signed URL involves appending authentication information to the URL, such as expiration time, access permissions, and other pertinent details. This enables individuals possessing the pre-signed URL to access the file within the specified timeframe without compromising its security or exposing it to the public. This approach is valuable for sharing confidential or sensitive files with specifically authorized individuals or within a restricted timeframe, ensuring that only authorized parties can access the file during the designated period.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon EMR and many more.
FAQs
1. Can we add files using a pre-signed URL?
ANS: – Yes, we can upload the files using a pre-signed URL, for that we need to give write access in policy (i.e., PutObject).
2. Can we use pre-signed URLs to serve content on the website?
ANS: – Yes, we can use pre-signed URLs on the website. You might need to write a function that generates a pre-signed URL for a limited period.
WRITTEN BY Mayur Patel
Comments