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.configuration.aws_access_key_id, aws_secret_access_key: Upcloudify.configuration.aws_secret_access_key, aws_directory: Upcloudify.configuration.aws_directory, }) ⇒ S3

Returns a new instance of S3.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/upcloudify.rb', line 33

def initialize(options = {
    aws_access_key_id: Upcloudify.configuration.aws_access_key_id,
    aws_secret_access_key: Upcloudify.configuration.aws_secret_access_key,
    aws_directory: Upcloudify.configuration.aws_directory,
})
  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]
end

Instance Method Details

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

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



48
49
50
51
52
53
54
55
56
# File 'lib/upcloudify.rb', line 48

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: "", 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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/upcloudify.rb', line 74

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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/upcloudify.rb', line 60

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