首页 > 解决方案 > 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-web-servicesamazon-s3

解决方案


您可以使用 Amazon S3 开发工具包来执行此任务。最简单的方法是调用CopyObject并传入一个新名称。例如,如果您要在 Java 中实现此功能(您也可以使用 AWS 开发工具包支持的其他语言执行此功能),您可以使用以下代码:

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>\n\n" +
                "Where:\n" +
                "    objectKey - the name of the object (for example, book.pdf).\n\n" +
                "    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 %s\n",
                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]
}

推荐阅读