Upload files to S3 using API Gateway - Step by Step Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello service people enrique here this is a different type of video because i want to start from a comment i receive on one of my latest videos and this guy asked me can you help me with these questions this was asked during an aws interview create an api gateway which contains an endpoint to upload jpeg image to s3 buckets the image should be stored in s3 bucket using the file name parameter as the s3 object key assume that the size the image uploaded is always less than 4 megabyte alright so now i'm going to show you how to do it in the adress console as always if you like these videos remember to subscribe to the channel and to hit the like button so let's start from the comment again and let's break down the problem in two bullet points the interviewer is asking to create an api gateway that contains an endpoint to upload jpeg images to s3 buckets to an s3 bucket sorry the image should be stored in s3 bucket using the file name so here we have a parameter as the object key because you know every time you save an object into an s3 bucket it has a name that name is called object key assuming that the size the image uploaded is obviously less than four megabyte this is very important for the execution of the problem so i came up with two solutions and now i'm gonna explain you both of them and why i'm gonna choose the second one so the first one is of course we're gonna create an api gateway with an endpoint with a put http method who's backed by an aws lambda who's gonna get the file and upload it to s3 so lambda receiving the file through the api gateway body and then lambda is gonna take and upload to the s3 buckets the other way is to directly use the api gateway as the integration we use the s3 bucket it is possible because you can use an s3 bucket as the integration method for the api gateway one thing to consider is that the problem statement says that the object the image actually is always less than four megabit megabytes sorry so uh we have to remember that the uh size limit of the api gateway is 10 megabyte so in this case we are okay to use api gateway in front of the endpoint since the problem just says use the file name parameter has the object key and doesn't doesn't need any you know object uh modification like resizing the image or changing the extension changing the name any other you know modification in terms of i upload an image and i want to save it another way let's say changing the image to pdf it doesn't it doesn't need any modification we can update the aws lambda because uh putting the aws lambda in between the api gateway and bucket just adds latency and cost so in this case i'm gonna choose this solution and i'm gonna show you how to create the api gateway im roles and policies and link the api gateway to s3 packet okay let's go on the iws console here i am on the i am role sorry on the i am menu i am roles here and i'm going to create a role for this example so i'm going to call it so here i have to select which service i need to uh use with this role it's going to be api gateway is here next permission i'm gonna add the permission after since i'm gonna create a policy for this example so i'm gonna call it api gateway sample one and here is like hello api gateway to push objects into an s3 bucket as a description and i'm going to create a role next step is to create a policy and then attach the policy to the role so we have created the role let's create a policy for the policy we need to be able to upload objects inside an s3 bucket so here i'm going to select s3 here i'm gonna choose just the put object action which is this one here i need to select the s3 packet so before doing that i have already created the s3 bucket and it's called enrico origin h1 which i use in another video anyway you can add the s3 bucket here so packet name and object name any so this policy lets you upload put objects inside this packet let's create the policy next review let's call it api gateway to s3 enrico edge and create the policy once the policy has been created we just need to attach a role which is going to be this one so here action attach and here i have to search for my role okay that's it and now we have attached the policy to the role that we have just created so let's go again to the rules and we're gonna just select the role here because we're gonna need the on later when we create the api gateway now that we have the poison the role let's go on the uh aws console again and create the api gateway so this is the api gateway menu we're gonna create a new api here we select rest api and click the build button we need to set a name for the api i'm gonna call it like my api example my api get for example and i'm gonna select regional as the endpoint type and click create api so here we have the [Music] resources we had only had the root path now we have to create two new resources one to specify the bucket where we want to save the image and wants to specify the file name that we're gonna use to save the image to s3 so let's create the first results here and we're gonna call it like bucket and here we're gonna use the curly braces in order to be able to use this bucket as a parameter when we use the endpoint so we i click create resources and i'm going to do the same thing under this one so create resources slash i'm going to call it file name and here is going to be again with the curly braces file name and create resources so now we have specified an api what's going to be slash packet file name next thing we have to do is to create the actual http method so for this use case since we have to upload an image to an s3 bucket i'm going to use the put method so here i create method select put here create method and here i have to choose the integration type in our case we not we decided to not use the lambda function so let's go back quickly on the architecture i'm gonna just use the s3 bucket directly so here i have to choose aws services region of the packet in my case is eus2 which is london and here i have to search for the simple storage service which is s3 subdomain can be empty http method can is put as we said before and here we have to use the use path override in this way we're going to use the path override to be able to specify the packet name and the file name as i said before so here i'm gonna use bucket slash key which is the key of the object save into the s3 bucket here i have to specify the execution role so it's the role that api gateway is gonna assume when you hit the endpoint and i'm gonna copy the on from the role we've just created let me copy the iron as well and here i'm gonna click save the next step is to tell the api so using the integration request to use the path parameters as the packet and the object key so i'll go here url path parameter i'm gonna add a path which is gonna be here name it's gonna be bucket and here you have to use the method path meta request path bucket which is you refers to this one and the next one is key so here i'm going to use the key and it's going to be method request path funny in this way the api gateway knows that it has to use the packet key values from the path of the url of the endpoint basically so let's go back and the only thing that we are missing is to tell api gateway that this is like a binary request which means the body is going to be a binary media types so you go on the settings here you choose binary media types and you can decide to add uh you can decide to allow basically any media types by doing like this but in this case since we have been asked to use the image jpeg i'm gonna just specify image jpeg type and click save changes okay so we have done all the configuration needed and the last step is to actually deploy the api because now it's not live yet so we select here actions and we're gonna choose deploy api and we have to create a new stage in this case you can just call it dev and click deploy once you've deployed the api you should see an invoke url so you copy the url and i'll be using a postman as the api client the method is a put and then here i have to specify slash the packet name that i'm using so it's this one a slash define name i'm going to call it enrico dash example dot jpg the next thing we have to do is to tell postman to use a binary type so he's here select file and i'm gonna use the same image as always the dojo so i have everything configured here i'm gonna click send and we've got a one which means something has happened and should be successful so now if i refresh the bucket i should see the image uploaded yes so it's as you can see it's been just uploaded it's 9 20 it's 920 as well here and that's it all right guys this is all from this uh video hope it was useful i hope it's gonna help you with your aws interviews or just you know to understand which services you can integrate with api gateway let me know if you have any other questions remember to subscribe to the channel and thanks again for watching cheers
Info
Channel: Enrico Portolan
Views: 1,431
Rating: undefined out of 5
Keywords: aws, serverless, cloud
Id: Q_2CIivxVVs
Channel Id: undefined
Length: 10min 52sec (652 seconds)
Published: Thu Nov 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.