diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java index c13b96db5af..2d4639d58ca 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java @@ -14,8 +14,10 @@ import software.amazon.awssdk.transfer.s3.progress.LoggingTransferListener; import java.io.IOException; +import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.UUID; // snippet-end:[s3.tm.java2.downloadfile.import] @@ -70,7 +72,14 @@ private void setUp() { .bucket(bucketName) .key(key), RequestBody.fromString("Hello World")); URL resource = DownloadFile.class.getClassLoader().getResource("."); - downloadedFileWithPath = resource.getFile() + downloadedFileName; + try { + Path basePath = Paths.get(resource.toURI()); + Path fullPath = basePath.resolve(downloadedFileName); + downloadedFileWithPath = fullPath.toString(); + } catch (URISyntaxException | NullPointerException e) { + logger.error("Exception creating URI [{}]", e.getMessage()); + System.exit(1); + } } public void cleanUp() { diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java index 44cafbf3562..33bcdfcd019 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java @@ -14,7 +14,10 @@ import software.amazon.awssdk.transfer.s3.model.DownloadDirectoryRequest; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashSet; import java.util.Set; @@ -34,7 +37,7 @@ public class DownloadToDirectory { private static final Logger logger = LoggerFactory.getLogger(DownloadToDirectory.class); public final String bucketName = "x-" + UUID.randomUUID(); - public String destinationPath; + public URI destinationPathURI; private final Set downloadedFileNameSet = new HashSet<>(); private final String destinationDirName = "downloadDirectory"; @@ -45,16 +48,16 @@ public DownloadToDirectory() { public static void main(String[] args) { DownloadToDirectory download = new DownloadToDirectory(); Integer numFilesFailedToDownload = download.downloadObjectsToDirectory(S3ClientFactory.transferManager, - download.destinationPath, download.bucketName); + download.destinationPathURI, download.bucketName); logger.info("Number of files that failed to download [{}].", numFilesFailedToDownload); download.cleanUp(); } // snippet-start:[s3.tm.java2.downloadtodirectory.main] public Integer downloadObjectsToDirectory(S3TransferManager transferManager, - String destinationPath, String bucketName) { + URI destinationPathURI, String bucketName) { DirectoryDownload directoryDownload = transferManager.downloadDirectory(DownloadDirectoryRequest.builder() - .destination(Paths.get(destinationPath)) + .destination(Paths.get(destinationPathURI)) .bucket(bucketName) .build()); CompletedDirectoryDownload completedDirectoryDownload = directoryDownload.completionFuture().join(); @@ -77,7 +80,12 @@ private void setUp() { .key(fileName), requestBody); }); - destinationPath = DownloadToDirectory.class.getClassLoader().getResource(destinationDirName).getPath(); + try { + destinationPathURI = DownloadToDirectory.class.getClassLoader().getResource(destinationDirName).toURI(); + } catch (URISyntaxException | NullPointerException e) { + logger.error("Exception creating URI [{}]", e.getMessage()); + System.exit(1); + } } public void cleanUp() { @@ -96,7 +104,9 @@ public void cleanUp() { // Delete files downloaded. downloadedFileNameSet.stream().forEach(fileName -> { try { - Files.delete(Paths.get(destinationPath + "/" + fileName)); + Path basePath = Paths.get(destinationPathURI); + Path fullPath = basePath.resolve(fileName); + Files.delete(fullPath); } catch (IOException e) { logger.error("Exception deleting file [{}]", fileName); throw new RuntimeException(e); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java index b089e37b40a..3a0c4cbf219 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java @@ -12,6 +12,8 @@ import software.amazon.awssdk.transfer.s3.model.DirectoryUpload; import software.amazon.awssdk.transfer.s3.model.UploadDirectoryRequest; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Paths; import java.util.UUID; @@ -29,7 +31,7 @@ public class UploadADirectory { private static final Logger logger = LoggerFactory.getLogger(UploadADirectory.class); public final String bucketName = "x-" + UUID.randomUUID(); - public String sourceDirectory; + public URI sourceDirectory; public UploadADirectory() { setUp(); @@ -46,7 +48,7 @@ public static void main(String[] args) { // snippet-start:[s3.tm.java2.uploadadirectory.main] public Integer uploadDirectory(S3TransferManager transferManager, - String sourceDirectory, String bucketName) { + URI sourceDirectory, String bucketName) { DirectoryUpload directoryUpload = transferManager.uploadDirectory(UploadDirectoryRequest.builder() .source(Paths.get(sourceDirectory)) .bucket(bucketName) @@ -62,7 +64,12 @@ public Integer uploadDirectory(S3TransferManager transferManager, private void setUp() { S3ClientFactory.s3Client.createBucket(b -> b.bucket(bucketName)); URL dirResource = UploadADirectory.class.getClassLoader().getResource("uploadDirectory"); - sourceDirectory = dirResource.getPath(); + try { + sourceDirectory = dirResource.toURI(); + } catch (URISyntaxException | NullPointerException e) { + logger.error("Error getting file path URI: {}", e.getMessage()); + System.exit(1); + } } public void cleanUp() {