Class: S3Dir::Uploader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, key, options) ⇒ Uploader

Returns a new instance of Uploader.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/s3_dir.rb', line 44

def initialize dir, key, options
  @files_path = File.expand_path(dir)

  # Merge defaults with passed-in options
  settings = {credential: ENV['FOG_CREDENTIAL'],
              private: false}.merge(options)

  # Configure Fog
  Fog.credential = settings[:credential]

  # Get a region
  region = Fog.credentials[:region] || 'us-west-2'

  # If we don't specify this endpoint, Fog will complain about
  # not using the correct endpoint if the bucket has dots in
  # the name (i.e. website bucket)
  endpoint = 'http://s3.amazonaws.com'

  # This may be a public bucket
  @is_public = !settings[:private]

  # Set up our storage object
  # We have to specify path_style here because Fog will complain about
  # our website bucket (if we're using a bucket with dots in the name)
  # not being covered by the SSL certificate.
  fog_options = Fog.credentials.merge({provider: 'aws', path_style: true,
                                       region: region, endpoint: endpoint})
  fog_options.delete(:key_name)
  @storage = Fog::Storage.new(fog_options)
  @bucket = storage.directories.get(key)
  @bucket ||= storage.directories.create(key: key, public: is_public)
  @key = key
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



39
40
41
# File 'lib/s3_dir.rb', line 39

def bucket
  @bucket
end

#files_pathObject (readonly)

Returns the value of attribute files_path.



42
43
44
# File 'lib/s3_dir.rb', line 42

def files_path
  @files_path
end

#is_publicObject (readonly)

Returns the value of attribute is_public.



41
42
43
# File 'lib/s3_dir.rb', line 41

def is_public
  @is_public
end

#keyObject (readonly)

Returns the value of attribute key.



38
39
40
# File 'lib/s3_dir.rb', line 38

def key
  @key
end

#storageObject (readonly)

Returns the value of attribute storage.



40
41
42
# File 'lib/s3_dir.rb', line 40

def storage
  @storage
end

Instance Method Details

#uploadObject



78
79
80
81
82
83
84
# File 'lib/s3_dir.rb', line 78

def upload
  Dir.chdir(files_path) do
    Dir['**/*'].each do |entry|
      File.directory?(entry) ? create_directory(entry) : create_file(entry)
    end
  end
end