Tutorial: How to upload files into a S3 Bucket using a REST API via API Gateway

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] foreign so in this demo we're going to go over a quick tutorial on how you can upload files via the API Gateway into an S3 bucket so first we'll start off with the use case there are times when you will need to accept file uploads on your website and for this you can set up the following you can create an S3 bucket that'll hold all your files that you're uploading and we can use the API Gateway to create a rest API it'll accept put requests for those file uploads so now that we have the use case defined let's get into it okay so to start off we'll first start with creating the S3 bucket so we'll click on S3 go to create a bucket start by giving our bucket a name so I'm going to call Mines let's find Solutions file upload I'll pick us East one I'll leave that same I just want to take note of that because you'll need to use it again later you'll have to keep in the same region later so USC Swan is the one I'm going to go with uh everything else I'm going to keep the same we won't make this public I will enable bucket versioning and encryption I will leave the same and go to create bucket so now that our bucket's created we'll move on to the next step okay so next thing we're going to do is going to create a role and attach a policy to it so let's start off by selecting iam go down to roles create a roll let's go down at that bottom we're going to look for API Gateway oops this API Gateway is there let's select API Gateway highlight it click next so we have our API Gateway role here we'll leave everything the same and click next and let's give it a name so we'll call this API Gateway S3 scroll down leave everything the same and we can create this role so you can see here the roles created now we'll need to attach a policy to this we click on here click on the new row we created there's a add permissions and we are going to attach policies so right now we'll have to create a brand new policy so what we will do is we are going to choose our service which will be S3 and then the action we're going to be putting doing a put so let's search for put object and select that scroll down and this will need an Arn to our resource so we can add our Arn here we're going to want to add our S3 bucket name so our S3 bucket mines was uh let's find Solutions file upload that'll actually be the bucket name sorry so let's do this I have it typed here and we're going to allow it everything all the objects within that bucket so here we have our bucket name and we're going to allow everything within our bucket I'm going to click add there we don't need to change the requests conditions and we can click on to next leave that the same we can go on to review and we're going to give this policy a name so we'll call this uh let's call this S3 bucket policy and we're going to create this policy and if we scroll down so we see our policy has been created so what we're going to do is Select it and go to actions and we're going to attach it to our role that we created earlier so once we do that in the list you'll see the role created there we're going to pick on API Gateway S3 and attach that policy to that role so if we actually go back to the role we'll see that our role now has our new policy that we created attached to it and the original API Gateway policy that was attached to it so now that we have the role and the policy created we'll move on to the next step so next we'll move on to creating the API Gateway but before we do that let's go back to our IM role let's just copy that Arn whoops under roles because we're going to need that in our API Gateway setup so if we go there copy the Arn maybe save it to notepad for now let's save that so now let's go to API Gateway and create that next go to apis what we're going to create is a rest API we're not creating a private one just a regular rest API go to build say okay it's going to be a rest API we're going to create a new API and we'll call this file upload S3 and let's go create API so now we have our new API what we're going to do next is Select actions create a resource so the First Resource we're going to create is our bucket so be our bucket resource what we want to do is add brackets in front and after so that signifies that this is going to be a parameter in our path URL you can enable enable cores now or later that depends usually we can just enable it now if we want all right just because we might have to come back later and enable it if we had to I just can I'm just going to enable it now save us time so now we have our bucket next we're going to create another resource under that this will be our file name resource so this is going to be the file the name of the file when we upload it to our bucket again is going to be in a parameter one of the path parameters so put uh brackets around it we'll enable cores again okay so now under this we are going to now create a method so we are creating a put method check that box so we're going to create a AWS service so the region will be the same region that we selected earlier so I selected Us East one so I'm going to use that region for the service we're going to go with S3 so let me scroll down S3 is in the list domain will leave alone this is going to be a put method we are going to use a path override which will be our bucket slash and then bucket file name our role is the Arn that we saved earlier so I can save that copy it over we'll leave it pass through and leave the timeout the same and we'll click on Save so that creates our put request the next thing we'll want to do is go to integration we're actually going to want to add our URL parameters there we have two that we need to add first one again is the bucket bucket and it would be method dot request dot path dot bucket save that and add a second one which again is our file name method dot request dot file name we'll save that okay now that's saved we can go back so one other thing we do need to set um is the kind of file types that we're going to accept so if we go to settings we're going to set the binary media types so for this demo we'll just let it accept everything and save changes but depending on your use case you can set additional or you can limit it to a specific media types if you want like image files PDFs are only text files but for the sake of this demo um yeah I'm going to leave it star just to accept all file types so I'm going to save that oops and then what we can do next is go and deploy our API I already have a Dev stage setup click on dev and we will deploy so now it's going to take a couple minutes deploying the API if you go to put there's our address for our API endpoint give it a couple minutes and we can test out the API in Postman okay so the final thing we're going to do is test out our API in Postman we'll start off by copying this endpoint we'll head over to postman so what I've done is I've already created a put request here all we'll do is take that endpoint and paste it in there we're going to replace this bucket parameter with our bucket name so let's let's find Solutions Dash file upload and then we'll put in a file name so this file name doesn't have to match your actual file name it's the file name that you want to call it in S3 so we'll call it test upload one dot text since I'll be uploading a text file we'll go and select a file text file here and we'll try to click Send see what happens so we got back at 200 okay and a one so let's go to S3 bucket and see if we have a file there here we'll go into our S3 bucket click on the S3 bucket and you can see here we got our file and there you have it our working file upload from the API Gateway into our S3 bucket so there you have it that's how you set up a file upload via API Gateway into S3 buckets so if you like this video please give a like or thumbs up and I'll see you in the next video thanks again for watching take care now
Info
Channel: Let's Find Solutions
Views: 21,300
Rating: undefined out of 5
Keywords: AWS Step by step, static site hosting, custom domain, SSL Certificate, AWS hosted site, Route 53, CloudFront, S3 Bucket, Certificate Manager, AWS Static site with custom domain, DNS Entry, Web Hosting, AWS Cloud hosting, create website, hosted web site, AWS, AWS Tutorial, AWS Hosting, domain name registration, CDN, content delivery network, S3 buckets, AWS CloudFront, public domain, File Upload, REST API, api, REST, AWS Upload, api gateway, API Gateway, gateway, aws how to
Id: PxO8L9s3xOw
Channel Id: undefined
Length: 12min 34sec (754 seconds)
Published: Sun Feb 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.