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
# Gemfilegem '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
# in irb:s3 = Aws::S3::Resource.new(region:ENV['AWS_REGION'])obj = s3.bucket(ENV['S3_BUCKET']).object(file)file = 'pdf/development/Thomas_Ochman-2015-11-23.pdf'obj.upload_file(file,acl:'public-read')obj.public_url
Let's add two properties to the Certificate class:
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:
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!