对象存储

  • 对象存储 > 使用指南 > 开发指南 > AWS S3 兼容 > 兼容 SDK 示例 > AWS SDK for Java

    AWS SDK for Java

    最近更新时间: 2024-02-19 17:12:51

    导入 AWS SDK for Java

    确保 Java v1.8 或更新版本和 Gradle 已经安装。

    build.gradledependencies 代码块中增加如下代码

    dependencies {
        implementation platform('software.amazon.awssdk:bom:2.20.151')
        implementation 'software.amazon.awssdk:s3'
        implementation 'software.amazon.awssdk:s3-transfer-manager'
        implementation 'software.amazon.awssdk:sts'
    }
    

    com.qiniu.s3.examples.App 包为例,这里提供 build.gradle 的完整案例(Groovy DSL 版本)

    plugins {
        id 'application'
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation platform('software.amazon.awssdk:bom:2.20.151')
        implementation 'software.amazon.awssdk:s3'
        implementation 'software.amazon.awssdk:s3-transfer-manager'
        implementation 'software.amazon.awssdk:sts'
        testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
        testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
        implementation 'com.google.guava:guava:32.1.1-jre'
    }
    
    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(8)
        }
    }
    
    application {
        mainClass = 'com.qiniu.s3.examples.App'
    }
    
    tasks.named('test') {
        useJUnitPlatform()
    }
    

    对于之后的每个代码示例,将代码创建在 app/src/main/java/com/qiniu/s3/examples/App.java 后,执行

    gradle run
    

    即可执行代码。

    对象上传

    获取客户端上传 URL

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.net.URI;
    import java.time.Duration;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.presigner.S3Presigner;
    import software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest;
    
    public class App {
        public static void main(String[] args) {
            final S3Presigner presigner = S3Presigner.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            final PresignedPutObjectRequest presignedPutObjectRequest = presigner
                    .presignPutObject(b -> b.putObjectRequest(builder -> builder.bucket("<Bucket>").key("<Key>"))
                            .signatureDuration(Duration.ofHours(1)));
            System.out.println(presignedPutObjectRequest.url());
        }
    }
    

    这段代码将生成一个经过预先签名的客户端上传 URL,有效期为一小时,客户端可以在有效时间内对该 URL 发送 HTTP PUT 请求将文件上传。

    以下是用 curl 上传文件的案例:

    curl -X PUT --upload-file "<path/to/file>" "<presigned url>"
    

    服务器端直传

    单请求上传(文件)

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.File;
    import java.net.URI;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.core.sync.RequestBody;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.PutObjectResponse;
    
    public class App {
        public static void main(String[] args) {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            final PutObjectResponse putObjectResponse = s3.putObject(b -> b.bucket("<Bucket>").key("<Key>"),
                    RequestBody.fromFile(new File("<path/to/upload>")));
            System.out.printf("ETag: %s\n", putObjectResponse.eTag());
        }
    }
    

    单请求上传(数据流)

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.net.URI;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.core.sync.RequestBody;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.PutObjectResponse;
    
    public class App {
        public static void main(String[] args) throws IOException {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            try (final RandomAccessFile file = new RandomAccessFile("<path/to/upload>", "r")) { // 这里还是以文件作为数据流案例
                final PutObjectResponse putObjectResponse = s3.putObject(b -> b.bucket("<Bucket>").key("<Key>"),
                        RequestBody.fromInputStream(new FileInputStream(file.getFD()), file.length())); // AWS Java SDK 规定数据流必须给出大小,或是实现 ContentStreamProvider 接口实现数据流的重置
                System.out.printf("ETag: %s\n", putObjectResponse.eTag());
            }
        }
    }
    

    分片上传(文件)

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URI;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Vector;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.core.sync.RequestBody;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse;
    import software.amazon.awssdk.services.s3.model.CompletedPart;
    import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse;
    import software.amazon.awssdk.services.s3.model.UploadPartResponse;
    
    public class App {
        public static void main(String[] args) throws IOException {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            final int PART_SIZE = 5 * 1024 * 1024; // 分片大小为 5 MB
            try (final InputStream inputStream = new FileInputStream("<path/to/upload>")) {
                final CreateMultipartUploadResponse createMultipartUploadResponse = s3
                        .createMultipartUpload(builder -> builder.bucket("<Bucket>").key("<Key>"));
                // 这里给出的案例是串行分片上传。可以自行改造成并行分片上传以进一步提升上传速度
                final byte[] bytes = new byte[PART_SIZE];
                final List<CompletedPart> parts = new Vector<>();
                for (int partNumber = 1;; partNumber++) {
                    final int haveRead = inputStream.read(bytes);
                    if (haveRead <= 0) {
                        break;
                    }
                    final int pn = partNumber;
                    final UploadPartResponse uploadPartResponse = s3
                            .uploadPart(builder -> builder.bucket(createMultipartUploadResponse.bucket())
                                    .key(createMultipartUploadResponse.key())
                                    .uploadId(createMultipartUploadResponse.uploadId()).partNumber(pn),
                                    RequestBody.fromBytes(Arrays.copyOf(bytes, haveRead)));
                    parts.add(CompletedPart.builder().eTag(uploadPartResponse.eTag()).partNumber(pn).build());
                }
                final CompleteMultipartUploadResponse completeMultipartUploadResponse = s3
                        .completeMultipartUpload(builder -> builder.bucket(createMultipartUploadResponse.bucket())
                                .key(createMultipartUploadResponse.key()).uploadId(createMultipartUploadResponse.uploadId())
                                .multipartUpload(b -> b.parts(parts)));
                System.out.printf("ETag: %s\n", completeMultipartUploadResponse.eTag());
            }
        }
    }
    

    上传文件

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.File;
    import java.io.IOException;
    import java.net.URI;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.core.async.AsyncRequestBody;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3AsyncClient;
    import software.amazon.awssdk.transfer.s3.S3TransferManager;
    import software.amazon.awssdk.transfer.s3.model.CompletedUpload;
    
    public class App {
        public static void main(String[] args) throws IOException {
            final S3AsyncClient s3 = S3AsyncClient.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
    
            final S3TransferManager transferManager = S3TransferManager.builder().s3Client(s3).build();
            final CompletedUpload fileUpload = transferManager
                    .upload(b -> b.putObjectRequest(builder -> builder.bucket("<Bucket>").key("<Key>"))
                            .requestBody(AsyncRequestBody.fromFile(new File("<path/to/upload>"))))
                    .completionFuture().join();
            System.out.printf("ETag: %s\n", fileUpload.response().eTag());
        }
    }
    

    上传目录

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.IOException;
    import java.net.URI;
    import java.nio.file.Paths;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3AsyncClient;
    import software.amazon.awssdk.transfer.s3.S3TransferManager;
    import software.amazon.awssdk.transfer.s3.model.CompletedDirectoryUpload;
    import software.amazon.awssdk.transfer.s3.model.DirectoryUpload;
    
    public class App {
        public static void main(String[] args) throws IOException {
            final S3AsyncClient s3 = S3AsyncClient.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
    
            final S3TransferManager transferManager = S3TransferManager.builder().s3Client(s3).build();
            final DirectoryUpload directoryUpload = transferManager
                    .uploadDirectory(b -> b.bucket("<Bucket>").source(Paths.get("<path/to/upload>")).s3Prefix("<KeyPrefix>"));
            final CompletedDirectoryUpload completedDirectoryUpload = directoryUpload.completionFuture().join();
            completedDirectoryUpload.failedTransfers().forEach(System.out::println);
        }
    }
    

    对象下载

    获取客户端下载 URL

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.net.URI;
    import java.time.Duration;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.presigner.S3Presigner;
    import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;
    
    public class App {
        public static void main(String[] args) {
            final S3Presigner presigner = S3Presigner.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            final PresignedGetObjectRequest presignedGetObjectRequest = presigner
                    .presignGetObject(b -> b.getObjectRequest(builder -> builder.bucket("<Bucket>").key("<Key>"))
                            .signatureDuration(Duration.ofHours(1)));
            System.out.println(presignedGetObjectRequest.url());
        }
    }
    

    这段代码将生成一个经过预先签名的客户端下载 URL,有效期为一小时,客户端可以在有效时间内对该 URL 发送 HTTP GET 请求将文件下载。

    以下是用 curl 下载文件的案例:

    curl -o "<path/to/download>" "<presigned url>"
    

    服务器端直接下载

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.File;
    import java.net.URI;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.GetObjectResponse;
    
    public class App {
        public static void main(String[] args) {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            final GetObjectResponse getObjectResponse = s3.getObject(b -> b.bucket("<Bucket>").key("<Key>"), new File("<path/to/upload>").toPath());
            System.out.printf("ETag: %s\n", getObjectResponse.eTag());
        }
    }
    

    下载目录

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.IOException;
    import java.net.URI;
    import java.nio.file.Paths;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3AsyncClient;
    import software.amazon.awssdk.transfer.s3.S3TransferManager;
    import software.amazon.awssdk.transfer.s3.model.CompletedDirectoryDownload;
    import software.amazon.awssdk.transfer.s3.model.DirectoryDownload;
    
    public class App {
        public static void main(String[] args) throws IOException {
            final S3AsyncClient s3 = S3AsyncClient.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
    
            final S3TransferManager transferManager = S3TransferManager.builder().s3Client(s3).build();
            final DirectoryDownload directoryDownload = transferManager.downloadDirectory(b -> b.bucket("<Bucket>")
                    .destination(Paths.get("<path/to/download>")).listObjectsV2RequestTransformer(l -> l.prefix("<KeyPrefix>")));
            final CompletedDirectoryDownload completedDirectoryDownload = directoryDownload.completionFuture().join();
            completedDirectoryDownload.failedTransfers().forEach(System.out::println);
        }
    }
    

    对象管理

    获取对象信息

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.net.URI;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.HeadObjectResponse;
    
    public class App {
        public static void main(String[] args) {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            final HeadObjectResponse headObjectResponse = s3.headObject(b -> b.bucket("<Bucket>").key("<Key>"));
            System.out.printf("ETag: %s\n", headObjectResponse.eTag());
        }
    }
    

    修改对象 MimeType

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.net.URI;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.MetadataDirective;
    
    public class App {
        public static void main(String[] args) {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            s3.copyObject(b -> b.sourceBucket("<Bucket>").sourceKey("<Key>").destinationBucket("<Bucket>").destinationKey("<Key>")
                    .metadataDirective(MetadataDirective.REPLACE).contentType("<NewContentType>"));
            System.out.println("Done");
        }
    }
    

    修改对象存储类型

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.net.URI;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.MetadataDirective;
    import software.amazon.awssdk.services.s3.model.StorageClass;
    
    public class App {
        public static void main(String[] args) {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            s3.copyObject(b -> b.sourceBucket("<Bucket>").sourceKey("<Key>").destinationBucket("<Bucket>").destinationKey("<Key>")
                    .metadataDirective(MetadataDirective.REPLACE).storageClass(StorageClass.GLACIER));
            System.out.println("Done");
        }
    }
    

    复制对象副本

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.net.URI;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.MetadataDirective;
    
    public class App {
        public static void main(String[] args) {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            s3.copyObject(b -> b.sourceBucket("<FromBucket>").sourceKey("<FromKey>").destinationBucket("<ToBucket>").destinationKey("<ToKey>")
                    .metadataDirective(MetadataDirective.COPY));
            System.out.println("Done");
        }
    }
    

    复制对象副本(大于 5GB)

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.IOException;
    import java.net.URI;
    import java.util.List;
    import java.util.Vector;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse;
    import software.amazon.awssdk.services.s3.model.CompletedPart;
    import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse;
    import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
    import software.amazon.awssdk.services.s3.model.HeadObjectResponse;
    import software.amazon.awssdk.services.s3.model.UploadPartCopyResponse;
    
    public class App {
        public static void main(String[] args) throws IOException {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            final HeadObjectRequest headObjectRequest = HeadObjectRequest.builder().bucket("<FromBucket>").key("<FromKey>")
                    .build();
            final HeadObjectResponse headObjectResponse = s3.headObject(headObjectRequest);
            final CreateMultipartUploadResponse createMultipartUploadResponse = s3
                    .createMultipartUpload(b -> b.bucket("<ToBucket>").key("<ToKey>"));
            final List<CompletedPart> parts = new Vector<>();
            final long PART_SIZE = 5 * 1024 * 1024; // 分片大小为 5 MB
            long copied = 0;
            // 这里给出的案例是串行分片复制。可以自行改造成并行分片复制以进一步提升复制速度
            for (int partNumber = 1; copied < headObjectResponse.contentLength(); partNumber++) {
                final long partSize = Math.min(PART_SIZE, headObjectResponse.contentLength() - copied);
                final int pn = partNumber;
                final String copySourceRange = String.format("bytes=%d-%d", copied, copied + partSize - 1);
                final UploadPartCopyResponse uploadPartCopyResponse = s3
                        .uploadPartCopy(b -> b.sourceBucket(headObjectRequest.bucket()).sourceKey(headObjectRequest.key())
                                .destinationBucket(createMultipartUploadResponse.bucket())
                                .destinationKey(createMultipartUploadResponse.key())
                                .uploadId(createMultipartUploadResponse.uploadId()).partNumber(pn)
                                .copySourceRange(copySourceRange));
                parts.add(CompletedPart.builder().eTag(uploadPartCopyResponse.copyPartResult().eTag()).partNumber(pn)
                        .build());
                copied += partSize;
            }
            final CompleteMultipartUploadResponse completeMultipartUploadResponse = s3.completeMultipartUpload(
                    b -> b.bucket(createMultipartUploadResponse.bucket()).key(createMultipartUploadResponse.key())
                            .uploadId(createMultipartUploadResponse.uploadId())
                            .multipartUpload(builder -> builder.parts(parts)));
            System.out.printf("ETag: %s\n", completeMultipartUploadResponse.eTag());
        }
    }
    

    删除空间中的文件

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.IOException;
    import java.net.URI;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    
    public class App {
        public static void main(String[] args) throws IOException {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            s3.deleteObject(b -> b.bucket("<Bucket>").key("<Key>"));
            System.out.println("Done");
        }
    }
    

    获取指定前缀的文件列表

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.IOException;
    import java.net.URI;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
    import software.amazon.awssdk.services.s3.model.S3Object;
    
    public class App {
        public static void main(String[] args) throws IOException {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            final ListObjectsV2Response listObjectsV2Response = s3.listObjectsV2(b -> b.bucket("<Bucket>").prefix("<KeyPrefix>"));
            for (final S3Object s3Object : listObjectsV2Response.contents()) {
                System.out.printf("Key: %s\n", s3Object.key());
                System.out.printf("ETag: %s\n", s3Object.eTag());
            }
        }
    }
    

    批量删除空间中的文件

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.IOException;
    import java.net.URI;
    import java.util.List;
    import java.util.Vector;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.ObjectIdentifier;
    
    public class App {
        public static void main(String[] args) throws IOException {
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(
                            StaticCredentialsProvider.create(
                                    AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                    .build();
            final List<ObjectIdentifier> objectIdentifiers = new Vector<>();
            objectIdentifiers.add(ObjectIdentifier.builder().key("<Key1>").build());
            objectIdentifiers.add(ObjectIdentifier.builder().key("<Key2>").build());
            objectIdentifiers.add(ObjectIdentifier.builder().key("<Key3>").build());
            s3.deleteObjects(b -> b.bucket("<Bucket>").delete(builder -> builder.objects(objectIdentifiers)));
            System.out.println("Done");
        }
    }
    

    临时安全凭证

    创建 app/src/main/java/com/qiniu/s3/examples/App.java

    package com.qiniu.s3.examples;
    
    import java.io.IOException;
    import java.net.URI;
    
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.Bucket;
    import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
    import software.amazon.awssdk.services.sts.StsClient;
    import software.amazon.awssdk.services.sts.auth.StsGetFederationTokenCredentialsProvider;
    
    public class App {
        public static void main(String[] args) throws IOException {
            final StsGetFederationTokenCredentialsProvider stsGetFederationTokenCredentialsProvider = StsGetFederationTokenCredentialsProvider
                    .builder()
                    .stsClient(StsClient.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                            .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                            .credentialsProvider(StaticCredentialsProvider
                                    .create(AwsBasicCredentials.create("<QiniuAccessKey>", "<QiniuSecretKey>")))
                            .build())
                    .refreshRequest(builder -> builder.name("Bob")
                            .durationSeconds(3600)
                            .policy("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Stmt1\",\"Effect\":\"Allow\",\"Action\":[\"*\"],\"Resource\":[\"*\"]}]}"))
                    .build();
            final S3Client s3 = S3Client.builder().region(Region.of("cn-east-1")) // 华东-浙江区 region id
                    .endpointOverride(URI.create("https://s3.cn-east-1.qiniucs.com")) // 华东-浙江区 endpoint
                    .credentialsProvider(stsGetFederationTokenCredentialsProvider)
                    .build();
            // 可以使用这些临时凭证调用 S3 服务
            final ListBucketsResponse listBucketsResponse = s3.listBuckets();
            for (final Bucket bucket : listBucketsResponse.buckets()) {
                System.out.println(bucket.name());
            }
        }
    }
    
    以上内容是否对您有帮助?
  • Qvm free helper
    Close