Class: Upcloudify::S3

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of S3.



20
21
22
23
24
25
26
27
28
# File 'lib/upcloudify.rb', line 20

def initialize(options = {
    s3_id: Upcloudify.configuration.aws_access_key_id,
    s3_secret: Upcloudify.configuration.aws_secret_access_key,
    s3_directory: Upcloudify.configuration.aws_directory
})
  @id = options[:s3_id]
  @secret = options[:s3_secret]
  @directory = options[:s3_directory]
end

Instance Method Details

#cloudObject

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



32
33
34
35
36
37
38
39
40
# File 'lib/upcloudify.rb', line 32

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

#email(email_address, filename, attachment, options = { suffix: " generated on #{Time.now.to_s}-#{Rails.env}", expiration: Time.now.tomorrow, from: 'upcloudify', subject: 'your file is attached', body: 'your report is linked ' }) ⇒ Object

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/upcloudify.rb', line 55

def email(email_address,
  filename,
  attachment,
  options = {
    suffix: " generated on #{Time.now.to_s}-#{Rails.env}",
    expiration: Time.now.tomorrow,
    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).parameterize, attachment)

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

end

#upload(filename, data) ⇒ Object

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



44
45
46
47
48
49
50
51
# File 'lib/upcloudify.rb', line 44

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