Class: RestFtpDaemon::Remote::RemoteS3

Inherits:
RemoteBase
  • Object
show all
Includes:
CommonHelpers
Defined in:
lib/rest-ftp-daemon/remote/s3.rb

Instance Attribute Summary collapse

Attributes inherited from RemoteBase

#job, #log_prefix

Instance Method Summary collapse

Methods included from CommonHelpers

#dashboard_url, #exception_to_error, #format_bytes, #identifier, #underscore

Methods inherited from RemoteBase

#chdir_or_create, #close, #initialize, #prepare, #remove!

Constructor Details

This class inherits a constructor from RestFtpDaemon::Remote::RemoteBase

Instance Attribute Details

#clientObject (readonly)

Class options



10
11
12
# File 'lib/rest-ftp-daemon/remote/s3.rb', line 10

def client
  @client
end

#targetObject (readonly)

Returns the value of attribute target.



11
12
13
# File 'lib/rest-ftp-daemon/remote/s3.rb', line 11

def target
  @target
end

Instance Method Details

#connectObject

def prepare end



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rest-ftp-daemon/remote/s3.rb', line 16

def connect
  super

  # Connect init
  log_debug "connect region[#{target.aws_region}] id[#{target.aws_id}]"

  # Connect remote server
  @client = Aws::S3::Client.new(
    region: @target.aws_region,
    credentials: Aws::Credentials.new(@target.aws_id, @target.aws_secret),
    http_wire_trace: @debug
    )
end

#connected?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rest-ftp-daemon/remote/s3.rb', line 57

def connected?
  !@client.nil?
end

#size_if_exists(target) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/rest-ftp-daemon/remote/s3.rb', line 30

def size_if_exists target
  log_debug "size_if_exists [#{target.path}]"
  object = @client.get_object(bucket: target.aws_bucket, key: target.path)
rescue Aws::S3::Errors::NotFound => e
  return false
else
  return object.content_length
end

#upload(source, target, use_temp_name = false, &callback) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rest-ftp-daemon/remote/s3.rb', line 39

def upload source, target, use_temp_name = false, &callback
  # Push init
  raise RestFtpDaemon::AssertionFailed, "upload/client" if @client.nil?
  log_debug "upload bucket[#{target.aws_bucket}] path[#{target.path}]"

  # Do the transfer, passing the file to the best method
  File.open(source.path_fs, 'r', encoding: 'BINARY') do |file|
    if file.size >= JOB_S3_MIN_PART
      upload_multipart  file, target.aws_bucket, target.path, target.name, &callback
    else
      upload_onefile    file, target.aws_bucket, target.path, target.name, &callback
    end
  end

  # We're all set
  log_debug "RemoteS3.upload done"
end