Class: OpenDelivery::Storage
- Inherits:
-
Object
- Object
- OpenDelivery::Storage
- Defined in:
- lib/opendelivery/storage.rb
Instance Method Summary collapse
- #add_timestamp(identifier, artifact) ⇒ Object
- #copy(bucket, key, desired_key) ⇒ Object
- #download(bucket, key, output_directory) ⇒ Object
-
#initialize(region = nil) ⇒ Storage
constructor
A new instance of Storage.
- #upload(bucket, file, key) ⇒ Object
Constructor Details
#initialize(region = nil) ⇒ Storage
Returns a new instance of Storage.
28 29 30 31 32 33 34 |
# File 'lib/opendelivery/storage.rb', line 28 def initialize(region=nil) if region.nil? @s3 = AWS::S3.new else @s3 = AWS::S3.new(:region => region) end end |
Instance Method Details
#add_timestamp(identifier, artifact) ⇒ Object
68 69 70 71 72 |
# File 'lib/opendelivery/storage.rb', line 68 def (identifier, artifact) = Time.now.strftime("%Y.%m.%d.%H.%M.%S.%L") stamped_artifact = "#{artifact}-#{identifier}-#{}" return stamped_artifact end |
#copy(bucket, key, desired_key) ⇒ Object
36 37 38 |
# File 'lib/opendelivery/storage.rb', line 36 def copy(bucket, key, desired_key) @s3.buckets[bucket].objects[key].copy_to(desired_key) end |
#download(bucket, key, output_directory) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/opendelivery/storage.rb', line 44 def download(bucket, key, output_directory) # Puke if the bucket doesn't exist raise "Bucket #{bucket} doesn't exist" unless @s3.buckets[bucket].exists? # Puke if the key doesn't exist. This can cause problems with some bucket policies, so we catch and re-throw begin raise "File with key #{key} doesn't exist in bucket #{bucket}" unless @s3.buckets[bucket].objects[key].exists? rescue Exception => e raise "Exception [#{e.}] occurred downloading key #{key} from bucket #{bucket}" end obj = @s3.buckets[bucket].objects[key] base = Pathname.new("#{obj.key}").basename Dir.mkdir(output_directory) unless File.exists?(output_directory) File.open("#{output_directory}/#{base}", 'wb') do |file| obj.read do |chunk| file.write(chunk) end end end |
#upload(bucket, file, key) ⇒ Object
40 41 42 |
# File 'lib/opendelivery/storage.rb', line 40 def upload(bucket, file, key) @s3.buckets[bucket].objects[key].write(:file => file) end |