[英]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.