001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.fs.s3native; 020 021import java.io.BufferedOutputStream; 022import java.io.EOFException; 023import java.io.File; 024import java.io.FileNotFoundException; 025import java.io.FileOutputStream; 026import java.io.IOException; 027import java.io.InputStream; 028import java.io.OutputStream; 029import java.net.URI; 030import java.security.DigestOutputStream; 031import java.security.MessageDigest; 032import java.security.NoSuchAlgorithmException; 033import java.util.ArrayList; 034import java.util.HashMap; 035import java.util.List; 036import java.util.Map; 037import java.util.Set; 038import java.util.TreeSet; 039import java.util.concurrent.TimeUnit; 040 041import com.google.common.base.Preconditions; 042import org.apache.hadoop.classification.InterfaceAudience; 043import org.apache.hadoop.classification.InterfaceStability; 044import org.apache.hadoop.conf.Configuration; 045import org.apache.hadoop.fs.BufferedFSInputStream; 046import org.apache.hadoop.fs.FSDataInputStream; 047import org.apache.hadoop.fs.FSDataOutputStream; 048import org.apache.hadoop.fs.FSExceptionMessages; 049import org.apache.hadoop.fs.FSInputStream; 050import org.apache.hadoop.fs.FileAlreadyExistsException; 051import org.apache.hadoop.fs.FileStatus; 052import org.apache.hadoop.fs.FileSystem; 053import org.apache.hadoop.fs.LocalDirAllocator; 054import org.apache.hadoop.fs.Path; 055import org.apache.hadoop.fs.permission.FsPermission; 056import org.apache.hadoop.fs.s3.S3Exception; 057import org.apache.hadoop.io.retry.RetryPolicies; 058import org.apache.hadoop.io.retry.RetryPolicy; 059import org.apache.hadoop.io.retry.RetryProxy; 060import org.apache.hadoop.util.Progressable; 061import org.slf4j.Logger; 062import org.slf4j.LoggerFactory; 063 064/** 065 * A {@link FileSystem} for reading and writing files stored on 066 * <a href="http://aws.amazon.com/s3">Amazon S3</a>. 067 * Unlike {@link org.apache.hadoop.fs.s3.S3FileSystem} this implementation 068 * stores files on S3 in their 069 * native form so they can be read by other S3 tools. 070 * <p> 071 * A note about directories. S3 of course has no "native" support for them. 072 * The idiom we choose then is: for any directory created by this class, 073 * we use an empty object "#{dirpath}_$folder$" as a marker. 074 * Further, to interoperate with other S3 tools, we also accept the following: 075 * <ul> 076 * <li>an object "#{dirpath}/' denoting a directory marker</li> 077 * <li> 078 * if there exists any objects with the prefix "#{dirpath}/", then the 079 * directory is said to exist 080 * </li> 081 * <li> 082 * if both a file with the name of a directory and a marker for that 083 * directory exists, then the *file masks the directory*, and the directory 084 * is never returned. 085 * </li> 086 * </ul> 087 * 088 * @see org.apache.hadoop.fs.s3.S3FileSystem 089 */ 090@InterfaceAudience.Public 091@InterfaceStability.Stable 092public class NativeS3FileSystem extends FileSystem { 093 094 public static final Logger LOG = 095 LoggerFactory.getLogger(NativeS3FileSystem.class); 096 097 private static final String FOLDER_SUFFIX = "_$folder$"; 098 static final String PATH_DELIMITER = Path.SEPARATOR; 099 private static final int S3_MAX_LISTING_LENGTH = 1000; 100 101 static class NativeS3FsInputStream extends FSInputStream { 102 103 private NativeFileSystemStore store; 104 private Statistics statistics; 105 private InputStream in; 106 private final String key; 107 private long pos = 0; 108 109 public NativeS3FsInputStream(NativeFileSystemStore store, Statistics statistics, InputStream in, String key) { 110 Preconditions.checkNotNull(in, "Null input stream"); 111 this.store = store; 112 this.statistics = statistics; 113 this.in = in; 114 this.key = key; 115 } 116 117 @Override 118 public synchronized int read() throws IOException { 119 int result; 120 try { 121 result = in.read(); 122 } catch (IOException e) { 123 LOG.info("Received IOException while reading '{}', attempting to reopen", 124 key); 125 LOG.debug("{}", e, e); 126 try { 127 seek(pos); 128 result = in.read(); 129 } catch (EOFException eof) { 130 LOG.debug("EOF on input stream read: {}", eof, eof); 131 result = -1; 132 } 133 } 134 if (result != -1) { 135 pos++; 136 } 137 if (statistics != null && result != -1) { 138 statistics.incrementBytesRead(1); 139 } 140 return result; 141 } 142 @Override 143 public synchronized int read(byte[] b, int off, int len) 144 throws IOException { 145 if (in == null) { 146 throw new EOFException("Cannot read closed stream"); 147 } 148 int result = -1; 149 try { 150 result = in.read(b, off, len); 151 } catch (EOFException eof) { 152 throw eof; 153 } catch (IOException e) { 154 LOG.info( "Received IOException while reading '{}'," + 155 " attempting to reopen.", key); 156 seek(pos); 157 result = in.read(b, off, len); 158 } 159 if (result > 0) { 160 pos += result; 161 } 162 if (statistics != null && result > 0) { 163 statistics.incrementBytesRead(result); 164 } 165 return result; 166 } 167 168 @Override 169 public synchronized void close() throws IOException { 170 closeInnerStream(); 171 } 172 173 /** 174 * Close the inner stream if not null. Even if an exception 175 * is raised during the close, the field is set to null 176 * @throws IOException if raised by the close() operation. 177 */ 178 private void closeInnerStream() throws IOException { 179 if (in != null) { 180 try { 181 in.close(); 182 } finally { 183 in = null; 184 } 185 } 186 } 187 188 /** 189 * Update inner stream with a new stream and position 190 * @param newStream new stream -must not be null 191 * @param newpos new position 192 * @throws IOException IO exception on a failure to close the existing 193 * stream. 194 */ 195 private synchronized void updateInnerStream(InputStream newStream, long newpos) throws IOException { 196 Preconditions.checkNotNull(newStream, "Null newstream argument"); 197 closeInnerStream(); 198 in = newStream; 199 this.pos = newpos; 200 } 201 202 @Override 203 public synchronized void seek(long newpos) throws IOException { 204 if (newpos < 0) { 205 throw new EOFException( 206 FSExceptionMessages.NEGATIVE_SEEK); 207 } 208 if (pos != newpos) { 209 // the seek is attempting to move the current position 210 LOG.debug("Opening key '{}' for reading at position '{}", key, newpos); 211 InputStream newStream = store.retrieve(key, newpos); 212 updateInnerStream(newStream, newpos); 213 } 214 } 215 216 @Override 217 public synchronized long getPos() throws IOException { 218 return pos; 219 } 220 @Override 221 public boolean seekToNewSource(long targetPos) throws IOException { 222 return false; 223 } 224 } 225 226 private class NativeS3FsOutputStream extends OutputStream { 227 228 private Configuration conf; 229 private String key; 230 private File backupFile; 231 private OutputStream backupStream; 232 private MessageDigest digest; 233 private boolean closed; 234 private LocalDirAllocator lDirAlloc; 235 236 public NativeS3FsOutputStream(Configuration conf, 237 NativeFileSystemStore store, String key, Progressable progress, 238 int bufferSize) throws IOException { 239 this.conf = conf; 240 this.key = key; 241 this.backupFile = newBackupFile(); 242 LOG.info("OutputStream for key '" + key + "' writing to tempfile '" + this.backupFile + "'"); 243 try { 244 this.digest = MessageDigest.getInstance("MD5"); 245 this.backupStream = new BufferedOutputStream(new DigestOutputStream( 246 new FileOutputStream(backupFile), this.digest)); 247 } catch (NoSuchAlgorithmException e) { 248 LOG.warn("Cannot load MD5 digest algorithm," + 249 "skipping message integrity check.", e); 250 this.backupStream = new BufferedOutputStream( 251 new FileOutputStream(backupFile)); 252 } 253 } 254 255 private File newBackupFile() throws IOException { 256 if (lDirAlloc == null) { 257 lDirAlloc = new LocalDirAllocator("fs.s3.buffer.dir"); 258 } 259 File result = lDirAlloc.createTmpFileForWrite("output-", LocalDirAllocator.SIZE_UNKNOWN, conf); 260 result.deleteOnExit(); 261 return result; 262 } 263 264 @Override 265 public void flush() throws IOException { 266 backupStream.flush(); 267 } 268 269 @Override 270 public synchronized void close() throws IOException { 271 if (closed) { 272 return; 273 } 274 275 backupStream.close(); 276 LOG.info("OutputStream for key '{}' closed. Now beginning upload", key); 277 278 try { 279 byte[] md5Hash = digest == null ? null : digest.digest(); 280 store.storeFile(key, backupFile, md5Hash); 281 } finally { 282 if (!backupFile.delete()) { 283 LOG.warn("Could not delete temporary s3n file: " + backupFile); 284 } 285 super.close(); 286 closed = true; 287 } 288 LOG.info("OutputStream for key '{}' upload complete", key); 289 } 290 291 @Override 292 public void write(int b) throws IOException { 293 backupStream.write(b); 294 } 295 296 @Override 297 public void write(byte[] b, int off, int len) throws IOException { 298 backupStream.write(b, off, len); 299 } 300 } 301 302 private URI uri; 303 private NativeFileSystemStore store; 304 private Path workingDir; 305 306 public NativeS3FileSystem() { 307 // set store in initialize() 308 } 309 310 public NativeS3FileSystem(NativeFileSystemStore store) { 311 this.store = store; 312 } 313 314 /** 315 * Return the protocol scheme for the FileSystem. 316 * 317 * @return <code>s3n</code> 318 */ 319 @Override 320 public String getScheme() { 321 return "s3n"; 322 } 323 324 @Override 325 public void initialize(URI uri, Configuration conf) throws IOException { 326 super.initialize(uri, conf); 327 if (store == null) { 328 store = createDefaultStore(conf); 329 } 330 store.initialize(uri, conf); 331 setConf(conf); 332 this.uri = URI.create(uri.getScheme() + "://" + uri.getAuthority()); 333 this.workingDir = 334 new Path("/user", System.getProperty("user.name")).makeQualified(this.uri, this.getWorkingDirectory()); 335 } 336 337 private static NativeFileSystemStore createDefaultStore(Configuration conf) { 338 NativeFileSystemStore store = new Jets3tNativeFileSystemStore(); 339 340 RetryPolicy basePolicy = RetryPolicies.retryUpToMaximumCountWithFixedSleep( 341 conf.getInt("fs.s3.maxRetries", 4), 342 conf.getLong("fs.s3.sleepTimeSeconds", 10), TimeUnit.SECONDS); 343 Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap = 344 new HashMap<Class<? extends Exception>, RetryPolicy>(); 345 exceptionToPolicyMap.put(IOException.class, basePolicy); 346 exceptionToPolicyMap.put(S3Exception.class, basePolicy); 347 348 RetryPolicy methodPolicy = RetryPolicies.retryByException( 349 RetryPolicies.TRY_ONCE_THEN_FAIL, exceptionToPolicyMap); 350 Map<String, RetryPolicy> methodNameToPolicyMap = 351 new HashMap<String, RetryPolicy>(); 352 methodNameToPolicyMap.put("storeFile", methodPolicy); 353 methodNameToPolicyMap.put("rename", methodPolicy); 354 355 return (NativeFileSystemStore) 356 RetryProxy.create(NativeFileSystemStore.class, store, 357 methodNameToPolicyMap); 358 } 359 360 private static String pathToKey(Path path) { 361 if (path.toUri().getScheme() != null && path.toUri().getPath().isEmpty()) { 362 // allow uris without trailing slash after bucket to refer to root, 363 // like s3n://mybucket 364 return ""; 365 } 366 if (!path.isAbsolute()) { 367 throw new IllegalArgumentException("Path must be absolute: " + path); 368 } 369 String ret = path.toUri().getPath().substring(1); // remove initial slash 370 if (ret.endsWith("/") && (ret.indexOf("/") != ret.length() - 1)) { 371 ret = ret.substring(0, ret.length() -1); 372 } 373 return ret; 374 } 375 376 private static Path keyToPath(String key) { 377 return new Path("/" + key); 378 } 379 380 private Path makeAbsolute(Path path) { 381 if (path.isAbsolute()) { 382 return path; 383 } 384 return new Path(workingDir, path); 385 } 386 387 /** This optional operation is not yet supported. */ 388 @Override 389 public FSDataOutputStream append(Path f, int bufferSize, 390 Progressable progress) throws IOException { 391 throw new IOException("Not supported"); 392 } 393 394 @Override 395 public FSDataOutputStream create(Path f, FsPermission permission, 396 boolean overwrite, int bufferSize, short replication, long blockSize, 397 Progressable progress) throws IOException { 398 399 if (exists(f) && !overwrite) { 400 throw new FileAlreadyExistsException("File already exists: " + f); 401 } 402 403 if(LOG.isDebugEnabled()) { 404 LOG.debug("Creating new file '" + f + "' in S3"); 405 } 406 Path absolutePath = makeAbsolute(f); 407 String key = pathToKey(absolutePath); 408 return new FSDataOutputStream(new NativeS3FsOutputStream(getConf(), store, 409 key, progress, bufferSize), statistics); 410 } 411 412 @Override 413 public boolean delete(Path f, boolean recurse) throws IOException { 414 FileStatus status; 415 try { 416 status = getFileStatus(f); 417 } catch (FileNotFoundException e) { 418 if(LOG.isDebugEnabled()) { 419 LOG.debug("Delete called for '" + f + 420 "' but file does not exist, so returning false"); 421 } 422 return false; 423 } 424 Path absolutePath = makeAbsolute(f); 425 String key = pathToKey(absolutePath); 426 if (status.isDirectory()) { 427 if (!recurse && listStatus(f).length > 0) { 428 throw new IOException("Can not delete " + f + " as is a not empty directory and recurse option is false"); 429 } 430 431 createParent(f); 432 433 if(LOG.isDebugEnabled()) { 434 LOG.debug("Deleting directory '" + f + "'"); 435 } 436 String priorLastKey = null; 437 do { 438 PartialListing listing = store.list(key, S3_MAX_LISTING_LENGTH, priorLastKey, true); 439 for (FileMetadata file : listing.getFiles()) { 440 store.delete(file.getKey()); 441 } 442 priorLastKey = listing.getPriorLastKey(); 443 } while (priorLastKey != null); 444 445 try { 446 store.delete(key + FOLDER_SUFFIX); 447 } catch (FileNotFoundException e) { 448 //this is fine, we don't require a marker 449 } 450 } else { 451 if(LOG.isDebugEnabled()) { 452 LOG.debug("Deleting file '" + f + "'"); 453 } 454 createParent(f); 455 store.delete(key); 456 } 457 return true; 458 } 459 460 @Override 461 public FileStatus getFileStatus(Path f) throws IOException { 462 Path absolutePath = makeAbsolute(f); 463 String key = pathToKey(absolutePath); 464 465 if (key.length() == 0) { // root always exists 466 return newDirectory(absolutePath); 467 } 468 469 if(LOG.isDebugEnabled()) { 470 LOG.debug("getFileStatus retrieving metadata for key '" + key + "'"); 471 } 472 FileMetadata meta = store.retrieveMetadata(key); 473 if (meta != null) { 474 if(LOG.isDebugEnabled()) { 475 LOG.debug("getFileStatus returning 'file' for key '" + key + "'"); 476 } 477 return newFile(meta, absolutePath); 478 } 479 if (store.retrieveMetadata(key + FOLDER_SUFFIX) != null) { 480 if(LOG.isDebugEnabled()) { 481 LOG.debug("getFileStatus returning 'directory' for key '" + key + 482 "' as '" + key + FOLDER_SUFFIX + "' exists"); 483 } 484 return newDirectory(absolutePath); 485 } 486 487 if(LOG.isDebugEnabled()) { 488 LOG.debug("getFileStatus listing key '" + key + "'"); 489 } 490 PartialListing listing = store.list(key, 1); 491 if (listing.getFiles().length > 0 || 492 listing.getCommonPrefixes().length > 0) { 493 if(LOG.isDebugEnabled()) { 494 LOG.debug("getFileStatus returning 'directory' for key '" + key + 495 "' as it has contents"); 496 } 497 return newDirectory(absolutePath); 498 } 499 500 if(LOG.isDebugEnabled()) { 501 LOG.debug("getFileStatus could not find key '" + key + "'"); 502 } 503 throw new FileNotFoundException("No such file or directory '" + absolutePath + "'"); 504 } 505 506 @Override 507 public URI getUri() { 508 return uri; 509 } 510 511 /** 512 * <p> 513 * If <code>f</code> is a file, this method will make a single call to S3. 514 * If <code>f</code> is a directory, this method will make a maximum of 515 * (<i>n</i> / 1000) + 2 calls to S3, where <i>n</i> is the total number of 516 * files and directories contained directly in <code>f</code>. 517 * </p> 518 */ 519 @Override 520 public FileStatus[] listStatus(Path f) throws IOException { 521 522 Path absolutePath = makeAbsolute(f); 523 String key = pathToKey(absolutePath); 524 525 if (key.length() > 0) { 526 FileMetadata meta = store.retrieveMetadata(key); 527 if (meta != null) { 528 return new FileStatus[] { newFile(meta, absolutePath) }; 529 } 530 } 531 532 URI pathUri = absolutePath.toUri(); 533 Set<FileStatus> status = new TreeSet<FileStatus>(); 534 String priorLastKey = null; 535 do { 536 PartialListing listing = store.list(key, S3_MAX_LISTING_LENGTH, priorLastKey, false); 537 for (FileMetadata fileMetadata : listing.getFiles()) { 538 Path subpath = keyToPath(fileMetadata.getKey()); 539 String relativePath = pathUri.relativize(subpath.toUri()).getPath(); 540 541 if (fileMetadata.getKey().equals(key + "/")) { 542 // this is just the directory we have been asked to list 543 } 544 else if (relativePath.endsWith(FOLDER_SUFFIX)) { 545 status.add(newDirectory(new Path( 546 absolutePath, 547 relativePath.substring(0, relativePath.indexOf(FOLDER_SUFFIX))))); 548 } 549 else { 550 status.add(newFile(fileMetadata, subpath)); 551 } 552 } 553 for (String commonPrefix : listing.getCommonPrefixes()) { 554 Path subpath = keyToPath(commonPrefix); 555 String relativePath = pathUri.relativize(subpath.toUri()).getPath(); 556 status.add(newDirectory(new Path(absolutePath, relativePath))); 557 } 558 priorLastKey = listing.getPriorLastKey(); 559 } while (priorLastKey != null); 560 561 if (status.isEmpty() && 562 key.length() > 0 && 563 store.retrieveMetadata(key + FOLDER_SUFFIX) == null) { 564 throw new FileNotFoundException("File " + f + " does not exist."); 565 } 566 567 return status.toArray(new FileStatus[status.size()]); 568 } 569 570 private FileStatus newFile(FileMetadata meta, Path path) { 571 return new FileStatus(meta.getLength(), false, 1, getDefaultBlockSize(), 572 meta.getLastModified(), path.makeQualified(this.getUri(), this.getWorkingDirectory())); 573 } 574 575 private FileStatus newDirectory(Path path) { 576 return new FileStatus(0, true, 1, 0, 0, path.makeQualified(this.getUri(), this.getWorkingDirectory())); 577 } 578 579 @Override 580 public boolean mkdirs(Path f, FsPermission permission) throws IOException { 581 Path absolutePath = makeAbsolute(f); 582 List<Path> paths = new ArrayList<Path>(); 583 do { 584 paths.add(0, absolutePath); 585 absolutePath = absolutePath.getParent(); 586 } while (absolutePath != null); 587 588 boolean result = true; 589 for (Path path : paths) { 590 result &= mkdir(path); 591 } 592 return result; 593 } 594 595 private boolean mkdir(Path f) throws IOException { 596 try { 597 FileStatus fileStatus = getFileStatus(f); 598 if (fileStatus.isFile()) { 599 throw new FileAlreadyExistsException(String.format( 600 "Can't make directory for path '%s' since it is a file.", f)); 601 602 } 603 } catch (FileNotFoundException e) { 604 if(LOG.isDebugEnabled()) { 605 LOG.debug("Making dir '" + f + "' in S3"); 606 } 607 String key = pathToKey(f) + FOLDER_SUFFIX; 608 store.storeEmptyFile(key); 609 } 610 return true; 611 } 612 613 @Override 614 public FSDataInputStream open(Path f, int bufferSize) throws IOException { 615 FileStatus fs = getFileStatus(f); // will throw if the file doesn't exist 616 if (fs.isDirectory()) { 617 throw new FileNotFoundException("'" + f + "' is a directory"); 618 } 619 LOG.info("Opening '" + f + "' for reading"); 620 Path absolutePath = makeAbsolute(f); 621 String key = pathToKey(absolutePath); 622 return new FSDataInputStream(new BufferedFSInputStream( 623 new NativeS3FsInputStream(store, statistics, store.retrieve(key), key), bufferSize)); 624 } 625 626 // rename() and delete() use this method to ensure that the parent directory 627 // of the source does not vanish. 628 private void createParent(Path path) throws IOException { 629 Path parent = path.getParent(); 630 if (parent != null) { 631 String key = pathToKey(makeAbsolute(parent)); 632 if (key.length() > 0) { 633 store.storeEmptyFile(key + FOLDER_SUFFIX); 634 } 635 } 636 } 637 638 639 @Override 640 public boolean rename(Path src, Path dst) throws IOException { 641 642 String srcKey = pathToKey(makeAbsolute(src)); 643 644 if (srcKey.length() == 0) { 645 // Cannot rename root of file system 646 return false; 647 } 648 649 final String debugPreamble = "Renaming '" + src + "' to '" + dst + "' - "; 650 651 // Figure out the final destination 652 String dstKey; 653 try { 654 boolean dstIsFile = getFileStatus(dst).isFile(); 655 if (dstIsFile) { 656 if(LOG.isDebugEnabled()) { 657 LOG.debug(debugPreamble + 658 "returning false as dst is an already existing file"); 659 } 660 return false; 661 } else { 662 if(LOG.isDebugEnabled()) { 663 LOG.debug(debugPreamble + "using dst as output directory"); 664 } 665 dstKey = pathToKey(makeAbsolute(new Path(dst, src.getName()))); 666 } 667 } catch (FileNotFoundException e) { 668 if(LOG.isDebugEnabled()) { 669 LOG.debug(debugPreamble + "using dst as output destination"); 670 } 671 dstKey = pathToKey(makeAbsolute(dst)); 672 try { 673 if (getFileStatus(dst.getParent()).isFile()) { 674 if(LOG.isDebugEnabled()) { 675 LOG.debug(debugPreamble + 676 "returning false as dst parent exists and is a file"); 677 } 678 return false; 679 } 680 } catch (FileNotFoundException ex) { 681 if(LOG.isDebugEnabled()) { 682 LOG.debug(debugPreamble + 683 "returning false as dst parent does not exist"); 684 } 685 return false; 686 } 687 } 688 689 boolean srcIsFile; 690 try { 691 srcIsFile = getFileStatus(src).isFile(); 692 } catch (FileNotFoundException e) { 693 if(LOG.isDebugEnabled()) { 694 LOG.debug(debugPreamble + "returning false as src does not exist"); 695 } 696 return false; 697 } 698 if (srcIsFile) { 699 if(LOG.isDebugEnabled()) { 700 LOG.debug(debugPreamble + 701 "src is file, so doing copy then delete in S3"); 702 } 703 store.copy(srcKey, dstKey); 704 store.delete(srcKey); 705 } else { 706 if(LOG.isDebugEnabled()) { 707 LOG.debug(debugPreamble + "src is directory, so copying contents"); 708 } 709 store.storeEmptyFile(dstKey + FOLDER_SUFFIX); 710 711 List<String> keysToDelete = new ArrayList<String>(); 712 String priorLastKey = null; 713 do { 714 PartialListing listing = store.list(srcKey, S3_MAX_LISTING_LENGTH, priorLastKey, true); 715 for (FileMetadata file : listing.getFiles()) { 716 keysToDelete.add(file.getKey()); 717 store.copy(file.getKey(), dstKey + file.getKey().substring(srcKey.length())); 718 } 719 priorLastKey = listing.getPriorLastKey(); 720 } while (priorLastKey != null); 721 722 if(LOG.isDebugEnabled()) { 723 LOG.debug(debugPreamble + 724 "all files in src copied, now removing src files"); 725 } 726 for (String key: keysToDelete) { 727 store.delete(key); 728 } 729 730 try { 731 store.delete(srcKey + FOLDER_SUFFIX); 732 } catch (FileNotFoundException e) { 733 //this is fine, we don't require a marker 734 } 735 if(LOG.isDebugEnabled()) { 736 LOG.debug(debugPreamble + "done"); 737 } 738 } 739 740 return true; 741 } 742 743 @Override 744 public long getDefaultBlockSize() { 745 return getConf().getLong("fs.s3n.block.size", 64 * 1024 * 1024); 746 } 747 748 /** 749 * Set the working directory to the given directory. 750 */ 751 @Override 752 public void setWorkingDirectory(Path newDir) { 753 workingDir = newDir; 754 } 755 756 @Override 757 public Path getWorkingDirectory() { 758 return workingDir; 759 } 760 761 @Override 762 public String getCanonicalServiceName() { 763 // Does not support Token 764 return null; 765 } 766}