Class: RestFtpDaemon::RemoteS3

Inherits:
Remote
  • Object
show all
Defined in:
lib/rest-ftp-daemon/remote_s3.rb

Constant Summary collapse

MULTIPART_THRESHOLD_MB =
4

Instance Attribute Summary collapse

Attributes inherited from Remote

#job, #log_prefix, #logger

Instance Method Summary collapse

Methods inherited from Remote

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

Constructor Details

This class inherits a constructor from RestFtpDaemon::Remote

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rest-ftp-daemon/remote_s3.rb', line 18

def connect
  super

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

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

#connected?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/rest-ftp-daemon/remote_s3.rb', line 71

def connected?
  !@client.nil?
end

#prepareObject



13
14
15
16
# File 'lib/rest-ftp-daemon/remote_s3.rb', line 13

def prepare
  @multipart_threshold = MULTIPART_THRESHOLD_MB.to_i * 1024 * 1024
  log_debug "RemoteS3.prepare target[#{@target.inspect}] #{@multipart_threshold}"
end

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



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
64
65
66
67
68
69
# File 'lib/rest-ftp-daemon/remote_s3.rb', line 33

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

  # Update progress before
  #yield 0, target.name
  # Point to the right bucket and object
  bucket = @client.bucket(target.aws_bucket)
  object = bucket.object(target.name)

  # Do the transfer
  object.upload_file(source.path, {
    multipart_threshold: @multipart_threshold
    })

  # Wait for transfer to complete
  object.wait_until_exists do |waiter|
    # waiter.delay = 1
    # # log_debug "- progress[#{progress}] total[#{total}]"
    # waiter.before_wait do |attempts, response|
    #   puts "#{attempts} made"
    #   puts response.error.inspect
    #   puts response.data.inspect
    # end
    # log_debug "- progress[] #{waiter.inspect}"
  end

  # Update progress after
  #yield target.size, target.name

  # Dump information about this file
  log_debug "RemoteS3.upload url[#{object.public_url}]"
  log_debug "RemoteS3.upload etag[#{object.etag}]"
  set_info :target_aws_public_url, object.public_url
  set_info :target_aws_etag, object.etag
end