Amazon S3批量文件重命名



我的Amazon S3存储桶中有一堆文件,看起来像这样(它不止两个,有几百个):

09292021-testpilot-state-test-callers-09-29-2021-970669454.csv?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=BRIAN72GRAHWB6KIND7M%2F20210930%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210930T012101Z&X-Amz-Expires=172800&X-Amz-SignedHeaders=host&X-Amz-Signature=8d4de3f18g2c178e1f6c94d0a09bf6a07ffr731454da5ec8940868f499cfc2bc
09272021-testpilot-state-test-callers-09-29-2021-970669454.csv?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=STEVE2313DJFSOODC%2F20210930%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210930T012101Z&X-Amz-Expires=172800&X-Amz-SignedHeaders=host&X-Amz-Signature=8d4de3f18g2c178e1f6c53-309f3f6a07ffr731454da5ec8940868f499cfc2bc

我试着对它们进行批量重命名,这样你就可以删除.csv之后的所有内容,这样文件就会看起来像这样:

09292021-testpilot-state-test-callers-09-29-2021-970669454.csv
09272021-testpilot-state-test-callers-09-29-2021-970669454.csv

实现这一目标的最有效方法是什么?

可以使用Amazon S3 SDK执行此任务。最简单的方法是调用CopyObject然后输入一个新名字。例如,如果要在Java中实现此功能(也可以在AWS SDK支持的其他语言中执行此功能),则可以使用以下代码:

package com.example.s3;
// snippet-start:[s3.java2.copy_object.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CopyObjectRequest;
import software.amazon.awssdk.services.s3.model.CopyObjectResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
// snippet-end:[s3.java2.copy_object.import]
/**
* To run this AWS code example, ensure that you have setup your development environment, including your AWS credentials.
*
* For information, see this documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/

public class CopyObject {
public static void main(String[] args) {
final String USAGE = "n" +
"Usage:n" +
"    <objectKey> <fromBucket> <toBucket>nn" +
"Where:n" +
"    objectKey - the name of the object (for example, book.pdf).nn" +
"    fromBucket - the S3 bucket name that contains the object (for example, bucket1).n" +
"    toBucket - the S3 bucket to copy the object to (for example, bucket2).n";
if (args.length != 3) {
System.out.println(USAGE);
System.exit(1);
}
String objectKey = args[0];
String fromBucket = args[1];
String toBucket =  args[2];
System.out.format("Copying object %s from bucket %s to %sn",
objectKey, fromBucket, toBucket);
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder()
.region(region)
.build();
copyBucketObject (s3, fromBucket, objectKey, toBucket);
s3.close();
}
// snippet-start:[s3.java2.copy_object.main]
public static String copyBucketObject (S3Client s3, String fromBucket, String objectKey, String toBucket) {
String encodedUrl = null;
try {
encodedUrl = URLEncoder.encode(fromBucket + "/" + objectKey, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
System.out.println("URL could not be encoded: " + e.getMessage());
}
CopyObjectRequest copyReq = CopyObjectRequest.builder()
.copySource(encodedUrl)
.destinationBucket(toBucket)
.destinationKey(objectKey)
.build();
try {
CopyObjectResponse copyRes = s3.copyObject(copyReq);
return copyRes.copyObjectResult().toString();
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
// snippet-end:[s3.java2.copy_object.main]
}

最新更新