Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9161659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:01:06+00:00 2026-06-17T14:01:06+00:00

I have a class that uses the AWS S3 gem and have several methods

  • 0

I have a class that uses the AWS S3 gem and have several methods in my class that utilise the gem. My issue is, that rather than configuring it in several locations, I’d like to make it a property of my object.

In PHP, we’d do this;

<?php
  class myClass {

    private $obj;

    public function __construct() {
      $this->obj = new Object();
    }
  }
?>

And then I could use $this->obj->method() anywhere in myClass.

I am having a hard time getting similar to work in ruby.

My scenario is similar to this;

require 'aws/s3'

class ProfileVideo < ActiveRecord::Base

  def self.cleanup

    # <snip> YAML load my config etc etc

    AWS::S3::Base.establish_connection!(
      :access_key_id     => @aws_config['aws_key'],
      :secret_access_key => @aws_config['aws_secret']
    )      

  end

  def self.another_method
    # I want to use AWS::S3 here without needing to establish connection again
  end

end

I have also noticed in my class that initialize fails to execute – a simple ‘puts “here”‘ does nothing. Considering this is a rake task and I can ‘puts “here”‘ in the other methods. I’m not sure if maybe rake does not init like running ProfileVideo.new would?

Anyway, thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:01:07+00:00Added an answer on June 17, 2026 at 2:01 pm

    I’m not familiar with the S3 gem in particular but here are a couple ways you could go about this.

    If you simply want to make establishing the connection easier, you can create a method in your model like so:

    def open_s3
      return if @s3_opened
      AWS::S3::Base.establish_connection!(
        :access_key_id     => @aws_config['aws_key'],
        :secret_access_key => @aws_config['aws_secret']
      )
      @s3_opened = true
    end
    

    then you can call open_s3 at the top of any methods that require it and it will only open once.

    Another route you could take is to place the connection code in a before hook set to fire before any other hooks (IIRC, the order in which you define them sets the order in which they fire) and then make your calls.

    In either case, I would recommend against putting your AWS key and secret into your code. Instead, those should go into a config file is ignored by your version control system and generated on-deploy for remote systems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom class that uses boost mutexes and locks like this (only
I have a class that uses savon as a gem to speak with a
I have a class Game that uses a class TCPServer . I would like
So I have a class that uses references (&) with functions like void request(tcp::socket&
I have a class that uses filesystem entities to manipulate data. We have several
I have a class that uses a variety of methods and other classes as
I have a class that uses linq to access the database. Some methods call
Say I have a class with synchronized methods. It's actually a class that uses
My problem is the following; I have a class that uses non-static native methods
I have a UITableView class that uses the following methods to call a loading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.