Class: Bosh::AwsCliPlugin::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh_cli_plugin_aws/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ S3

Returns a new instance of S3.



4
5
6
# File 'lib/bosh_cli_plugin_aws/s3.rb', line 4

def initialize(credentials)
  @aws_provider = AwsProvider.new(credentials)
end

Instance Method Details

#bucket_exists?(bucket_name) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/bosh_cli_plugin_aws/s3.rb', line 40

def bucket_exists?(bucket_name)
  bucket_names.include?(bucket_name)
end

#bucket_namesObject



36
37
38
# File 'lib/bosh_cli_plugin_aws/s3.rb', line 36

def bucket_names
  aws_s3.buckets.map &:name
end

#copy_remote_file(bucket_name, remote_file, file_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bosh_cli_plugin_aws/s3.rb', line 62

def copy_remote_file(bucket_name, remote_file, file_name)
  say("Fetching remote file #{remote_file} from #{bucket_name} bucket")
  bucket = aws_s3.buckets[bucket_name]
  object = bucket.objects[remote_file]
  release_file = Tempfile.new file_name
  Bosh::Cli::FileWithProgressBar.open(release_file, 'wb') do |f|
    f.size=object.content_length
    object.read do |chunk|
      f.write chunk
    end
  end
  release_file
rescue AWS::S3::Errors::NoSuchKey => e
  new_exception = Exception.new("Can't find #{remote_file} in bucket #{bucket_name}")
  new_exception.set_backtrace(e.backtrace)
  raise new_exception
end

#create_bucket(bucket_name) ⇒ Object



8
9
10
# File 'lib/bosh_cli_plugin_aws/s3.rb', line 8

def create_bucket(bucket_name)
  aws_s3.buckets.create(bucket_name)
end

#delete_bucket(bucket_name) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/bosh_cli_plugin_aws/s3.rb', line 18

def delete_bucket(bucket_name)
  bucket = fetch_bucket(bucket_name)

  bucket.clear!
  bucket.delete
rescue AWS::S3::Errors::NoSuchBucket
end

#emptyObject



26
27
28
29
30
31
32
33
34
# File 'lib/bosh_cli_plugin_aws/s3.rb', line 26

def empty
  aws_s3.buckets.each do |bucket|
    begin
      bucket.delete!
    rescue AWS::S3::Errors::NoSuchBucket
      # when the bucket goes away while going through the list
    end
  end
end

#fetch_object_contents(bucket_name, object_name) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/bosh_cli_plugin_aws/s3.rb', line 53

def fetch_object_contents(bucket_name, object_name)
  bucket = fetch_bucket(bucket_name)
  Bosh::Common.retryable(on: AWS::S3::Errors::NoSuchBucket, tries: 10) do
    bucket.objects[object_name].read
  end
rescue AWS::S3::Errors::NoSuchKey
  nil
end

#move_bucket(old_bucket, new_bucket) ⇒ Object



12
13
14
15
16
# File 'lib/bosh_cli_plugin_aws/s3.rb', line 12

def move_bucket(old_bucket, new_bucket)
  fetch_bucket(old_bucket).objects.each do |object|
    object.move_to(object.key, :bucket_name => new_bucket)
  end
end

#objects_in_bucket(bucket_name) ⇒ Object



49
50
51
# File 'lib/bosh_cli_plugin_aws/s3.rb', line 49

def objects_in_bucket(bucket_name)
  fetch_bucket(bucket_name).objects.map { |object| object.key }
end

#upload_to_bucket(bucket_name, object_name, io) ⇒ Object



44
45
46
47
# File 'lib/bosh_cli_plugin_aws/s3.rb', line 44

def upload_to_bucket(bucket_name, object_name, io)
  bucket = fetch_bucket(bucket_name)
  bucket.objects[object_name].write(io)
end