【问题标题】:How to upload folder with subfolder to amazon s3?如何将带有子文件夹的文件夹上传到亚马逊 s3?
【发布时间】:2013-02-11 23:33:57
【问题描述】:

我需要在 amazon s3 上上传带有子文件夹的文件夹。我尝试使用此片段上传。

    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/eota7tas0cdlg2ufq5mlke7olf") 创建完整路径文件。如何使用路径上传它("/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;
    }

【问题讨论】:

    标签: java file-upload amazon-s3 amazon


    【解决方案1】:

    您是否查看过适用于 Java 的 AWS 开发工具包中的 TransferManager?您可以为此使用uploadDirectory 方法。 javadoc 是here。本质上,你可以这样做:

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

    【讨论】:

    • 获取 TransferManger 的建议方法是使用 TransferManagerBuilder.defaultTransferManager()
    【解决方案2】:

    我自己写的。

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

    【讨论】:

    • 我想上传单个文件夹中存在的所有文件,例如假设我有文件夹名称“TestMech”,其中包含我需要在进度指示器的帮助下上传的所有照片的数量可能以及如何
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 2022-07-02
    • 2013-10-10
    • 2016-11-14
    • 1970-01-01
    相关资源
    最近更新 更多