We are going to use Amazon Web Services for storing the generated certificates and images.
Set up AWS account
You may choose to set up your own AWS account or you can get credentials from the coach. If you choose to set up your own account make sure to take all the necessary security precautions in order to avoid having your account compromised. It can be a very costly experience. [Instructions on how to set up AWS account with some screen shots?]
Create S3 bucket
[Instructions on how to create an S3 bucket with some screen shots?]
Configure and implement AWS upload
# Gemfile
gem 'aws-sdk'
gem 'dotenv'
Let's start with configuring dotenv
As early as possible in your application bootstrap process, load .env:
Create a .env and .env.example file in the project root:
touch .env
touch .env.example
Make sure to exclude the configuration file from version control by adding .env to your .gitignore:
# .gitignore
...
.env
Now, set up your credentials in your .env file:
# .env
S3_BUCKET=< your bucket name >
AWS_REGION=< your bucket region >
AWS_ACCESS_KEY_ID=< your access key id >
AWS_SECRET_ACCESS_KEY=< your secret access key >
Let's do some manual testing and upload a file to the S3 bucket
Let's add two properties to the Certificate class:
# spec/certificate_spec.rb
it { is_expected.to have_property :certificate_key }
it { is_expected.to have_property :image_key }
# lib/certificate.rb
...
property :certificate_key, Text
property :image_key, Text
...
We need to set these properties to Text rather then String due to the fact that length can exceed 50 characters.
We are going to do some major re-factoring of the CertificateGenerator, extracting some functionality to private methods and adding a method that will handle our upload to S3.
Update your certificate_generator.rb with the following code:
We also want our Certificate class to delete the associated images and pdf's from S3 when the record is deleted. For that, we need to create an before :destroy callback:
# lib/certificate.rb
...
before :save do
student_name = self.student.full_name
course_name = self.delivery.course.title
generated_at = self.created_at.to_s
identifier = Digest::SHA256.hexdigest("#{student_name} - #{course_name} - #{generated_at}")
self.identifier = identifier
self.save!
end
before :destroy do
s3 = Aws::S3::Resource.new(region: ENV['AWS_REGION'])
bucket = s3.bucket(ENV['S3_BUCKET'])
certificate_key = bucket.object(self.certificate_key)
image_key = bucket.object(self.image_key)
certificate_key.delete
image_key.delete
end
...
Make sure to run all your features and specs. The testing suite will take longer to execute then it used to.
We are actually hitting the AWS cloud storage in our tests (that is in principle a no, no!). There is much room for improvement of the way we set up our tests, but we will not focus on re-factoring our test at this point. The important take away for you is that testing play a vital supportive role in your development process and needs to be done in a smart way so it not becomes an obstacle.
Anyway, we have now implemented a cloud storage solution in our application. That is huge!