Copy all .txt files from one object to another but in the same bucket using Java (Amazon S3)

huangapple go评论90阅读模式
英文:

Copy all .txt files from one object to another but in the same bucket using Java (Amazon S3)

问题

在名为myBuckets3存储桶中,有一个名为folder1的文件夹中有一些文件。我需要将folder1中的只有.txt扩展名的文件复制到folder2中。
注意:folder1folder2都存在于同一个存储桶中。

我该如何使用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允许您按照特定前缀列出存储桶中的对象,在您的情况下,您可以更改对象名称,以便前缀包含文件类型(目前暂时没有后缀函数 Copy all .txt files from one object to another but in the same bucket using Java (Amazon 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());
}

或者...

您可以获取所有文件,然后在循环内部根据文件名进行过滤 Copy all .txt files from one object to another but in the same bucket using Java (Amazon S3) 这样您就不需要更改文件名,但是这会导致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 Copy all .txt files from one object to another but in the same bucket using Java (Amazon S3) )

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 Copy all .txt files from one object to another but in the same bucket using Java (Amazon S3) and you don't need to change the file names, but you have more get request in s3 and that implies more costs.

huangapple
  • 本文由 发表于 2020年10月8日 21:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64263219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定