How to upload a folder with a subfolder on amazon s3?

I need to upload a folder with subfolders on amazon s3. I am trying to download this sniper.

    for (Path path : directoryWalk("/home/rmuhamedgaliev/tmp/eota7tas0cdlg2ufq5mlke7olf/")){
       if (!path.getParent().toString().equals("eota7tas0cdlg2ufq5mlke7olf")){
        amazonS3Client.putObject("*****", "/plans/eota7tas0cdlg2ufq5mlke7olf/" + path.getParent().toString() + "/" + path.getFileName(), new File(path.toString()));
       } else {
        amazonS3Client.putObject("*******", "/plans/eota7tas0cdlg2ufq5mlke7olf/" + path.getFileName(), new File(path.toString()));
       }
    }

But this code creates full path files with ("/ home / rmuhamedgaliev / tmp / eota7tas0cdlg2ufq5mlke7olf") . How to download it using the path ("/ plans / eota7tas0cdlg2ufq5mlke7olf / {subfolders and files}")

    private List<Path> directoryWalk(String path) throws IOException {
        final List<Path> files = new ArrayList<>();
        Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                files.add(file);
                return FileVisitResult.CONTINUE;
            }
        });
        return files;
    }
+5
source share
2 answers

Have you looked TransferManagerin the AWS SDK for Java? You can use the method for this uploadDirectory. Here is the javadoc. In essence, you could do something like this:

transferManager.uploadDirectory(bucketName, "plans/eota7tas0cdlg2ufq5mlke7olf/", new File("/home/rmuhamedgaliev/tmp/eota7tas0cdlg2ufq5mlke7olf/"), true);
+12
source

I record my own way.

        List<File> files = new LinkedList<File>();
        listFiles(new File("/home/rmuhamedgaliev/tmp/eota7tas0cdlg2ufq5mlke7olf"), files, true);
        for (File f : files) {
            String key = f.getAbsolutePath().substring(new File("/home/rmuhamedgaliev/tmp/eota7tas0cdlg2ufq5mlke7olf").getAbsolutePath().length() + 1)
                .replaceAll("\\\\", "/");
            amazonS3Client.putObject("****", "plans/eota7tas0cdlg2ufq5mlke7olf/" + key, f);
        }
+1
source

All Articles