Class: S3sync::S3Node

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

Overview

———- S3Node ———- #

Instance Attribute Summary

Attributes inherited from Node

#name, #size, #tag

Instance Method Summary collapse

Methods inherited from Node

#directory?

Constructor Details

#initialize(bucket, prefix, itemOrName) ⇒ S3Node

Returns a new instance of S3Node.



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/s3sync/s3sync.rb', line 433

def initialize(bucket, prefix, itemOrName)
	@bucket = bucket
	if itemOrName.kind_of? String
		@name = itemOrName
		@name.sub!(%r{/$}, "") # don't create directories with a slash on the end
		#6/2007. the prefix can be filled but the name empty, in the case of s3sync -r somedir somebucket:
		if (not prefix.empty? and @name.empty?)
			@name = prefix
			itemOrName = prefix
			prefix = ""
		end
		slash = prefix.empty? ? "" : "/"
		@path = prefix + slash + itemOrName
	else
		@name = (itemOrName.name.slice((prefix.length)..itemOrName.name.length) or '')
		# depending whether the prefix is / tailed, the name might need trimming
		@name.sub!(%r{^/},"") # get rid of leading slash in name if there (from above simplistic split)
		@name.sub!(%r{/$}, "") # don't create directories with a slash on the end
		@path = itemOrName.name
		@path.sub!(%r{/$}, "") # don't create directories with a slash on the end
		@size = itemOrName.size
		@tag = itemOrName.tag.gsub(/"/,'')
	end
	debug("s3 node object init. Name:#{@name} Path:#{@path} Size:#{@size} Tag:#{@tag}")
end

Instance Method Details

#deleteObject



524
525
526
# File 'lib/s3sync/s3sync.rb', line 524

def delete
	@result = S3sync.S3try(:delete, @bucket, @path)
end

#groupObject



477
478
479
480
481
482
# File 'lib/s3sync/s3sync.rb', line 477

def group
	unless @result
		@result = S3sync.S3try(:head, @bucket, @path)
	end
	@result.object.['group'].to_i # 0 default ok
end

#ownerObject



470
471
472
473
474
475
476
# File 'lib/s3sync/s3sync.rb', line 470

def owner
	unless @result
		@result = S3sync.S3try(:head, @bucket, @path)
	end
	debug("Owner of this s3 node is #{@result.object.['owner']}")
	@result.object.['owner'].to_i # if not there, will be nil => 0 which == root so good default
end

#permissionsObject



483
484
485
486
# File 'lib/s3sync/s3sync.rb', line 483

def permissions
	g = @result.object.['permissions']
	g ? g.to_i : 600 # default to owner only
end

#symlink?Boolean

Returns:

  • (Boolean)


463
464
465
466
467
468
469
# File 'lib/s3sync/s3sync.rb', line 463

def symlink?() 
	unless @result
		@result = S3sync.S3try(:head, @bucket, @path)
	end
	debug("symlink value is: #{@result.object.['symlink']}")
	@result.object.['symlink'] == 'true'
end

#to_stream(s) ⇒ Object

get this item from s3 into the provided stream S3 pushes to the local item, due to how http streaming is implemented



460
461
462
# File 'lib/s3sync/s3sync.rb', line 460

def to_stream(s) 
	@result = S3sync.S3try(:get_stream, @bucket, @path, {}, s)
end

#updateFrom(fromNode) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/s3sync/s3sync.rb', line 487

def updateFrom(fromNode)
	if fromNode.respond_to?(:stream)
		meta = Hash.new
		meta['owner'] = fromNode.owner.to_s
		meta['group'] = fromNode.group.to_s
		meta['permissions'] = fromNode.permissions.to_s
		meta['symlink'] = 'true' if fromNode.symlink?
		begin
			theStream = fromNode.stream
			theStream = ProgressStream.new(theStream, fromNode.size) if $S3syncOptions['--progress']

			s3o = S3::S3Object.new(theStream, meta)
			debug(@path)
			headers = {'Content-Length' => (fromNode.size.respond_to?(:nonzero?) ? fromNode.size.to_s : '0')}
			headers['x-amz-acl'] = 'public-read' if $S3syncOptions['--public-read']
			headers['Expires'] = $S3syncOptions['--expires'] if $S3syncOptions['--expires']
			headers['Cache-Control'] = $S3syncOptions['--cache-control'] if $S3syncOptions['--cache-control']
			fType = @path.split('.').last
			debug("File extension: #{fType}")
			if defined?($mimeTypes) and fType != '' and (mType = $mimeTypes[fType]) and mType != ''
				debug("Mime type: #{mType}")
				headers['Content-Type'] = mType
			end
			@result = S3sync.S3try(:put, @bucket, @path, s3o, headers)
			theStream.close if (theStream and not theStream.closed?)
		rescue NoMethodError
			# when --progress is used and we can't get the stream object, it doesn't report as null
			# so the above .closed? test will break
			$stderr.puts "Skipping #{@path}: " + $!
		rescue SystemCallError
			theStream.close if (theStream and not theStream.closed?)
			$stderr.puts "Skipping #{@path}: " + $!
		end
	else
		raise "Node provided as update source doesn't support :stream"
	end
end