java 如何上传带有子文件夹的文件夹到amazon s3?

3lxsmp7m  于 2023-02-18  发布在  Java
关注(0)|答案(2)|浏览(159)

我需要上传文件夹与子文件夹在亚马逊s3。我尝试上传与此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()));
       }
    }

但是这段代码创建了完整路径文件**(“/home/rmuhamedgaliev/tmp/eota 7 tas 0 cdlg 2ufq 5 mlke 7 olf”)。如何上传路径为(“/plans/eota 7 tas 0 cdlg 2ufq 5 mlke 7 olf/{子文件夹和文件}”)**

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;
    }
hlswsv35

hlswsv351#

你看过AWS SDK for Java中的TransferManager了吗?你可以使用uploadDirectory方法来实现这一点。javadoc就在这里。本质上,你可以这样做:

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

b1uwtaje2#

我用自己的方式写作。

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

相关问题