Class: GoodDataS3::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gooddata_s3/client.rb', line 16

def initialize config = {}

  public_key = config[:public_key]
  private_key = config[:private_key] || config[:secret_key]
  proposed_bucket = S3Bucket.new(config[:bucket] || 'gooddata_connector')

  @bucket_name = proposed_bucket.name
  @directories = proposed_bucket.directories

  AWS.config(:access_key_id => public_key, :secret_access_key => private_key)

  @s3 = AWS::S3::Client.new(region: 'us-east-1')

  begin

    bucket_exists = @s3.list_buckets.include? @bucket_name

    if bucket_exists == false
      bucket = @s3.create_bucket(:bucket_name => @bucket_name)
    else
      bucket = bucket_exists
    end

  rescue Exception => exp
    puts "#{Time.now} => Attempted to list all buckets, recieved: #{exp}"
    puts "#{Time.now} => Failed to access all buckets and/or create bucket, ensuring minimal write acccess to \"#{@bucket_name}\""

    # Create and upload test file.
    test_access_file = SecureRandom.hex
    puts "#{Time.now} Creating test file \"#{test_access_file}\""
    file = File.open(test_access_file, 'w+')
    file.puts 'test'
    self.upload(test_access_file)

    # Remove test file from remote and local
    self.delete(test_access_file)
    file = File.delete(test_access_file) if File.exists?(test_access_file)

  end

  puts "#{Time.now} => Sucessfully authenticated read/write to S3 Bucket: \"#{@bucket_name}\""
  unless @directories.empty?
    puts "#{Time.now} => Assigned default directory location: \"#{@directories.join('/')}\""
  end

  raise 'ERROR! :public_key, :private_key must be passed for configuration.' unless public_key && private_key

end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



11
12
13
# File 'lib/gooddata_s3/client.rb', line 11

def bucket
  @bucket
end

#bucket_nameObject

Returns the value of attribute bucket_name.



10
11
12
# File 'lib/gooddata_s3/client.rb', line 10

def bucket_name
  @bucket_name
end

#directoriesObject Also known as: directory

Returns the value of attribute directories.



12
13
14
# File 'lib/gooddata_s3/client.rb', line 12

def directories
  @directories
end

Instance Method Details

#create_directory(name) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/gooddata_s3/client.rb', line 119

def create_directory name
  resp = @s3.put_object(
      data:'',
      bucket_name: @bucket_name,
      key: name
  )
end

#delete(file_path, config = {}) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/gooddata_s3/client.rb', line 240

def delete file_path, config = {}

  begin

    if @directories.any?
      puts "#{Time.now} => Using default directory \"#{@directories.join('/')}\""

      statement = file_path.split('/')
      local_file = statement.pop
      directory = @directories.join('/')
      key = directory+"/"+statement.join('/')+"/"+local_file

    elsif @directories.empty?

      directory = config[:directory] || ''
      key = directory+file_path

    else

      key = file_path

    end

    #key = "/#{key}" unless key[0] == '/'

    puts "#{Time.now} => Deleting from S3 Bucket (#{@bucket_name}): \"#{key}\""
    @s3.delete_object(bucket_name: @bucket_name, key: key)

  rescue Exception => exp
    puts exp

  end


end

#download(file_path, config = {}) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/gooddata_s3/client.rb', line 127

def download file_path, config ={}

  begin

    if @directories.any?
      puts "#{Time.now} => Using default directory \"#{@directories.join('/')}\""

      statement = file_path.split('/')
      local_file = statement.pop
      directory = @directories.join('/')
      key = directory+"/"+statement.join('/')+"/"+local_file

    elsif @directories.empty?

      directory = config[:directory] || ''
      key = directory+file_path

    else

      key = file_path

    end

    puts "#{Time.now} => Downloading from S3 Bucket (#{@bucket_name}): \"#{key}\""

    #key = "/#{key}" unless key[0] == '/'

    resp = @s3.get_object(bucket_name: @bucket_name, key:key)
    resp[:data]

  rescue Exception => exp
    puts exp

  end
end

#exists?(file) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)


229
230
231
232
233
234
235
236
# File 'lib/gooddata_s3/client.rb', line 229

def exists? file
  begin
    @s3.get_object(bucket_name: @bucket_name, key:file)
    true
  rescue AWS::S3::Errors::NoSuchKey
    false
  end
end

#get_config(config = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/gooddata_s3/client.rb', line 163

def get_config config = {}

  file = config[:file] || 'gooddata_connector_config.json'

  if self.exists? file

    json = JSON.parse(self.download(file), :symbolize_names => true)

    File.open(file,'w'){ |f| JSON.dump(json, f) }

    self.download(file)

    json

  else

    json = {
        :id => SecureRandom.uuid,
        :updated => Time.now.to_s,
        :initial_load_get_multiple => false,
        :initial_load_get_changes => false
    }

    File.open(file,'w'){ |f| JSON.dump(json, f) }

    self.upload(file)

    json

  end

end

#latest_configObject



222
223
224
225
226
227
# File 'lib/gooddata_s3/client.rb', line 222

def latest_config
  if File.exists?('gooddata_connector_config.json')
    File.delete('gooddata_connector_config.json')
  end
  self.download('gooddata_connector_config.json')
end

#set_config(config = {}) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/gooddata_s3/client.rb', line 196

def set_config config = {}

  if config[:file]
    file = config.delete(:file)
  else
    file = 'gooddata_connector_config.json'
  end

  if self.exists? file
    json = JSON.parse(self.download(file), :symbolize_names => true)
  else
    json = Hash.new
  end

  new_json = json.merge(config)

  new_json[:updated] = Time.now.to_s

  File.open(file,'w'){ |f| JSON.dump(new_json, f) }

  self.upload(file)

  new_json

end

#testObject



65
66
67
68
69
70
71
72
73
# File 'lib/gooddata_s3/client.rb', line 65

def test
  begin
    self.exists? 'tmp_dumb_file'
    puts "#{Time.now} => SETUP: Connect to AWS S3 Bucket:#{self.bucket_name}...success!"
    true
  rescue
    false
  end
end

#upload(file_path, config = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gooddata_s3/client.rb', line 75

def upload file_path, config = {}

  if File.exists? file_path

    if @directories.empty?
      directory = config[:directory] || ''
    else
      puts "#{Time.now} => Using default directory \"#{@directories.join('/')}\""
      directory = @directories.join('/')
    end

    puts local_file = file_path.split('/')[-1]

  else

    statement = file_path.split('/')
    local_file = statement.pop

    if @directories.empty?
      directory = statement.join('/')
    else
      puts "#{Time.now} => Using default directory \"#{@directories.join('/')}\""
      default_directory = @directories.join('/')
      directory = default_directory+"/"+statement.join('/')
    end

  end

  raise 'Could not find local file...' unless File.exists? local_file

  if directory.empty?
    key = local_file
  else
    key = directory+"/"+local_file
  end

  puts "#{Time.now} => Uploading local file \"#{local_file}\" to S3 Bucket \"#{@bucket_name}\" (\"#{key}\")"
  resp = @s3.put_object(
      data: IO.read(local_file),
      bucket_name: @bucket_name,
      key: key
  )
end