1
\$\begingroup\$

I wrote a simple ruby program for interacting with one of my temporary AWS S3 using Ruby SDK. Below is the program I've written. Since I learned ruby by myself I need help/suggestions in following ruby's best practices with OOPs concept.

require './my_aws_helper'

class AwsDemo
  include MyAwsHelper

  def start_execution
    p '1. Bucket Details, 2. Create Bucket 3. Upload To Bucket'
    choice = gets.chomp.to_i
    start_user_interaction(choice)
  end

  private

  def start_user_interaction(choice)
    case choice
    when 1 # Bucket details
      bucket_details
    when 2 # Create bucket
      create_bucket
    when 3 # Upload to bucket
      upload_file
    else
      p 'Invalid Selection'
    end
  end

  def bucket_details
    p "Enter ALL for all bucket details or select from [#{MyAwsHelper.bucket_names.join(', ')}]"
    selection = gets.chomp
    if selection == 'ALL'
      MyAwsHelper.all_bucket_details
    else
      MyAwsHelper.bucket_details(selection)
    end
  end

  def create_bucket
    p 'Enter bucket name'
    selection = gets.chomp
    response = MyAwsHelper.create_bucket(selection)
    if response.class == Aws::S3::Types::CreateBucketOutput
      p "Bucket created successfully, #{response.location}"
    else
      p response
    end
  end

  def upload_file
    p 'Select a bucket from bellow'
    bucket_names = MyAwsHelper.bucket_names
    bucket_names.each_with_index do |name, index|
      p "#{index + 1}. #{name}"
    end
    bucket_name = bucket_names[gets.chomp.to_i - 1]
    p 'Select files from bellow list'
    p current_dir_files
    file_name = gets.chomp
    response = MyAwsHelper.upload_file_to_aws(bucket_name, file_name)
    if response == true
      p 'File uploaded sucessfully'
    else
      p response
    end
  end

  def current_dir_files
    Dir.glob('*.*')
  end

  def file_in_dir(_dir_name)
    Dir.chdir('dir_name')
    Dir.glob('*.*')
  end
end

aws_demo = AwsDemo.new
aws_demo.start_execution

And below is my helper module

require 'aws-sdk'

module MyAwsHelper
  def self.set_aws_configs
    Aws.config.update(credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']),
                      region: 'us-east-1')
  end

  def self.aws_resource
    Aws::S3::Resource.new(region: 'us-east-2')
  end

  def self.aws_client
    Aws::S3::Client.new(region: 'us-east-2')
  end

  def self.create_bucket(bucket_name)
    p "Creating your bucket with name #{bucket_name}..."
    bucket = aws_resource.bucket(bucket_name)
    bucket.create
  rescue Aws::S3::Errors::ServiceError => e
    e.message
  end

  def self.upload_file_to_aws(bucket_name, file_name)
    p "Uploading #{file_name} to #{bucket_name} bucket...."
    obj = aws_resource.bucket(bucket_name).object(file_name)
    obj.upload_file(file_name)
  rescue Aws::S3::Errors::ServiceError => e
    e.message
  end

  def self.get_bucket(bucket_name)
    aws_resource.bucket(bucket_name)
  rescue Aws::S3::Errors::ServiceError => e
    e.message
  end

  def self.buckets
    aws_client.list_buckets
  end

  def self.bucket_names
    buckets.buckets.map(&:name)
  end

  def self.all_bucket_details
    p "Found #{bucket_names.length} buckets...."
    bucket_names.each do |bucket|
      bucket_details(bucket)
    end
  end

  def self.bucket_details(bucket_name)
    puts "\n\n"
    p 'Please wait..'
    bucket = get_bucket(bucket_name)
    if bucket.exists?
      p "name    : #{bucket.name}"
      p "created : #{bucket.creation_date}"
      contents = aws_client.list_objects_v2(bucket: bucket_name).contents
      puts("#{contents.length} records present")
      contents.each_with_index do |content, index|
        puts " => #{index + 1} #{content.key} \n"
      end
    else
      p "Bucket #{bucket_name} not found"
    end
  end
end

I need suggestions on

  • how this program can be improved
  • what are the other ways of achieving it
  • Security issues in the program
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.