Top Level Namespace
Defined Under Namespace
Modules: AWS, S3CP Classes: ProxyIO
Constant Summary collapse
- DEBUG =
true
Instance Method Summary collapse
- #copy(from, to, options) ⇒ Object
- #debug(str) ⇒ Object
- #depth(path) ⇒ Object
- #direction(from, to) ⇒ Object
- #key_path(prefix, key) ⇒ Object
- #local_to_s3(bucket_to, key, file, options = {}) ⇒ Object
- #log(msg) ⇒ Object
- #match(path) ⇒ Object
- #mkdir_relative(regex, key) ⇒ Object
- #no_slash(path) ⇒ Object
-
#nth_occurrence(str, substr, n) ⇒ Object
Returns the index of the nth occurrence of substr if it exists, otherwise -1.
- #operation(options) ⇒ Object
- #print(key, size) ⇒ Object
- #print_buckets(buckets) ⇒ Object
- #print_keys(bucket, keys) ⇒ Object
-
#relative(base, path) ⇒ Object
relative(“path/to/”, “path/to/file”) => “file” relative(“path/to”, “path/to/file”) => “to/file”.
- #rule_to_str(r) ⇒ Object
- #s3?(url) ⇒ Boolean
- #s3_checksum(bucket, key) ⇒ Object
- #s3_exist?(bucket, key) ⇒ Boolean
- #s3_size(bucket, key) ⇒ Object
- #s3_to_local(bucket_from, key_from, dest, options = {}) ⇒ Object
- #s3_to_s3(bucket_from, key, bucket_to, dest, options = {}) ⇒ Object
- #time_or_date_str(msg, t) ⇒ Object
- #with_headers(msg) ⇒ Object
Instance Method Details
#copy(from, to, options) ⇒ Object
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 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
# File 'lib/s3cp/s3cp.rb', line 488 def copy(from, to, ) bucket_from, key_from = S3CP.bucket_and_key(from) bucket_to, key_to = S3CP.bucket_and_key(to) case direction(from, to) when :s3_to_s3 if [:recursive] keys = [] @s3.buckets[bucket_from].objects.with_prefix(key_from).each do |entry| keys << entry.key end keys.each do |key| if match(key) dest = key_path key_to, relative(key_from, key) if ![:overwrite] && s3_exist?(bucket_to, dest) $stderr.puts "Skipping s3://#{bucket_to}/#{dest} - already exists." else s3_to_s3(bucket_from, key, bucket_to, dest, ) end end end else key_to += File.basename(key_from) if key_to[-1..-1] == "/" if ![:overwrite] && s3_exist?(bucket_to, key_to) $stderr.puts "Skipping s3://#{bucket_to}/#{key_to} - already exists." else s3_to_s3(bucket_from, key_from, bucket_to, key_to, ) end end when :local_to_s3 if [:recursive] files = Dir[from + "/**/*"] files.each do |f| if File.file?(f) && match(f) #puts "bucket_to #{bucket_to}" #puts "no_slash(key_to) #{no_slash(key_to)}" #puts "relative(from, f) #{relative(from, f)}" key = key_path key_to, relative(from, f) if ![:overwrite] && s3_exist?(bucket_to, key) $stderr.puts "Skipping s3://#{bucket_to}/#{key} - already exists." else local_to_s3(bucket_to, key, File.(f), ) end end end else key_to += File.basename(from) if key_to[-1..-1] == "/" if ![:overwrite] && s3_exist?(bucket_to, key_to) $stderr.puts "Skipping s3://#{bucket_to}/#{key_to} - already exists." else local_to_s3(bucket_to, key_to, File.(from), ) end end when :s3_to_local if [:recursive] keys = [] @s3.buckets[bucket_from].objects.with_prefix(key_from).each do |entry| keys << entry.key end keys.each do |key| if match(key) dest = if [:mkdir] suffix = mkdir_relative([:mkdir], key) or next dest = File.join(File.(to), suffix) else dest = File.(to) + '/' + relative(key_from, key) dest = File.join(dest, File.basename(key)) if File.directory?(dest) dest end dir = File.dirname(dest) FileUtils.mkdir_p dir unless File.exist? dir fail "Destination path is not a directory: #{dir}" unless File.directory?(dir) if ![:overwrite] && File.exist?(dest) $stderr.puts "Skipping #{dest} - already exists." else s3_to_local(bucket_from, key, dest, ) end end end else dest = if [:mkdir] suffix = mkdir_relative([:mkdir], key_from) or fail("Error: The key '#{key_from}' does not match the --mkdir regular expression '#{[:mkdir]}'") dest = File.join(File.(to), suffix) dir = File.dirname(dest) FileUtils.mkdir_p dir unless File.exist? dir dest else dest = File.(to) dest = File.join(dest, File.basename(from)) if File.directory?(dest) dest end if ![:overwrite] && File.exist?(dest) $stderr.puts "Skipping #{dest} - already exists." else s3_to_local(bucket_from, key_from, dest, ) end end when :local_to_local if [:include_regex].any? || [:exclude_regex].any? fail "Include/exclude not supported on local-to-local copies" end if [:recursive] FileUtils.cp_r from, to FileUtils.rm_r from if [:move] else FileUtils.cp from, to FileUtils.rm from if [:move] end end end |
#debug(str) ⇒ Object
44 45 46 |
# File 'lib/s3cp/completion.rb', line 44 def debug(str) @debug.puts(str) if @debug end |
#depth(path) ⇒ Object
71 72 73 |
# File 'lib/s3cp/s3du.rb', line 71 def depth(path) path.count("/") end |
#direction(from, to) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/s3cp/s3cp.rb', line 211 def direction(from, to) if s3?(from) && s3?(to) :s3_to_s3 elsif !s3?(from) && s3?(to) :local_to_s3 elsif s3?(from) && !s3?(to) :s3_to_local else :local_to_local end end |
#key_path(prefix, key) ⇒ Object
476 477 478 479 480 481 482 |
# File 'lib/s3cp/s3cp.rb', line 476 def key_path(prefix, key) if (prefix.nil? || prefix.strip == '') key else no_slash(prefix) + '/' + key end end |
#local_to_s3(bucket_to, key, file, options = {}) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/s3cp/s3cp.rb', line 283 def local_to_s3(bucket_to, key, file, = {}) log(with_headers("#{operation()} #{file} to s3://#{bucket_to}/#{key}")) expected_md5 = if [:checksum] || [:sync] S3CP.md5(file) end actual_md5 = if [:sync] md5 = s3_checksum(bucket_to, key) case md5 when :not_found nil when :invalid $stderr.puts "Warning: No MD5 checksum available and ETag not suitable due to multi-part upload; file will be force-copied." nil else md5 end end if actual_md5.nil? || ([:sync] && expected_md5 != actual_md5) retries = 0 begin if retries == [:retries] fail "Unable to upload to s3://#{bucket_to}/#{key} after #{retries} attempts." end if retries > 0 delay = [:retry_delay] * ([:retry_backoff] ** retries) $stderr.puts "Sleeping #{"%0.2f" % delay} seconds. Will retry #{[:retries] - retries} more time(s)." sleep delay end begin obj = @s3.buckets[bucket_to].objects[key] = {} S3CP.(, @headers) [:acl] = [:acl] if [:acl] [:content_length] = File.size(file) multipart_threshold = [:multipart].is_a?(Fixnum) ? [:multipart] : AWS.config.s3_multipart_threshold if (expected_md5 != nil) && (File.size(file) >= multipart_threshold) && [:multipart] = [:metadata] || {} [:md5] = expected_md5 [:metadata] = end if [:multipart] [:multipart_threshold] = multipart_threshold else [:single_request] = true end = if $stdout.isatty ProgressBar.new(File.basename(file), File.size(file)).tap do |p| p.file_transfer_mode end end File.open(file) do |io| obj.write(ProxyIO.new(io, ), ) end .finish if if [:checksum] actual_md5 = s3_checksum(bucket_to, key) if actual_md5.is_a? String if actual_md5 != expected_md5 $stderr.puts "Warning: invalid MD5 checksum. Expected: #{expected_md5} Actual: #{actual_md5}" end else $stderr.puts "Warning: invalid MD5 checksum in metadata: #{actual_md5.inspect}; skipped checksum verification." actual_md5 = nil end end rescue => e actual_md5 = "bad" if .clear $stderr.puts "Error copying #{file} to s3://#{bucket_to}/#{key}" end raise e if ![:checksum] || e.is_a?(AWS::S3::Errors::AccessDenied) $stderr.puts e end retries += 1 end until [:checksum] == false || actual_md5.nil? || expected_md5 == actual_md5 else log "Already synchronized." end FileUtils.rm file if [:move] end |
#log(msg) ⇒ Object
248 249 250 |
# File 'lib/s3cp/s3cp.rb', line 248 def log(msg) puts msg if @verbose end |
#match(path) ⇒ Object
223 224 225 226 227 228 |
# File 'lib/s3cp/s3cp.rb', line 223 def match(path) matching = true return false if @includes.any? && !@includes.any? { |regex| regex.match(path) } return false if @excludes.any? && @excludes.any? { |regex| regex.match(path) } true end |
#mkdir_relative(regex, key) ⇒ Object
242 243 244 245 246 |
# File 'lib/s3cp/s3cp.rb', line 242 def mkdir_relative(regex, key) m = regex.match(key) or return pos = m.captures.empty? ? m.begin(0) : m.begin(1) suffix = key[pos..-1] end |
#no_slash(path) ⇒ Object
230 231 232 233 |
# File 'lib/s3cp/s3cp.rb', line 230 def no_slash(path) path = path.match(/\/$/) ? path[0..-2] : path path.match(/^\//) ? path[1..-1] : path end |
#nth_occurrence(str, substr, n) ⇒ Object
Returns the index of the nth occurrence of substr if it exists, otherwise -1
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/s3cp/s3du.rb', line 76 def nth_occurrence(str, substr, n) pos = -1 if n > 0 && str.include?(substr) i = 0 while i < n do pos = str.index(substr, pos + substr.length) if pos != nil i += 1 end end pos != nil && pos != -1 ? pos + 1 : -1 end |
#operation(options) ⇒ Object
262 263 264 265 266 267 |
# File 'lib/s3cp/s3cp.rb', line 262 def operation() operation = "Copy" operation = "Move" if [:move] operation = "Sync" if [:sync] operation end |
#print(key, size) ⇒ Object
93 94 95 96 |
# File 'lib/s3cp/s3du.rb', line 93 def print(key, size) size = S3CP.format_filesize(size, :unit => @options[:unit], :precision => @options[:precision]) puts ("%#{7 + @options[:precision]}s " % size) + key end |
#print_buckets(buckets) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/s3cp/completion.rb', line 88 def print_buckets(buckets) buckets = buckets.map { |b| @legacy_format ? b + ":" : b + "/" } buckets << buckets[0] + " " if buckets.size == 1 buckets.each do |bucket| if @legacy_format debug bucket puts bucket else debug "//#{bucket}" puts "//#{bucket}" end end end |
#print_keys(bucket, keys) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/s3cp/completion.rb', line 75 def print_keys(bucket, keys) keys << keys[0] + " " if keys.size == 1 && @dirs_only keys.each do |key| if @legacy_format debug key puts key else debug "//#{bucket}/#{key}" puts "//#{bucket}/#{key}" end end end |
#relative(base, path) ⇒ Object
relative(“path/to/”, “path/to/file”) => “file” relative(“path/to”, “path/to/file”) => “to/file”
237 238 239 240 |
# File 'lib/s3cp/s3cp.rb', line 237 def relative(base, path) dir = base.rindex("/") ? base[0..base.rindex("/")] : "" no_slash(path[dir.length..-1]) end |
#rule_to_str(r) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/s3cp/s3lifecycle.rb', line 82 def rule_to_str(r) if r.expiration_time [ r.prefix || "[root]", r.id, r.status, time_or_date_str("Expire", r.expiration_time)] elsif r.glacier_transition_time [ r.prefix || "[root]", r.id, r.status, time_or_date_str("Glacier", r.glacier_transition_time)] else [ r.prefix || "[root]", r.id, r.status, "???"] end end |
#s3?(url) ⇒ Boolean
196 197 198 |
# File 'lib/s3cp/s3cp.rb', line 196 def s3?(url) S3CP.bucket_and_key(url)[0] end |
#s3_checksum(bucket, key) ⇒ Object
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/s3cp/s3cp.rb', line 457 def s3_checksum(bucket, key) begin = @s3.buckets[bucket].objects[key].head() return :not_found unless rescue => e return :not_found if e.is_a?(AWS::S3::Errors::NoSuchKey) raise e end case when [:meta] && [:meta]["md5"] [:meta]["md5"] when [:etag] && [:etag] !~ /-/ [:etag].sub(/^"/, "").sub(/"$/, "") # strip beginning and trailing quotes else :invalid end end |
#s3_exist?(bucket, key) ⇒ Boolean
453 454 455 |
# File 'lib/s3cp/s3cp.rb', line 453 def s3_exist?(bucket, key) @s3.buckets[bucket].objects[key].exists? end |
#s3_size(bucket, key) ⇒ Object
484 485 486 |
# File 'lib/s3cp/s3cp.rb', line 484 def s3_size(bucket, key) @s3.buckets[bucket].objects[key].content_length end |
#s3_to_local(bucket_from, key_from, dest, options = {}) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/s3cp/s3cp.rb', line 376 def s3_to_local(bucket_from, key_from, dest, = {}) log("#{operation()} s3://#{bucket_from}/#{key_from} to #{dest}") raise ArgumentError, "source key may not be blank" if key_from.to_s.empty? retries = 0 begin if retries == [:retries] File.delete(dest) if File.exist?(dest) fail "Unable to download s3://#{bucket_from}/#{key_from} after #{retries} attempts." end if retries > 0 delay = [:retry_delay] * ([:retry_backoff] ** retries) delay = delay.to_i $stderr.puts "Sleeping #{"%0.2f" % delay} seconds. Will retry #{[:retries] - retries} more time(s)." sleep delay end begin expected_md5 = if [:checksum] || [:sync] md5 = s3_checksum(bucket_from, key_from) if [:sync] && !md5.is_a?(String) $stderr.puts "Warning: invalid MD5 checksum in metadata; file will be force-copied." nil elsif !md5.is_a? String $stderr.puts "Warning: invalid MD5 checksum in metadata; skipped checksum verification." nil else md5 end end actual_md5 = if [:sync] && File.exist?(dest) S3CP.md5(dest) end if ![:sync] || expected_md5.nil? || (expected_md5 != actual_md5) f = File.new(dest, "wb") begin = if $stdout.isatty size = s3_size(bucket_from, key_from) ProgressBar.new(File.basename(key_from), size).tap do |p| p.file_transfer_mode end end @s3.buckets[bucket_from].objects[key_from].read_as_stream do |chunk| f.write(chunk) .inc chunk.size if end .finish if rescue => e .halt if raise e ensure f.close() end else log("Already synchronized") return end rescue => e raise e if e.is_a?(AWS::S3::Errors::NoSuchKey) raise e unless [:checksum] $stderr.puts e end if [:checksum] && expected_md5 != nil actual_md5 = S3CP.md5(dest) if actual_md5 != expected_md5 $stderr.puts "Warning: invalid MD5 checksum. Expected: #{expected_md5} Actual: #{actual_md5}" end end retries += 1 end until [:checksum] == false || expected_md5.nil? || S3CP.md5(dest) == expected_md5 @s3.buckets[bucket_from].objects[key_from].delete() if [:move] end |
#s3_to_s3(bucket_from, key, bucket_to, dest, options = {}) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/s3cp/s3cp.rb', line 269 def s3_to_s3(bucket_from, key, bucket_to, dest, = {}) log(with_headers("#{operation()} s3://#{bucket_from}/#{key} to s3://#{bucket_to}/#{dest}")) s3_source = @s3.buckets[bucket_from].objects[key] s3_dest = @s3.buckets[bucket_to].objects[dest] = {} S3CP.(, @headers) [:acl] = [:acl] if [:acl] unless [:move] s3_source.copy_to(s3_dest, ) else s3_source.move_to(s3_dest, ) end end |
#time_or_date_str(msg, t) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/s3cp/s3lifecycle.rb', line 74 def time_or_date_str(msg, t) if t.is_a?(Fixnum) || t.to_s =~ /^\d+$/ "%s after %d days" % [msg, t.to_i] else "%s on %s" % [msg, t] end end |
#with_headers(msg) ⇒ Object
254 255 256 257 258 259 260 |
# File 'lib/s3cp/s3cp.rb', line 254 def with_headers(msg) unless @headers.empty? msg += " with headers:" msg += @headers.collect{|k,v| "'#{k}: #{v}'"}.join(", ") end msg end |