简体   繁体   中英

How to upload folder with subfolder to amazon s3?

I need upload folder with subfolders on amazon s3. I try upload with this snipet.

    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 create full path files with ("/home/rmuhamedgaliev/tmp/eota7tas0cdlg2ufq5mlke7olf") . How to upload it with 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;
    }

Have you looked at the TransferManager in the AWS SDK for Java? You could use the uploadDirectory method for this. The javadoc is here . In essence, you could do something like this:

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

I writen 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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM