Class: Vacation::S3
- Inherits:
-
Object
- Object
- Vacation::S3
- Defined in:
- lib/vacation/s3.rb
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
readonly
Returns the value of attribute access_key_id.
-
#bucket_name ⇒ Object
readonly
Returns the value of attribute bucket_name.
-
#secret_access_key ⇒ Object
readonly
Returns the value of attribute secret_access_key.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
Instance Method Summary collapse
- #backup_bucket ⇒ Object
- #bucket ⇒ Object
- #deploy_to_bucket(path, options = { :backup => true }) ⇒ Object
- #download_from_bucket(path) ⇒ Object
-
#initialize(id, key, bucket) ⇒ S3
constructor
A new instance of S3.
- #strip_bucket ⇒ Object
- #upload_to_bucket(path, is_backup = false) ⇒ Object
Constructor Details
#initialize(id, key, bucket) ⇒ S3
Returns a new instance of S3.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/vacation/s3.rb', line 8 def initialize(id, key, bucket) @access_key_id = id @secret_access_key = key @bucket_name = bucket @storage = Fog::Storage.new( :provider => 'AWS', :aws_access_key_id => access_key_id, :aws_secret_access_key => secret_access_key ) end |
Instance Attribute Details
#access_key_id ⇒ Object (readonly)
Returns the value of attribute access_key_id.
6 7 8 |
# File 'lib/vacation/s3.rb', line 6 def access_key_id @access_key_id end |
#bucket_name ⇒ Object (readonly)
Returns the value of attribute bucket_name.
6 7 8 |
# File 'lib/vacation/s3.rb', line 6 def bucket_name @bucket_name end |
#secret_access_key ⇒ Object (readonly)
Returns the value of attribute secret_access_key.
6 7 8 |
# File 'lib/vacation/s3.rb', line 6 def secret_access_key @secret_access_key end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
6 7 8 |
# File 'lib/vacation/s3.rb', line 6 def storage @storage end |
Instance Method Details
#backup_bucket ⇒ Object
23 24 25 26 |
# File 'lib/vacation/s3.rb', line 23 def backup_bucket @_backup_bucket ||= storage.directories.create(:key => "#{bucket_name}-vacation-backup", :privacy => 'private') end |
#bucket ⇒ Object
19 20 21 |
# File 'lib/vacation/s3.rb', line 19 def bucket @_bucket ||= storage.directories.create(:key => bucket_name, :privacy => 'public-read') end |
#deploy_to_bucket(path, options = { :backup => true }) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/vacation/s3.rb', line 60 def deploy_to_bucket(path, = { :backup => true }) if [:backup] && [:backup] == true Dir.mktmpdir do |temp_dir| backup_dir = File.(Time.now.strftime("%Y-%m-%d@%H:%M:%S(%Z)"), temp_dir) FileUtils.mkdir backup_dir download_from_bucket backup_dir upload_to_bucket temp_dir, true end end upload_to_bucket path storage.put_bucket_website bucket_name, 'index.html', :key => '404.html' end |
#download_from_bucket(path) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/vacation/s3.rb', line 33 def download_from_bucket(path) return unless storage.directories.get(bucket_name) FileUtils.mkdir_p(path) bucket.files.each do |file| location = File.join(path, file.key) FileUtils.mkdir_p(File.dirname(location)) File.open(location, 'w') do |local| local.write(file.body) end end end |
#strip_bucket ⇒ Object
28 29 30 31 |
# File 'lib/vacation/s3.rb', line 28 def strip_bucket bucket.files.reload.each { |file| file.destroy } bucket.files.reload end |
#upload_to_bucket(path, is_backup = false) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/vacation/s3.rb', line 46 def upload_to_bucket(path, is_backup = false) return unless File.exists? path strip_bucket unless is_backup target = is_backup ? backup_bucket : bucket Dir[File.join(path, '**', '*')].each do |file| relative_path = file[path.length + 1..-1] target.files.create(:key => relative_path, :body => File.read(file)) if File.file?(file) end target.files.reload end |