Class: SmoothS3::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/smooth_s3/bucket.rb

Class Method Summary collapse

Class Method Details

.create(bucket_name, service) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/smooth_s3/bucket.rb', line 23

def self.create(bucket_name, service)
  begin
    new_bucket = service.proxy_service.buckets.build(bucket_name)
    new_bucket.save
  rescue S3::Error::BucketAlreadyExists
    raise SmoothS3::Error, "A bucket named '#{bucket_name}' already exists in the Global S3 Namespace. Please select one of you existing buckets or try a new name."
  end

  Service.new_buckets[service.aws_key] << new_bucket
end

.exists?(bucket, service) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/smooth_s3/bucket.rb', line 4

def self.exists?(bucket, service)
  service.refresh.buckets.keys.include? bucket
end

.file_exists?(file, bucket, service) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
# File 'lib/smooth_s3/bucket.rb', line 8

def self.file_exists?(file, bucket, service)
  b = service.buckets[bucket]

  begin
    b.objects.find_first(file)
    return true
  rescue
    return false
  end
end

.select(bucket, service) ⇒ Object



19
20
21
# File 'lib/smooth_s3/bucket.rb', line 19

def self.select(bucket, service)
  Bucket.create(bucket, service) unless Bucket.exists?(bucket, service)
end

.store_file(file, remote_file, bucket, service, options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/smooth_s3/bucket.rb', line 34

def self.store_file(file, remote_file, bucket, service, options)
  b = service.refresh.buckets[bucket]

  if options[:prefix]
    remote_file = options[:prefix] =~ /\/$/ ? (options[:prefix] + remote_file) : options[:prefix] + "/" + remote_file
  end

  unless options[:overwrite] == true
    if Bucket.file_exists?(remote_file, bucket, service)
      puts "'#{remote_file}' already exists on S3 bucket named '#{bucket}'. Use the bang(!) version of the method to overwrite."
      return
    end
  end

  bo = b.objects.build(remote_file)
  bo.content = open(file)
 
  if Bucket.save_with_retries(3, bo, file)
    puts "'#{file}' was uploaded to S3 bucket '#{bucket}' under the name '#{remote_file}'."
  else
    puts "Impossible to upload '#{file}' to S3 (retries were attempted). Please check file permissions."
  end
end