英文:
Copy all .txt files from one object to another but in the same bucket using Java (Amazon S3)
问题
在名为myBucket
的s3存储桶中,有一个名为folder1
的文件夹中有一些文件。我需要将folder1
中的只有.txt
扩展名的文件复制到folder2
中。
注意:folder1
和folder2
都存在于同一个存储桶中。
我该如何使用Java来实现这个操作?
两个文件夹的层级结构如下:
myBucket/folder1
myBucket/folder2
英文:
In my bucket named myBucket
on s3, There are some files in a folder called folder1
. I need to copy only the .txt
files from folder1
to folder2
.
Note: Both folder1
and folder2
are present in the same bucket.
How can I do this using Java?
The hierarchy of both the folders are:
myBucket/folder1
myBucket/folder2
答案1
得分: 1
S3允许您按照特定前缀列出存储桶中的对象,在您的情况下,您可以更改对象名称,以便前缀包含文件类型(目前暂时没有后缀函数 )。
示例文件名:
TXT_file1.txt
ObjectListing objectList = s3client.listObjects(new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix("TXT"));
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
s3client.copyObject(sourceBucketName, objectSummary.getKey(), destinationBucketName, objectSummary.getKey());
}
或者...
您可以获取所有文件,然后在循环内部根据文件名进行过滤 这样您就不需要更改文件名,但是这会导致S3中的更多GET请求,从而产生更多的费用。
英文:
S3 gives you the ability to list objects in a bucket with a certain prefix, in your case you can change the object name so that the prefix contains
the file type )for the moment not have suffix function )
Example name file:
TXT_file1.txt
ObjectListing objectList = s3client.listObjects(new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix("TXT"));
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
s3client.copyObject(sourceBucketName, objectSummary.getKey(), estinationBucketName, objectSummary.getKey());
}
or...
you can get all and filter with the name of the files inside the for and you don't need to change the file names, but you have more get request in s3 and that implies more costs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论