简体   繁体   English

如何复制存储桶形式为bucketName / folder1 / folder2的内容

[英]How do I copy contents of a bucket which is of the form bucketName/folder1/folder2

I want to copy folder2 contents and not the whole contents of bucketName. 我想复制folder2的内容,而不是bucketName的全部内容。 I am using the following code but no success. 我正在使用以下代码,但未成功。 I actually see no output. 我实际上看不到输出。 If I just copy the contents of the bucket and not the sub folders, I see the entire contents in the destination bucket. 如果仅复制存储桶中的内容而不复制子文件夹,则可以在目标存储桶中看到全部内容。 How do I just copy the contents of the subfolder2 under the bucket. 如何将子文件夹2的内容复制到存储桶下。

 System.out.println("Listing objects and copying objects");
        if (!prefix.endsWith(delimiter)) {
            prefix += delimiter;
        }
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
            .withBucketName(bucketName)
            .withPrefix(prefix)
            .withDelimiter(delimiter);
        ObjectListing objectListing;            
            objectListing = s3Client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : 
                objectListing.getObjectSummaries()) { s3Client.copyObject(bucketName, objectSummary.getKey(), bucketName2,  "output/" + objectSummary.getKey());
                System.out.println(" - " + objectSummary.getKey() + "  " +
                        "(size = " + objectSummary.getSize() + 
                        ")");
            listObjectsRequest.setMarker(objectListing.getNextMarker());

I set up something to try this out with you. 我设置了一些东西来与您一起尝试。 I created two buckets, one called mytestbucket20141006, which acts as the source, and another one called mytestbucket20141006b, acts as destination. 我创建了两个存储桶,一个名为mytestbucket20141006,用作源,另一个称为mytestbucket20141006b,用作目标。 Inside the source bucket, I have a folder called afolder, which contains couple files. 在源存储桶中,我有一个名为afolder的文件夹,其中包含几个文件。

The following code successfully copied objects to the destination: 以下代码将对象成功复制到目标:

    ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
        .withBucketName("mytestbucket20141006")
        .withPrefix("afolder/afolder2/afolder3");
        //.withDelimiter("/");
    ObjectListing objectListing = s3.listObjects(listObjectsRequest);
    for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
        String key = objectSummary.getKey();
        String newKey = key.replace("afolder/afolder2/afolder3/", "afolder3/");
        s3.copyObject("mytestbucket20141006", key, "mytestbucket20141006b",  newKey);
        System.out.println(key);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 项目“ProjectA”中的源文件夹“folder1”无法输出到 Eclipse 中的不同源文件夹“folder2” - Source Folder 'folder1' in project 'ProjectA' cannot output to distinct source folder 'folder2' in Eclipse 将文件夹发送到 s3 存储桶,但不要复制文件夹结构 - Send folder to s3 bucket, but do not copy the folder structure 如何将文件夹复制到temp目录? - How do I copy a folder into the temp directory? 如何将Android SD卡上的本地文件夹的内容与Dropbox上的文件夹的内容进行比较? - How do I campare contents of local folder on android sdcard with the contents of a folder on dropbox? 如何将单个文件夹及其内容复制到Java中的另一个文件夹 - How to copy a single folder with its contents to another folder in Java 我如何读取文件夹并在Java中显示内容。 - How do i read a folder and display the contents in java. Java:如何从资源复制文件夹并复制到临时目录? - Java : How to copy folder with contents from resource and copy to temp directory? Gradle:如何将Servlet JARS复制到WAR文件中的文件夹? - Gradle: How do I copy servlet JARS to a folder in WAR file? 如何使用maven将文件复制到tomcat webapp文件夹? - How do I copy a file to tomcat webapp folder using maven? 如何选择要构建的src文件夹? - How do I choose which src folder to build?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM