AWS S3 with PHP - Amazon Web Services End to End Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is a guide to using AWS s3 buckets with PHP now this is an engine guide because I have some other videos that cover each individual part but however I was getting a lot of questions relate to some other things not related to the videos so I figured I'd just cover everything from end to end because like that's how you do things in Kodi you know so what we're gonna do is we're going to setup the AWS s3 bucket or stuff to use it in proper permissions we're not you you know we'll be able to populate file upload we're gonna create a PHP form we'll do file upload to the s3 bucket and then from there we're gonna create so that the files can be accessed privately using a database now there's other ways you can upload public files as well I'm not gonna include that in this video if there is one I'll pin a comment to the top of the comments section for that video so check that out as well I also ask that you subscribe because I have weekly videos every single Wednesday and I think you may really enjoy them so let's get started jumping over to my screen I had a couple things I have the AWS Python setup how to setup an AWS s3 bucket it's the links below I have uploading image using a form for PHP to s3 it's here as well I have a private open improv private s3 file this is also a guest here and then I have my AWS console open and at the top of this I include this particular form submission 1 I include instructions on how to actually do it how to find a library sorry these are all very important because it's gonna help you get started and things like that so first want to start with setting up the s3 bucket and the reason why we're going to start with that is because it's kind of the en su bucket to upload to so jump it over to this repository here you see I'm at my dashboard I just signed in next instruction is open up s3 in creeper set your first bucket so I'm gonna hit s3 I'm gonna open that up in a new tab it's a Creed bucket let's just say simple sorry example PHP and two and oh it's an invalid character except those okay PHP and 2n and that's gonna be in the name of my bucket I'm gonna put in I like to use US east to Ohio and then I'm like that's all I need I'm gonna hit next next again and yes the owner should have all permissions and I'm gonna hit create alright so example intend bucket it's all set up now I have to follow the next instruction create and then I have it this is just me testing uploading a file directly to the s3 bucket so if you hit upload there skip over that making it public the next step is using the I M these are all found in just services Security has I am here and we jump to the I am management console I'm gonna go to users and this is we're gonna add a new user so the user name is I'm gonna copy what I have I call it a user - user this will be gone before you actually upload this video so you have to give him a programming access it next I actually want to be writing these things down so we have this than we have user I mean it next create user and then they give you a access key ID you need to copy this down because they don't show it to you again and a secret you want copy that to anyone close it I'm gonna jump back over to the instructions here we follow all these instructions okay so now that we have that set up we need to actually use this grab this user AR n copy it and I guess paste it into a text document for now because we're not to change the permissions on the s3 bucket copy that okay so you're gonna go to bucket policies copy the bucket policy I'm not shooting a copy mind right into my bucket so under overview permissions in the bucket bucket policies copy and paste grab my user here as you can see I have sample user replace out with and then I have an AR n for my bucket and so that's image bucket 0 1 2 3 paste it in there and I hit save so that's ID instruction the bucket policy is all set up and the last step for the bucket is setting up the course I which is the cross origin I forget what the arrow stance record of Gotham and hit save face then there okay so now the bucket policy is all set up buckets ready to go but now you have to actually set up the user so that he knows that it's allowed to use that bucket so we're gonna reopen at the I am dashboard or you can just leave it open like I did we're gonna do is go down to in line add in line policy you do custom policy select give it a name of some sort and then you scroll down I God right here again this the reason why I'm just showing out taking it directly from here because there's no sense you know copy and paste answer this is only a point of copy and pasting it because this doesn't change and so you're gonna give it the permission to get permission in the list all my buckets permission and you can validate if you want and you can apply the policy and therefore the user in the bucket s3 bucket are both set up you're ready and so then what I given this in this repository afterwards is how to upload it with Python we're gonna actually use PHP but you can test it if you want the codes included in the repository and that's all we're gonna need to use for this one next I'm using a Mac so I'm going to use map this is the actual s3 form portion MD and Gen guide so I'm using nap because it's Apache Mac Apache MySQL and PHP you need a way that's to have a MySQL server and run PHP so if you're on Windows you can use WAMP Windows Apache MySQL PHP Linux lamp and so on and so I'm gonna open with a map I have it running and then in my htdocs folder I have an s3 guys folder where I'm gonna put everything and that's under applications and so that's what I've been actually working in right here it's in my s3 guide and so I'm gonna need to create a file and say index dot PHP don't you say hello world or should I test out that the AF server is all set up forget the exact you know here it is s3 guide so hello world my I'm running well a little my local environment I'm all good okay so remove that and then what we need to do is actually this is the F we'll upload code what we need is the we're gonna actually need to form code warm up Lucas file upload here PHP file upload and that's this here copy and paste that right in here this all works fine for me model file upload which upload the Tor s3 bucket this is already that's all good and what we're doing is uploading to the s3 bucket okay so my upload runs OH but we'll need to run is this you need to have composer installed on your computer so what I've done is I've opened up my terminal navigated using CB folder name over to the s3 guide for my map and what you need is to run composer require AWS AWS - SDK - PHP and that's the PHP library given from Amazon as you can't run that it's gonna run as I'm gonna see my folder here it's going to create a composer JSON file and at some point it's gonna finish and there we go it's going to install the necessary packages and stuff and so it and so it's gonna set us up it's gonna create a lock from a vendor and what I'll need to do as you can see my upload code here so I need to reference that vendor autoload so what I'm first gonna do is copy and paste all this in here and I'm actually gonna reference the vendor correctly just because I'm not actually in the route and then what I'll need you to put in here is my bucket name which is my appeard example PHP and to end my aunt I am he I am secret make sure you get the whole thing paste that in there u.s. East to which is Ohio if you went with North Carolina its us East one you can always see this information at the top here us is one live mines one okay check that out you're on your own for sure it's us ec2 and what I do here is I upload it to the bucket name into a folder called test example and I use the file and I upload it and then it prints have done so let's try that out refresh this page so now should have choose file and press the choose file and I'm on my desktop and I have a couple photos for my trip sure it's for you to choose this one this is Vienna I'm gonna hit upload what's gonna do is gonna take me to my upload dot PHP and then I have overview here and it says done so if i refresh this page i my test example' folder as per the code so that says right here and then i test example and I have my PHP okay so I didn't get the folder the image the name right sorry I was a big jumble of words I don't think it's test name you know [Music] yeah so it's name okay so let's try that again actually what we'll do is hit back and we'll delete that folder [Music] I'm gonna hit upload again [Music] an ad says done and refresh the page as a proper name now image 3 1 whatever I can download it I hit open to me a photo of Vienna yeah so find a lot below done ok so the last thing is involving actually accessing the file and what we're gonna do is use a private link so the idea is like this would be a shareable link Google Drive or something like that and how we're gonna do it is we're actually using database because you you need to use a database to store the link is technically it this is a valid link here for your file but it says access to nine so you we're gonna write some code that it's gonna verify that to us and then it's going to pull up the body to the file and display it and we need to need to control who can see it right so first when I actually add one more step in here with map you have a database and that's important because it's what we actually need sue me and I don't what we're gonna do is create a database cold and three DB and we're create a table called files RS three files and there's me an ID the path I guess the file name let's say we really need any night stuff let's just say the path and then the ID will work so we only need two columns I mean you want to add probably a lot more but [Music] you know it will add one more 3d file I think the road trip is gonna be a strain I make that null that's like I mean they'll consume you know auto-increment to be the primary key never hit save we're actually going to add another row and we call this [Music] and say that's any string as well what's important about this is if you're actually gonna implement this I recommend creating a table for your files and then table for your s access code access codes so that if someone deletes an access code or something along those lines you can just go back in and like you don't lose your five the list of files and there's more than one you can create one more than one access code for a single file so you can set give a different access codes not hard just another table that stores your s3 files and then all you have to do is reference that file code or even you can even do it this way still but this is meant for one person I guess so what we're gonna do is do instead of it saying done right away if it doesn't hit these dies which stops the script we're not gonna do this because we don't actually need to do this run we only need one file on the upload and we can also you can also have it randomly generate access code but we're not going to do that either we're just going to use my constant so when I say 1 2 3 4 5 6 7 and what we're gonna do is we need to connect we're on the localhost the passer it's a route the passwords are it again or if they're blank let's go s3 DB is the database die let's connect and what we're gonna do here is say connection insert into this table called s3 files [Music] we're the s3 filepath is this and the access code values will be [Music] just go the key name you need to store this full path in s3 don't know why we have that path in history the Banquo tations and when I store the access code or die being able to save so now when we upload it's actually upload with into this database and we have information about our file let's try it one more time this time we'll choose a different full image choose image I have a bunch more from my trip [Music] Cheers such a zone [Music] their photo Prague hit upload make sure you hit save on that other page by the way I hit upload I haven't done a lot of very last thing that's gonna happen is nap so we actually see it probably updating in s3 before we see updating ma'am and it's done so there's no failure so you should see the file which we do and then in amp we should see the file okay boom so there it is test and then the image path and then the access code and so the last step is actually creating the file to access it the PHP file so what we're gonna do is still use the PHP SDK but now we're going to reach into the bucket and say okay see this this key and this path to this file we actually want to take it and get the body of it and so that's what this gist is about and so this is actually important so we're gonna call this file of get PHP it's going to be in a root same as everything and we're going to use this all this code here like the spacing on this [Music] okay so the bucket name we have all this information name is I also recommend from these bucket name and the user and stuff all in a constant and then I guess you want to get ignore that stuff but this is just again a quick demo of this put the secret in there I'm referencing directly because my vendors actually a back one more actually the htdocs folder so as you can see see it's maybe the URL and it's gonna be our get and then we're gonna grab it remove all the slashes and stuff we need the connection here which is not there yet got the connection and what it does here is it looks for s3 files is that actually our name oh sweet align them up s3 files it looks for the code which is not valid it's access code in our database right access code if it doesn't find it and then it doesn't finance an invalid access code and then it looks for the s3 file path sweet I lined up all these and then it goes out and grabs this file path and looks forward in the bucket which is here it creates an s3 object and then gets the object from the bucket and then takes the bought bucket says echoes the the type and then echoes the body and so this will actually load this page so if we do localhost eight eight eight slash s3 guide flush get dumped PHP sloshed let's just do one two invalid access code because it couldn't actually find on the table so now what we're going to do is three four five six seven which was our access code hit enter bucket is missing it it's missing bucket is missing bucket is missing invalid name those don't line up that guest is broken I may fix it before you see this video but save it and then reload the page what you do is actually either download or open up in Chrome and it created a get duck PHP file which doesn't seem right downloads [Music] I hope I got this right [Music] I use JPEG yeah so the right image it just has the wrong extension on the end so now I just gotta fix that and the name [Music] [Music] here file name [Music] header and then what we'll do is make this be [Music] again I may fix may fix us on the actual I guess but maybe not and we'll make it based name which means I graphed the very end of the path the path and keep path and so let's try this again one last time reload the page and this error doesn't help it's not happening here and then image zero three one or whatever and there's the image again and so what we can do is if we go like this invalid access code there's only one valid access code to prove that that works we'll change it in the database and say my name [Music] tables I didn't say table correctly [Music] why not [Music] primarykey doesn't contain okay it doesn't seem to be working because my man but that's the point so here that's how you do s3 and to end guide' for PHP so that's SEP answer e bucket that's set up the user and all the permissions that's upload a file and then download a file privately via link now yes everything there's not much more to it if you like this video please subscribe I have a new video every single Wednesday please also give this a like thanks very much just getting the last few things ready before recording this video but please hit that subscribe down below so I'd love to stop eating hot dogs for dinner you
Info
Channel: Keith, the Coder
Views: 32,789
Rating: undefined out of 5
Keywords: aws s3 bucket, keith the coder, aws s3, amazon s3 php, s3 iam, s3 bucket, s3 bucket setup, amazon s3 bucket, aws iam s3, upload files to s3, s3 upload php, aws s3 user permissions, s3 bucket in aws, aws s3 php, upload file to amazon cloud in php, aws upload files php, upload a file into s3 with php, aws s3 php file upload, setting up s3, Amazon Web Services, Coding, Tutorial, Hosting on S3
Id: aPBFqDIIPYE
Channel Id: undefined
Length: 24min 30sec (1470 seconds)
Published: Wed Sep 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.