Skip to content

Commit

Permalink
Fix bugs in TM tests on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
tkhill-AWS committed Feb 20, 2024
1 parent 2af2e03 commit d90824c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> downloadedFileNameSet = new HashSet<>();
private final String destinationDirName = "downloadDirectory";

Expand All @@ -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();
Expand All @@ -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() {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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() {
Expand Down

0 comments on commit d90824c

Please sign in to comment.