Module: Storage::Strategies::S3
Constant Summary collapse
- MissingBucket =
Class.new(StandardError)
Instance Method Summary collapse
- #connect! ⇒ Object
- #connection ⇒ Object
- #create_bucket(name) ⇒ Object
- #create_object(bucket, file, options) ⇒ Object
- #disconnect! ⇒ Object
- #find_bucket(name) ⇒ Object
- #find_bucket!(name) ⇒ Object
- #find_bucket_or_create(name) ⇒ Object
- #find_object(file, options = {}) ⇒ Object
- #get(file, options = {}) ⇒ Object
- #prepare! ⇒ Object
- #remove(file, options = {}) ⇒ Object
- #store(file, options = {}) ⇒ Object
Instance Method Details
#connect! ⇒ Object
21 22 |
# File 'lib/storage/strategies/s3.rb', line 21 def connect! end |
#connection ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/storage/strategies/s3.rb', line 8 def connection @connection ||= Fog::Storage.new( provider: "AWS", aws_access_key_id: Storage::Config.access_key, aws_secret_access_key: Storage::Config.secret_key, region: Storage::Config.region ) end |
#create_bucket(name) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/storage/strategies/s3.rb', line 57 def create_bucket(name) connection.directories.create( key: name, public: false ) end |
#create_object(bucket, file, options) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/storage/strategies/s3.rb', line 64 def create_object(bucket, file, ) bucket.files.create( key: .fetch(:name), body: file, public: ([:public] || [:access] == :public_read) ) end |
#disconnect! ⇒ Object
24 25 26 |
# File 'lib/storage/strategies/s3.rb', line 24 def disconnect! @connection = nil end |
#find_bucket(name) ⇒ Object
49 50 51 |
# File 'lib/storage/strategies/s3.rb', line 49 def find_bucket(name) connection.directories.get(name) end |
#find_bucket!(name) ⇒ Object
53 54 55 |
# File 'lib/storage/strategies/s3.rb', line 53 def find_bucket!(name) find_bucket(name) || fail(MissingBucket) end |
#find_bucket_or_create(name) ⇒ Object
80 81 82 |
# File 'lib/storage/strategies/s3.rb', line 80 def find_bucket_or_create(name) find_bucket(name) || create_bucket(name) end |
#find_object(file, options = {}) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/storage/strategies/s3.rb', line 72 def find_object(file, = {}) path = .fetch(:name, file) bucket = find_bucket!(.fetch(:bucket)) bucket.files.get(path) || fail(MissingFileError) rescue MissingBucket raise MissingFileError end |
#get(file, options = {}) ⇒ Object
28 29 30 31 32 |
# File 'lib/storage/strategies/s3.rb', line 28 def get(file, = {}) expires = .fetch(:expires, Time.now.to_i + 3600) object = find_object(file, ) object.public_url || object.url(expires) end |
#prepare! ⇒ Object
17 18 19 |
# File 'lib/storage/strategies/s3.rb', line 17 def prepare! disconnect! end |
#remove(file, options = {}) ⇒ Object
44 45 46 47 |
# File 'lib/storage/strategies/s3.rb', line 44 def remove(file, = {}) object = find_object(file, ) object.destroy end |
#store(file, options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/storage/strategies/s3.rb', line 34 def store(file, = {}) object = find_object(file, ) rescue nil fail FileAlreadyExistsError if object bucket = find_bucket_or_create(.fetch(:bucket)) file = File.open(file, "rb") unless file.respond_to?(:read) && !file.kind_of?(Pathname) create_object(bucket, file, ) end |