Accessing account buckets within a rails app
GEMFILE
: gem 'aws-sdk-s3', '~> 1'
bundle install
config/initializers/amazon.rb
Aws.config.update({
region: 'us-west-2',
credentials: Aws::Credentials.new(Rails.application.secrets.aws_access_key_id,
Rails.application.secrets.aws_secret_access_key),
endpoint: "https://s3-us-west-2.amazonaws.com"
})
config/secrets.yml
, store the actual access id and secrets:development
secret: adfbndggdhd
id: fdwets245hb3433
bundle exec rails c
, you can create a buckets, check existing buckets
and their items ...class ContentPage < ActiveRecord::Base
# Code for obtaining images on AMAZON S3
def self.get_files_from_s3
s3 = Aws::S3::Resource.new()
bucket = s3.bucket(Rails.application.secrets.aws_bucket)
file_array = []
files_from_bucket = bucket.objects(prefix: 'uploads/fsi/content_image/image').each do |obj|
file_array << obj.key if obj.key.include?("thumb_")
end
file_array
end
end
s3 = Aws::S3::Resource.new()
won't need to supply any credentials, they are done already in the
initializations3.bucket(name)
prefix
and insert the path you would like to retrieve your pictures from.bucket = s3.bucket(name)
, bucket.objects()
will return an array of images stored in the bucket
which you can now iterate over!