Coder Social home page Coder Social logo

rakibulislam / amazon-cloudstock-hackathon-solution Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dlobatog/amazon-cloudstock-hackathon-solution

0.0 3.0 0.0 726 KB

Implementation of Amazon DynamoDB and S3 push and retrieval of data using Ruby and Rails and the AWS SDK for Ruby. Quite seamless if you know ActiveRecord!

Ruby 61.68% JavaScript 1.39% CoffeeScript 0.69% CSS 4.64% HTML 31.59%

amazon-cloudstock-hackathon-solution's Introduction

Cloudstock Amazon DynamoDB & S3 Hackathon.

This application shows how to create a Ruby on Rails application that allows users to add information to a DynamoDB database, and to push and retrieve information from an S3 database. I included my own database names but you should change these for yours. You should change the initializer for your own secret and access keys.

Set Up

To set this up, as we won’t use ActiveRecord, call:

rails new your_app_name --skip-active-record

In order to get the AWS Ruby functionality, add the ‘aws-sdk’ gem to your Gemfile and execute ‘bundle install’ to get everything set up.

Initializer

See config/initializers/aws.rb . This gets called when Rails is initiated and it will log you in, provided you give your Amazon access_key_id and secret_access_key.

Model

Instead of getting functionality from ActiveRecord, we must inherit from AWS::Record, then:

User < AWS::Record::HashModel

The attributes should be defined here, as:

string_attr :id //This is my primary key in DynamoDB, you may have another one
integer_attr :whatever_number
boolean_attr :whatever_boolean, :default => false

In order to write information to S3, we use the following command:

AWS::S3.new.buckets[:your_bucket_name].objects[id].write(your_object_in_s3.read)

And to retrieve it we can use:

AWS::S3.new.buckets[:your_bucket_name].objects[id].url_for(:read)

Be wary that you should implement some sort of mechanism to avoid making too many calls in order to avoid getting to your S3 quota quickly. In this example there is a boolean attribute that takes care not to call to the database if there is no image.

View

I included a file_field caption in the form so that the users can upload their photos to Amazon S3. This is only done whenever there is already an id in DynamoDB set for the record (if @user.id).

In order to show this photos I call the user model in index with (user.img) so that the image is retrieved from S3. Try to call this method only if you are sure that the record contains an image so you can avoid unnecessary calls to the database.

Routes

Default controller redirects to @user using the default Rails ids, which by convention are just integers. DynamoDB uses ids with hyphens, lowercase and uppercase alphanumeric characters, so you have to figure out a way to handle this in routes.rb. A regular expression works well:

resources :your_model, :id => /[a-z0-9-]*/

amazon-cloudstock-hackathon-solution's People

Contributors

dlobatog avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.