See all articles
Manipulating Files on Amazon S3 Storage with Ruby’s Fog Gem

Manipulating Files on Amazon S3 Storage with Ruby’s Fog Gem

Storing web application user files is quite easy with Amazon S3, Amazon’s highly scalable and durable object storage. We will show you how to connect it to your Ruby web application and manipulate files on it with a use of a Ruby Fog gem.

Amazon S3 is a highly scalable and durable cloud object storage solution, built to retrieve any amount of data from any place. Amazon S3 is a great alternative to traditional server side storage, since we no longer have to worry about limitations on space. Single objects stored on S3 can range from 1 byte to 5 terabytes and there is no limit to the total volume of data and number of objects that you can store on S3.

Amazon S3 was created with the aim of providing 99.999999999% durability, as a highly a secure and reliable tool. It offers flexible storage (high scalability) and management capabilities with easy, tailored data transfer. Additionally, it is one of the most supported platforms around, with a huge ecosystem of associated tools and add-ons. Taking all this into account, S3 can make a brilliant storage tool for your web application - with Ruby on Rails development using Amazon S3.

There are also many ways in which you can connect to S3 from a Rails web application. One of them is a Fog gem, a library created to deal with file cloud storage. In the rest of this article, we will guide you through how to use Amazon S3 with Ruby on Rails application, with the installation of the Fog gem, through to establishing the connection with S3 and then basic actions which can be taken on stored files.

Installation and connection to S3

Let's start with a gem installation by typing the Fog gem name in a Gemfile:

gem 'fog'

You can run a bundle install after saving the file so you can straight away move to building a class that would be responsible for the connection with the S3 storage.

The Fog gem itself provides a nice way to connect with S3, but it is always worth to it to build your own class, in order to make logic more encapsulated for future use.

You will always need to establish the connection to S3 before you can take any other action on your files. To do this, you will need to obtain the following credentials from your S3 account:

  • AWS access key ID
  • AWS secret access key
  • Region

When you are ready, you can try to connect for the first time using this code (subbing in your own details of course):

connection = Fog::Storage.new({
  provider: 'AWS',
  aws_access_key_id: 'access_key_id',
  aws_secret_access_key: 'secret_access_key',
  region: 'region'
})

Now, using the bucket name (objects in S3 are organized in buckets, with each one having an individual, user-assigned key), you can access all files on your S3 storage:

connection.directories.new(key: bucket_name).files

Saving a new file

You have to pass three arguments in order to save a new file: a full path of the new file on S3, the file contents, and a public status in order to determine if anyone can view our file using its URL. You would use your previously established connection:

bucket = connection.directories.new(key: bucket_name)
file_name = "/path/to/file"
bucket.files.create(key: "dir/new_file_name.ext", body: File.open(file_name), public: false)

Reading a file

To read a previously saved file, you just have to pass the key, which is the file path:

file_name = "/path/to/file"
bucket.files.get(file_name)

Providing the URL to your file

To provide the URL of a private file you have to pass two arguments: the file path, and the time during which that file would be available under the public link. You pass that expiration time in seconds:

file_name = "/path/to/file"
bucket.files.get(file_name).url(Time.now.to_i + 60)

If your file is public, then you can retrieve its URL by calling the `public_url` method on the file:

file_name = "/path/to/file"
bucket.files.get(file_name).public_url

Deleting a file

In order to destroy a file you have to pass the file path and call the `destroy` method on a file object:

file_name = "/path/to/file"
bucket.files.get(file_name).destroy

Scale up your life!

We hope you’ve found our step-by-step tutorial helpful in finding out how to use Amazon S3 as a storage tool for your user files in a Ruby on Rails web application. This highly scalable storage service can be quite handy for many applications and circumstances. If you intend to start using Amazon S3 and have any questions about it or queries about our Fog gem solution - or any other IT solution or software development issue - contact iRonin! We are always glad to help.

Read Similar Articles