Use Presigned URL to upload files into AWS S3

In this blog post, I will be walking through the steps as to how we can utilize the presigned url feature to upload files into AWS S3. Serverless will be used to spin up the necessary AWS resources for this post.Why do we need a Presigned URL in the fi…


This content originally appeared on Level Up Coding - Medium and was authored by Kulasangar Gowrisangar

In this blog post, I will be walking through the steps as to how we can utilize the presigned url feature to upload files into AWS S3. Serverless will be used to spin up the necessary AWS resources for this post.

Why do we need a Presigned URL in the first place?

Presigned URL can be used in an instance such as the customer/user wants to upload a file into an S3 bucket, of which he/she doesn’t have access privileges to do so. Hence this mechanism can be used as a secured way of allowing unauthorized users to perform upload/download, into or from S3. This releases the burden from the user’s perspective of having the AWS Credentials (accesskey + secretkey) in place to make the request. Presigned URL also comes with an expiration date and time, hence it can be used more than once till it burns out.

Prerequisites:

AWS CLI and Serverless must be configured. You can follow this if you haven’t set it up already.

AWS Resources that we’ll need:

  • an S3 Bucket
  • an AWS Lambda Function
  • an API Gateway endpoint

Workflow

User Retrieving the Presigned URL and uploading/downloading files into or from S3

Steps

  1. Create the nodejs lambda function to generate the presigned url by using the S3 API. You can pick any language you want, I’ve used nodejs for testing purposes.

2. Create a serverless template to provision all the necessary AWS resources (S3, Lambda Function and API Gateway) for our testing.

3. Deploy the serverless template into your AWS environment by going into the template location and executing the following command:

sls deploy

4. Verify whether the resources have been provisioned:

API Gateway
Lambda Function
S3 bucket

5. Make a GET request to the created API endpoint to retrieve the presigned url using Postman or any other HTTP client:

You’ll be able to see the returned presigned url as the response if the request was success.

6. The URL above can be used to upload a file into the designated s3 bucket as below

upload_file.js

The original script can be found here. Please make sure to change the presigned url accordingly.

7. After updating the script with the received URL, try running the upload_file.js to see whether the file has got uploaded into the appropriate S3 bucket as an object. You may run the file as following:

node upload_file.js

8. Check your s3 bucket whether it has got uploaded:

Uploaded File

As you can see the video file which I used has uploaded successfully.

That’s it. It’s simple as that. By using CORS, you can even secure your API calls even further. In that case, make sure to include only the allowed origins when creating the API Gateway within the serverless template. You can also edit the CORS configuration of the bucket as well, so that only specific API calls from that origin will be allowed to upload files into the S3 bucket.

The complete example can be found here.

References:


Use Presigned URL to upload files into AWS S3 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Kulasangar Gowrisangar


Print Share Comment Cite Upload Translate Updates
APA

Kulasangar Gowrisangar | Sciencx (2021-04-13T12:59:15+00:00) Use Presigned URL to upload files into AWS S3. Retrieved from https://www.scien.cx/2021/04/13/use-presigned-url-to-upload-files-into-aws-s3/

MLA
" » Use Presigned URL to upload files into AWS S3." Kulasangar Gowrisangar | Sciencx - Tuesday April 13, 2021, https://www.scien.cx/2021/04/13/use-presigned-url-to-upload-files-into-aws-s3/
HARVARD
Kulasangar Gowrisangar | Sciencx Tuesday April 13, 2021 » Use Presigned URL to upload files into AWS S3., viewed ,<https://www.scien.cx/2021/04/13/use-presigned-url-to-upload-files-into-aws-s3/>
VANCOUVER
Kulasangar Gowrisangar | Sciencx - » Use Presigned URL to upload files into AWS S3. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/13/use-presigned-url-to-upload-files-into-aws-s3/
CHICAGO
" » Use Presigned URL to upload files into AWS S3." Kulasangar Gowrisangar | Sciencx - Accessed . https://www.scien.cx/2021/04/13/use-presigned-url-to-upload-files-into-aws-s3/
IEEE
" » Use Presigned URL to upload files into AWS S3." Kulasangar Gowrisangar | Sciencx [Online]. Available: https://www.scien.cx/2021/04/13/use-presigned-url-to-upload-files-into-aws-s3/. [Accessed: ]
rf:citation
» Use Presigned URL to upload files into AWS S3 | Kulasangar Gowrisangar | Sciencx | https://www.scien.cx/2021/04/13/use-presigned-url-to-upload-files-into-aws-s3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.