Class: MultiSync::AwsTarget

Inherits:
Target
  • Object
show all
Defined in:
lib/multi_sync/targets/aws_target.rb

Instance Method Summary collapse

Methods inherited from Target

#default_credentials

Methods included from Mixins::LogHelper

#class_name

Instance Method Details

#delete(resource) ⇒ Object



57
58
59
60
61
62
# File 'lib/multi_sync/targets/aws_target.rb', line 57

def delete(resource)
  MultiSync.say_status :delete, resource.path_without_root.to_s
  MultiSync.debug "Delete #{resource.class_name}:'#{resource.path_without_root}' from #{class_name}:'#{File.join('/', target_dir + destination_dir)}'"
  connection.delete_object(target_dir.to_s, (destination_dir + resource.path_without_root).to_s)
  resource
end

#filesObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/multi_sync/targets/aws_target.rb', line 11

def files
  files = []

  directory = connection.directories.get(target_dir.to_s, prefix: destination_dir.to_s)
  return files if directory.nil?

  directory.files.lazily.each { |file|

    pathname = Pathname.new(file.key)

    # eg directory or overreaching AWS globbing
    next unless valid_path?(pathname)

    files << MultiSync::RemoteResource.new(
      file: file,
      path_with_root: target_dir + pathname, # pathname seems to already have the prefix (destination_dir)
      path_without_root: destination_dir != '' ? pathname.relative_path_from(destination_dir).cleanpath : pathname
    )

  }

  files
end

#upload(resource) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/multi_sync/targets/aws_target.rb', line 35

def upload(resource)
  MultiSync.say_status :upload, resource.path_without_root.to_s
  MultiSync.debug "Upload #{resource.class_name}:'#{resource.path_without_root}' to #{class_name}:'#{File.join('/', target_dir + destination_dir)}'"
  directory = connection.directories.get(target_dir.to_s)
  return if directory.nil?

  upload_hash = {
    key: (destination_dir + resource.path_without_root).to_s,
    body: resource.body,
    content_type: resource.content_type,
    content_md5: Digest::MD5.base64digest(resource.body)
  }

  MultiSync::Resource::AWS_ATTRIBUTES.each do |attribute_hash|
    upload_hash[attribute_hash[:name]] = resource.send(attribute_hash[:name])
  end

  directory.files.create(upload_hash)

  resource
end