Class: Cloudsync::Backend::S3

Inherits:
Base
  • Object
show all
Defined in:
lib/cloudsync/backend/s3.rb

Constant Summary

Constants inherited from Base

Base::BUCKET_LIMIT, Base::CONTAINER_LIMIT, Base::OBJECT_LIMIT

Instance Attribute Summary

Attributes inherited from Base

#name, #store, #sync_manager, #upload_prefix

Instance Method Summary collapse

Methods inherited from Base

#copy, #needs_update?, #to_s

Constructor Details

#initialize(opts = {}) ⇒ S3

Returns a new instance of S3.



6
7
8
9
10
# File 'lib/cloudsync/backend/s3.rb', line 6

def initialize(opts={})
  @store = RightAws::S3.new(opts[:username],
                            opts[:password])
  super
end

Instance Method Details

#delete(file, delete_bucket_if_empty = true) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cloudsync/backend/s3.rb', line 53

def delete(file, delete_bucket_if_empty=true)
  $LOGGER.info("Deleting #{file}")
  return if dry_run?

  get_obj_from_store(file).delete

  if bucket = @store.bucket(file.bucket)
    bucket.key(file.download_path).delete
  
    if delete_bucket_if_empty && bucket.keys.empty?
      $LOGGER.debug("Deleting empty bucket '#{bucket.name}'")
      bucket.delete
    end
  end
rescue RightAws::AwsError => e
  $LOGGER.error("Caught error: #{e} trying to delete #{file}")
end

#download(file) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cloudsync/backend/s3.rb', line 27

def download(file)
  start_time = Time.now
  $LOGGER.info("Downloading file #{file} (#{file.path})")

  tempfile = file.tempfile

  if !dry_run?
    @store.interface.get(file.bucket, file.download_path) do |chunk|
      tempfile.write chunk
    end
  end

  tempfile.close

  $LOGGER.debug("Finished downloading file #{file} from #{self} (#{Time.now - start_time})")

  tempfile
rescue RightAws::AwsError => e
  $LOGGER.error("Caught error: #{e} (#{file})")
  if e.message =~ /NoSuchKey/
    tempfile.unlink and return nil
  else
    raise
  end
end

#files_to_sync(upload_prefix = "") ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cloudsync/backend/s3.rb', line 71

def files_to_sync(upload_prefix="")
  $LOGGER.info("Getting files to sync [#{self}]")
  
  buckets_to_sync(upload_prefix).inject([]) do |files, bucket|
    objects_from_bucket(bucket, upload_prefix) do |key|
      file = Cloudsync::File.from_s3_obj(key, self.to_s)
      if block_given?
        yield file
      else
        files << file
      end
    end
    files
  end
end

#get_file_from_store(file) ⇒ Object

Convenience to grab a single file



88
89
90
# File 'lib/cloudsync/backend/s3.rb', line 88

def get_file_from_store(file)
  Cloudsync::File.from_s3_obj( get_obj_from_store(file), self.to_s )
end

#put(file, local_filepath) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cloudsync/backend/s3.rb', line 12

def put(file, local_filepath)
  start_time = Time.now
  $LOGGER.info("Putting #{file} to #{self} (#{file.full_upload_path}).")
  return if dry_run?

  # Forces creation of the bucket if necessary
  get_or_create_obj_from_store(file)

  local_file = ::File.open(local_filepath)
  @store.interface.put(file.bucket, file.upload_path, local_file)
  local_file.close

  $LOGGER.debug("Finished putting #{file} to #{self} (#{Time.now - start_time})")
end