Class: Upcloudify::S3

Inherits:
Object
  • Object
show all
Includes:
GemConfig::Base
Defined in:
lib/upcloudify.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = { aws_access_key_id: Upcloudify::S3.configuration.aws_access_key_id, aws_secret_access_key: Upcloudify::S3.configuration.aws_secret_access_key, aws_directory: Upcloudify::S3.configuration.aws_directory, aws_region: Upcloudify::S3.configuration.aws_region }) ⇒ S3

Returns a new instance of S3.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/upcloudify.rb', line 39

def initialize(options = {
    aws_access_key_id: Upcloudify::S3.configuration.aws_access_key_id,
    aws_secret_access_key: Upcloudify::S3.configuration.aws_secret_access_key,
    aws_directory: Upcloudify::S3.configuration.aws_directory,
    aws_region: Upcloudify::S3.configuration.aws_region
})
  raise ArgumentError, "aws_access_key_id is required" unless options[:aws_access_key_id]
  raise ArgumentError, "aws_secret_access_key is required" unless options[:aws_secret_access_key]
  raise ArgumentError, "aws_directory is required" unless options[:aws_directory]
  @id = options[:aws_access_key_id]
  @secret = options[:aws_secret_access_key]
  @directory = options[:aws_directory]
  @region = options[:aws_region]
end

Instance Method Details

#cloud(connection = Fog::Storage.new( :provider => 'AWS', :aws_secret_access_key => @secret, :aws_access_key_id => @id, :region => @region )) ⇒ Object

Connects to Amazon S3 using stored credentials Returns an object handle for the S3 bucket-directory



56
57
58
59
60
61
62
63
64
65
# File 'lib/upcloudify.rb', line 56

def cloud(connection = Fog::Storage.new(
    :provider                 => 'AWS',
    :aws_secret_access_key    => @secret,
    :aws_access_key_id        => @id,
    :region                   => @region
  )
)
  directory = connection.directories.get(@directory)
  directory.files
end

#email(email_address, filename, attachment, options = { suffix: "", expiration: (Date.today + 7).to_time, from: 'upcloudify', subject: 'your file is attached', body: 'your report is linked ' }) ⇒ Object

Uploads a file to S3 and emails a link to the file. Returns nothing.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/upcloudify.rb', line 83

def email(email_address,
  filename,
  attachment,
  options = {
    suffix: "",
    expiration: (Date.today + 7).to_time,
    from: 'upcloudify',
    subject: 'your file is attached',
    body: 'your report is linked '
  }
)

  suffix = options[:suffix]
  expiration = options[:expiration]
  file = upload((filename.to_s + suffix.to_s), attachment)

  Pony.mail to: email_address,
    from: options[:from],
    subject: options[:subject],
    body: (options[:body] || '') + file.url(expiration) + ' '

end

#upload(filename, data) ⇒ Object

Uploads data into Amazon S3 Returns an object representing the uploaded file



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/upcloudify.rb', line 69

def upload(filename, data)
  filename.gsub!(/\//, '-') # replace slashes with dashes
  filename.gsub!(/^\-|\-$/, '') # remove leading and trailing dashes

  file = cloud.create(
    :key    => "#{filename}.zip",
    :body   => Zippy.new("#{filename}" => data).data,
    :public => false
  )
  file
end