Cloud Storage with Rails
Cloud storage is one of the most popular and flexible ways of storing files for applications these days, it allows the files to be accessed from anywhere and it is relatively less expensive than the onsite alternatives. In this article, we are going to see what cloud storage is, which are the most popular services, and how to integrate S3 into your Rails application.
Applications use files all the time with different purposes, some files are static like images and logos used throughout the applications, and other files are dynamic like reports or data exports/imports. No matter what type of files your application uses, they must be stored somewhere. This is why cloud storage was created and popularized, it is a form of storage in which data is stored on servers in off-site locations, these servers are maintained by a third-party provider responsible for hosting, managing, and securing the data and also the provider ensures that data on its servers is always accessible via public or private internet connections. Cloud Storage enables organizations to store, access, and maintain data without owning and operating their own data centers. It is also scalable, allowing organizations to expand or reduce their data capabilities depending on their needs.
All the cloud services in the market allow you to connect to those off-site servers through your cloud provider either from a web application, API calls, or console access using a public or a private connection. Using this model has a lot of advantages, especially for small and medium businesses, that don’t have the resources to run on-site servers. The cloud model offers off-site management so you relay the responsibility for maintaining and protecting the stored data to the provider. It also accelerates the process of setting up and adding to your storage capabilities, since you can provision the service and start using it within hours or days, depending on how much capacity is involved, and paying only for the capacity you use. This gives you the capability of scaling as your product needs it instead of having to assume the cost up-front. As part of the service, providers typically store the same data on multiple machines for redundancy. This way, if a server is taken down for maintenance or suffers an outage, you can still access your data.
There are also some disadvantages to using cloud storage, like having to be more diligent when it comes to resource usage and optimization. If you are not keeping an eye on it, the costs may get higher than with an on-premise server. Another issue is that there may be some request latency issues, since the data is not in your network, it can translate into traffic congestion or too many jumps to get to the servers. The last and most important issue with cloud storage is regulatory compliance. Certain industries, such as healthcare and finance, must comply with strict data privacy and archival regulations, which may prevent companies from using cloud storage for certain types of files, such as medical and investment records. If you can, choose a cloud storage provider that supports compliance with any industry regulations impacting your application.
Now that we know what cloud storage is let’s dig into the most popular services that are out there for this purpose:
- Aws S3: Also called Amazon Simple Storage Service (Amazon S3). It is AWS’ object storage service offering scalability, availability, security, and performance, with cost-effective storage classes and easy-to-use management features, where you can optimize costs, organize and analyze data, and configure fine-tuned access controls to meet specific business and compliance requirements.
- Azure Blob Storage: This is Azure’s service, that helps you create data layers to build powerful cloud-native and mobile apps, also optimizing costs with tiered storage for long-term data, and flexibly scaling up for high-performance computing and like machine learning.
- Google Cloud Storage: Google’s solution is a managed service for storing unstructured data. It stores any amount of data and you can retrieve it as often as you like.
Now let’s have a look into how to set it up in your Rails application, for the sake of showing how to implement cloud storage I’m going to use AWS. To use the cloud provider of your choice you must include their respective gem and there are several configurations you must make before being able to use it. The first is to include the AWS gem aws-sdk-s3
in your Gemfile and install its dependencies. You also have to get a set of keys from your AWS console for the application to access the buckets, you can watch this video to see how to do that. Once you have those you can start modifying the configuration files for the environments. One file is /config/strogae.yml
which has the configuration for all the storage options you may need in your Rails application. It is already populated with the services we saw earlier, so for this exercise, you just have to uncomment the amazon section. Once you have done that, you can go to /config/<environment>.rb
and look for the line config.active_storage.service
and set it to :amazon
. Remember to store your amazon access and secret keys in the credentials file and you are all set.
That was all I wanted to share with you. We went through the concept of cloud storage, popular storage services, and how to integrate it with your Rails application. Thank you for reading this article. If you liked it, give it a clap and check out my other articles.
References
- https://azure.microsoft.com/en-us/products/storage/blobs/
- https://cloud.google.com/storage
- https://aws.amazon.com/s3/
- https://cloud.google.com/learn/what-is-cloud-storage
- https://www.ibm.com/topics/cloud-storage
- https://en.wikipedia.org/wiki/Cloud_storage
- https://www.youtube.com/watch?v=lntWTStctIE
- https://dev.to/akbarnafisa/create-access-key-for-aws-s3-2cl3
- https://rubygems.org/gems/aws-sdk-s3