Ruby on Rails #21 Active Storage With Amazon S3 Cloud Storage

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so previously we've added the possibility to add an avatar to our posts and to add multiple images to our posts and now let's see how it will work in production basically i have pushed an application to heruku at this url and join the migrations and now i will try to create a post so i'm going to create a post i will edit it a title a post okay i will add an avatar so it can be one file and i'm going to add a few other files as images and i create a post so it is uploading that's why it is taking so long and yeah it says that the post was created and you see our avatar was saved our images were also saved but if we read the heruko documentation this is not follow so it seems that it works but if we restart the server if we run heroku restart or automatically every 24 hours by itself when hiroko restarts all these files that we saved in the local storage on haruku are going to just disappear so now i have written haruku restart and i will refresh the page it is going to connect to rook now and let's see what happens now see it is still loading because i have just written heroku restart so it is uh kind of rebooting the application okay you see so all our files have vanished so they are not and they were basically removed from our local storage on haruku so on haruko we basically have this kind of application and it also has this storage uh folder and it gets removed every 10 24 hours or after a heroku restart and that's why you definitely don't want to keep any files in the storage folder on heroku you will want to use an external cloud storage now the storage that i use most oftenly is aws s3 and now we're going to go through how to use it and how to install it so if you go to the active storage documentation on rails guides you will also see some hints about it so we can configure the active storage service to be either amazon local or whatever let's go into our rails application i'm going to go to config and here we have a storage via ml and here we can add settings for amazon for example and if we go to our environments we have development rb we have production so we are going to configure which active storage service we want to use in development and production now the most common way is to use local storage for development and to use something like amazon introduction so here you see config active storage service local now by default we have local service set for active storage in production so we are going to have amazon here amazon like this okay and now we've set amazon here and we need to go to storage by ml and add our amazon credentials so i'm going to uncomment this and the amazon service for storing the files is called s3 and now you see we need to have an access key id a secret access key a region and a bucket name so at the moment i'm already logged into an account on amazon and i'm going to go to services and here in storage we have this s3 service so it's loading okay and here we have this button to create a bucket so now let's create a new bucket a bucket is basically a folder in which you can keep files and read and write to the market so let's name it super rails okay i will select a region while i'm doing everything in this region you can select a region that is closer to where you are and let's continue we are not going to change any other settings i'm going to create a bucket okay and we have created a bucket now what is a bucket basically it is a container web you can have files we don't have any files here yet but if we go to one of the other pockets for example to this pocket we see we have a few files i can download one of the files i can try to open one of the files so here is one of the files that i uh uploaded okay so on s3 we have created a bucket named super rails so we can go to storage by ml and say what is the region the region is uh eu central one and the bucket name is super rails so it is your brailles and now we need to get an access key id and secret access key now for this we are going to need the identity management so we are going to go to services and somewhere here we have iam let's see well i found it here and recently visited i'll try to go in search yeah here we have iam and we are going to create the new credentials for accessing the aws s3 now i have three users i'm going to create one more user i'm going to add a user let's name it super rails s3 and it is going to have programmatic access let's just name it subrails okay and further so now we are going to attach some existing policies basically what a user that the these api keys can do so we are going to go to s3 and they are going to say amazon s3 full access next review create user okay so i have created a user here we have the access key id i can copy it and i can paste it here but i'm not going to do it because we need to use credentials for this you see it automatically says that we can use the credentials we shouldn't keep our api keys just in the open like this so i'm going to go to editor bim rails credentials edit and here you see by default we even have the prompts for aws so they're going to uncomment it they're going to say aws then we have access key id okay and now we get the secret access key so i press show here it is i paste it here okay i made a mistake and deleted something that shouldn't have been deleted i will do it once again so i open the file i press i i uncomment now i copy the access key id and now i copy the secret access key okay now i press escape column wq to exited saving enter okay file encrypted and saved i can check you see it was saved and we added our credentials so with this xs key id and secret access key we can access our asteroid storage on amazon so i'm going to close and it should kind of work so we have said in development we want to use local but we are not going to use local we're going to use amazon so we're going to use to try to use amazon s3 for our local development now in storage by ml we gave the path so we have amazon xskd secret access key region bucket so it should kind of work now let's start our server and see if it works okay i'm going to refresh and uh it says missing service adapter for s3 yes we should install the s3 jam so i think somewhere here there is yes here's the reference for the gem aws sdk s3 and we're going to add this jump to our gem file to make it work like this and now we run bundle now we start the server and it should work okay so i'm going to let's say try to create a new post with s3 i'm going to add an avatar one file images a few files create post and it was kind of creative let's see if it was saved on s3 we can do it in the aws console we can go to storage s3 go to our bucket here is our bucket and you see we have three files that put saved here so i can open one of the files object actions open and you see we managed to save our files to a cloud storage and preview them now this way if we go into our logs we also see that we managed to save the files on aws s3 so here is an url that was generated temporarily for this file we can open the file and you see this on aws s3 so now even in our local environment we are using s3 for storing items looks nice now let's go back and see if it will work in production so i'm going to go to production.rb and you see we already have amazon set before by default it was local now we say amazon and in development file for development purposes you will most likely want to use local long term so let's see if it works what have we done we have added the gem aws sdk s3 to our gm file then in development ob we tried adding amazon in production we replaced local with amazon in storage by mlv uh added our pocket our region and added our credentials in the credentials file and that's basically it so let's save our changes i will say git status git add all git commit main aws s3 for file storage okay now i'm going to push and push to heroku get push main and we'll see if it works okay so we have pushed our application to haruku and let's see if it works i'm going back to haruko i will try to create a new post now it's loading kind of restarting i'll go to posts yeah we should wait a bit apparently after pushing haruku okay actually i got this application error now why did they get it it is because i pushed my application to haruku but i did not push my master key to heruku so here i have my master key and i need to copy into the heroku settings so i'm going to type in this command heruko config set rails master key and in heroku we're going to set the rails master key as the master key that they have for in our config so i'm going to add this you see we've set the rails master key on haruku as the master key that we have in this file so now it should work i'm going to restart once again refreshing the page and with the new master key that we added to heruku it should work okay so the application started now i'll try to create a new post i'm going to add an image here i'm going to add a few images here okay and create the post and let's see if it works let's also have a look at our logs herruku logs tail so you see all the files seem to have been uploaded now were they uploaded to heruku let's oh i'm into s3 let's check i'm going to s3 super rails and here you see we have a few additional newly created items that were created 14 15 so just now and it seems to work so going to the logs let's have a look we see the rails active storage and you see we get this downloaded file from key so basically uh the images were uploaded to our s3 and now when we restart our heruku instance the images will still be available i can type heroku restart heroku restart and then we were storing the images initially on our local heroku storage after restart the images were not available but now as they're storing the images not on heroku but on the s3 cloud storage everything should be available so i have restarted herruku i will now refresh this page and i expect it to work correctly so that the images will be available now it is still restarting and voila the images are available so what have we done we went to our aws account then to s3 and created a new pocket and we created an iam role to which we gave access to s3 and that's basically it so this is the basics of making the application work with a cloud provider like amazon s3 thank you for being with me have a nice day
Info
Channel: SupeRails
Views: 1,992
Rating: undefined out of 5
Keywords: ruby, rails, ruby on rails, tutorial, programming
Id: OWBWXOcx1rU
Channel Id: undefined
Length: 16min 10sec (970 seconds)
Published: Tue May 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.