Module: Platformx::S3Helpers

Defined in:
lib/platformx/aws.rb

Overview

Amazon S3 helpers

Author:

  • Tim Mushen

Instance Method Summary collapse

Instance Method Details

Note:

The returned public URL will expire in 7 days

Download link from S3 asset

Parameters:

  • key (String) (defaults to: "")

    key or the file name

  • bucket (String) (defaults to: "")

    the bucket name

  • bucket_path (String) (defaults to: "")

    the bucket path

  • verbose (Boolean) (defaults to: false)

    make operation verborse

Returns:

  • (String)

    a public URL for the file



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/platformx/aws.rb', line 56

def x_s3_get_link(key: "", bucket: "", bucket_path: "", verbose: false)
  
  connection = x_s3_init
  expiry = Time.now.to_i + 604800
  connection.directories.new(:key => "#{bucket}").files.new(:key => "#{bucket_path}/#{key}").url(expiry)
 
  if verbose == true
    # returns more information about file from s3
    # directory = connection.directories.get("files.myclocktower.com")
    # file = directory.files.get("support/#{support_id}/#{key}")
    #return file.url(expiry)
  end 
end

#x_s3_initFog::Storage

Set up Amazon S3 connection

Returns:

  • (Fog::Storage)

    S3 storage connection



13
14
15
16
17
18
19
20
21
# File 'lib/platformx/aws.rb', line 13

def x_s3_init()
  connection = Fog::Storage.new({
    :provider                 => 'AWS',
    :region                   => Platformx.configuration.aws_region,
    :aws_access_key_id        => Platformx.configuration.aws_access_key_id,
    :aws_secret_access_key    => Platformx.configuration.aws_secret_access_key
  })
  return connection
end

#x_s3_upload(new_filename: "", file: "", bucket: "#{Platformx.configuration.aws_bucket}", path: "") ⇒ Object

Upload file to S3 with the given parameters

Parameters:

  • new_filename (String) (defaults to: "")

    new name of the file

  • file (String) (defaults to: "")

    file to be uploaded

  • bucket (String) (defaults to: "#{Platformx.configuration.aws_bucket}")

    bucket to upload the file to

  • path (String) (defaults to: "")

    path of the final file

Returns:

  • S3 bucket file



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

def x_s3_upload(new_filename: "", file: "", bucket: "#{Platformx.configuration.aws_bucket}", path: "")
  # Obtaining an S3 connection
  connection = x_s3_init

  # Creating the directory to upload the file to
  bucket = connection.directories.create(key: "#{bucket}/#{path}", public: false)

  # upload that resume
  file = bucket.files.create(
    :key    => "#{new_filename}",
    :body   => open(file),
    :public => false
  )
end