Class: Cloudcrypt::S3Transfer

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudcrypt/s3_transfer.rb

Instance Method Summary collapse

Constructor Details

#initialize(aws_access_key_id, aws_secret_access_key) ⇒ S3Transfer

Returns a new instance of S3Transfer.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cloudcrypt/s3_transfer.rb', line 4

def initialize(aws_access_key_id,aws_secret_access_key)
    begin 
      #Gem.clear_paths
      require 'fog'
    rescue LoadError
      #Chef::Log.warn("Missing gem 'fog'")
    end
    @s3=Fog::AWS::Storage.new(
    	:aws_access_key_id=> aws_access_key_id,
    	:aws_secret_access_key => aws_secret_access_key
    )
    
end

Instance Method Details

#delete(file_basename, bucket = RAVE_BUCKET) ⇒ Object



48
49
50
51
# File 'lib/cloudcrypt/s3_transfer.rb', line 48

def delete(file_basename,bucket=RAVE_BUCKET)
    abort("S3://#{bucket}/#{file_basename} doesn't exist") unless file_exists?(file_basename,bucket)
    @s3.directories.get(bucket).files.get(file_basename).destroy
end

#download(file_basename, destination, bucket = RAVE_BUCKET) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cloudcrypt/s3_transfer.rb', line 32

def download(file_basename,destination,bucket=RAVE_BUCKET)
    abort("S3://#{bucket}/#{file_basename} doesn't exist") unless file_exists?(file_basename,bucket)
    # this produces a NoMemoryError: failed to allocate memory
    #File.open(destination,'wb') { |f| f << @s3.directories.get(bucket).files.get(file_basename).body } 
    
    File.open(destination,'wb') do |file|
        @s3.directories.get(bucket).files.get(file_basename) do |chunk|
            file << chunk
        end
    end
end

#list(bucket = RAVE_BUCKET) ⇒ Object



44
45
46
# File 'lib/cloudcrypt/s3_transfer.rb', line 44

def list(bucket=RAVE_BUCKET)
    @s3.directories.get(bucket).files
end

#md5(file_basename, bucket = RAVE_BUCKET) ⇒ Object



27
28
29
30
# File 'lib/cloudcrypt/s3_transfer.rb', line 27

def md5(file_basename,bucket=RAVE_BUCKET)
    return false unless file_exists?(file_basename,bucket)
    return @s3.directories.get(bucket).files.get(file_basename).etag
end

#upload(file_uri, bucket = RAVE_BUCKET) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/cloudcrypt/s3_transfer.rb', line 18

def upload(file_uri,bucket=RAVE_BUCKET)
    file_basename = File.basename(file_uri)
    abort("S3://#{bucket}/#{file_basename} already exist") if file_exists?(file_basename,bucket)
    f=@s3.directories.get(bucket).files.new(:key => file_basename, :body=>File.open(file_uri,'rb').read)
    f.save
    f.etag
    File.delete(file_uri)
end