用SDK于 Java 2.x 的 S3 目录存储桶示例 - AWS SDK for Java 2.x

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

用SDK于 Java 2.x 的 S3 目录存储桶示例

以下代码示例向您展示了如何使用 AWS SDK for Java 2.x 与 S3 Directory Buckets 配合使用来执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

场景是向您展示如何通过在一个服务中调用多个函数或与其他 AWS 服务结合来完成特定任务的代码示例。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

开始使用

以下代码示例显示了如何开始使用 Amazon S3 目录存储桶。

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

package com.example.s3.directorybucket; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.BucketInfo; import software.amazon.awssdk.services.s3.model.BucketType; import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; import software.amazon.awssdk.services.s3.model.CreateBucketResponse; import software.amazon.awssdk.services.s3.model.DataRedundancy; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; import software.amazon.awssdk.services.s3.model.ListDirectoryBucketsRequest; import software.amazon.awssdk.services.s3.model.ListDirectoryBucketsResponse; import software.amazon.awssdk.services.s3.model.LocationInfo; import software.amazon.awssdk.services.s3.model.LocationType; import software.amazon.awssdk.services.s3.model.S3Exception; import java.util.List; import java.util.stream.Collectors; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; /** * Before running this example: * <p> * The SDK must be able to authenticate AWS requests on your behalf. If you have * not configured * authentication for SDKs and tools, see * https://docs.aws.amazon.com/sdkref/latest/guide/access.html in the AWS SDKs * and Tools Reference Guide. * <p> * You must have a runtime environment configured with the Java SDK. * See * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html in * the Developer Guide if this is not set up. * <p> * To use S3 directory buckets, configure a gateway VPC endpoint. This is the * recommended method to enable directory bucket traffic without * requiring an internet gateway or NAT device. For more information on * configuring VPC gateway endpoints, visit * https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-express-networking.html#s3-express-networking-vpc-gateway. * <p> * Directory buckets are available in specific AWS Regions and Zones. For * details on Regions and Zones supporting directory buckets, see * https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-express-networking.html#s3-express-endpoints. */ public class HelloS3DirectoryBuckets { private static final Logger logger = LoggerFactory.getLogger(HelloS3DirectoryBuckets.class); public static void main(String[] args) { String bucketName = "test-bucket-" + System.currentTimeMillis() + "--usw2-az1--x-s3"; Region region = Region.US_WEST_2; String zone = "usw2-az1"; S3Client s3Client = createS3Client(region); try { // Create the directory bucket createDirectoryBucket(s3Client, bucketName, zone); logger.info("Created bucket: {}", bucketName); // List all directory buckets List<String> bucketNames = listDirectoryBuckets(s3Client); bucketNames.forEach(name -> logger.info("Bucket Name: {}", name)); } catch (S3Exception e) { logger.error("An error occurred during S3 operations: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); } finally { try { // Delete the created bucket deleteDirectoryBucket(s3Client, bucketName); logger.info("Deleted bucket: {}", bucketName); } catch (S3Exception e) { logger.error("Failed to delete the bucket due to S3 error: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); } catch (RuntimeException e) { logger.error("Failed to delete the bucket due to unexpected error: {}", e.getMessage(), e); } finally { s3Client.close(); } } } /** * Creates a new S3 directory bucket in a specified Zone (For example, a * specified Availability Zone in this code example). * * @param s3Client The S3 client used to create the bucket * @param bucketName The name of the bucket to be created * @param zone The region where the bucket will be created * @throws S3Exception if there's an error creating the bucket */ public static void createDirectoryBucket(S3Client s3Client, String bucketName, String zone) throws S3Exception { logger.info("Creating bucket: {}", bucketName); CreateBucketConfiguration bucketConfiguration = CreateBucketConfiguration.builder() .location(LocationInfo.builder() .type(LocationType.AVAILABILITY_ZONE) .name(zone).build()) .bucket(BucketInfo.builder() .type(BucketType.DIRECTORY) .dataRedundancy(DataRedundancy.SINGLE_AVAILABILITY_ZONE) .build()) .build(); try { CreateBucketRequest bucketRequest = CreateBucketRequest.builder() .bucket(bucketName) .createBucketConfiguration(bucketConfiguration).build(); CreateBucketResponse response = s3Client.createBucket(bucketRequest); logger.info("Bucket created successfully with location: {}", response.location()); } catch (S3Exception e) { logger.error("Error creating bucket: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } } /** * Lists all S3 directory buckets. * * @param s3Client The S3 client used to interact with S3 * @return A list of bucket names */ public static List<String> listDirectoryBuckets(S3Client s3Client) { logger.info("Listing all directory buckets"); try { // Create a ListBucketsRequest ListDirectoryBucketsRequest listBucketsRequest = ListDirectoryBucketsRequest.builder().build(); // Retrieve the list of buckets ListDirectoryBucketsResponse response = s3Client.listDirectoryBuckets(listBucketsRequest); // Extract bucket names List<String> bucketNames = response.buckets().stream() .map(Bucket::name) .collect(Collectors.toList()); return bucketNames; } catch (S3Exception e) { logger.error("Failed to list buckets: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } } /** * Deletes the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the bucket to delete */ public static void deleteDirectoryBucket(S3Client s3Client, String bucketName) { try { DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder() .bucket(bucketName) .build(); s3Client.deleteBucket(deleteBucketRequest); } catch (S3Exception e) { logger.error("Failed to delete bucket: " + bucketName + " - Error code: " + e.awsErrorDetails().errorCode(), e); throw e; } } }

操作

以下代码示例演示如何使用 AbortMultipartUpload

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

中止目录存储桶中的分段上传。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; /** * Aborts a specific multipart upload for the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to be uploaded * @param uploadId The upload ID of the multipart upload to abort * @return True if the multipart upload is successfully aborted, false otherwise */ public static boolean abortDirectoryBucketMultipartUpload(S3Client s3Client, String bucketName, String objectKey, String uploadId) { logger.info("Aborting multipart upload: {} for bucket: {}", uploadId, bucketName); try { // Abort the multipart upload AbortMultipartUploadRequest abortMultipartUploadRequest = AbortMultipartUploadRequest.builder() .bucket(bucketName) .key(objectKey) .uploadId(uploadId) .build(); s3Client.abortMultipartUpload(abortMultipartUploadRequest); logger.info("Aborted multipart upload: {} for object: {}", uploadId, objectKey); return true; } catch (S3Exception e) { logger.error("Failed to abort multipart upload: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); return false; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 AbortMultipartUpload” 中的。

以下代码示例演示如何使用 CompleteMultipartUpload

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

在目录存储桶中完成分段上传。

import com.example.s3.util.S3DirectoryBucketUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest; import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse; import software.amazon.awssdk.services.s3.model.CompletedMultipartUpload; import software.amazon.awssdk.services.s3.model.CompletedPart; import software.amazon.awssdk.services.s3.model.S3Exception; import java.io.IOException; import java.nio.file.Path; import java.util.List; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.multipartUploadForDirectoryBucket; /** * This method completes the multipart upload request by collating all the * upload parts. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to be uploaded * @param uploadId The upload ID used to track the multipart upload * @param uploadParts The list of completed parts * @return True if the multipart upload is successfully completed, false * otherwise */ public static boolean completeDirectoryBucketMultipartUpload(S3Client s3Client, String bucketName, String objectKey, String uploadId, List<CompletedPart> uploadParts) { try { CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder() .parts(uploadParts) .build(); CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder() .bucket(bucketName) .key(objectKey) .uploadId(uploadId) .multipartUpload(completedMultipartUpload) .build(); CompleteMultipartUploadResponse response = s3Client.completeMultipartUpload(completeMultipartUploadRequest); logger.info("Multipart upload completed. ETag: {}", response.eTag()); return true; } catch (S3Exception e) { logger.error("Failed to complete multipart upload: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); return false; } }

以下代码示例演示如何使用 CopyObject

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

将对象从目录存储桶复制到目录存储桶。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.nio.file.Path; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketObject; /** * Copies an object from one S3 general purpose bucket to one S3 directory * bucket. * * @param s3Client The S3 client used to interact with S3 * @param sourceBucket The name of the source bucket * @param objectKey The key (name) of the object to be copied * @param targetBucket The name of the target bucket */ public static void copyDirectoryBucketObject(S3Client s3Client, String sourceBucket, String objectKey, String targetBucket) { logger.info("Copying object: {} from bucket: {} to bucket: {}", objectKey, sourceBucket, targetBucket); try { // Create a CopyObjectRequest CopyObjectRequest copyReq = CopyObjectRequest.builder() .sourceBucket(sourceBucket) .sourceKey(objectKey) .destinationBucket(targetBucket) .destinationKey(objectKey) .build(); // Copy the object CopyObjectResponse copyRes = s3Client.copyObject(copyReq); logger.info("Successfully copied {} from bucket {} into bucket {}. CopyObjectResponse: {}", objectKey, sourceBucket, targetBucket, copyRes.copyObjectResult().toString()); } catch (S3Exception e) { logger.error("Failed to copy object: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 CopyObject” 中的。

以下代码示例演示如何使用 CreateBucket

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

创建 S3 目录存储桶。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.BucketInfo; import software.amazon.awssdk.services.s3.model.BucketType; import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; import software.amazon.awssdk.services.s3.model.CreateBucketResponse; import software.amazon.awssdk.services.s3.model.DataRedundancy; import software.amazon.awssdk.services.s3.model.LocationInfo; import software.amazon.awssdk.services.s3.model.LocationType; import software.amazon.awssdk.services.s3.model.S3Exception; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; /** * Creates a new S3 directory bucket in a specified Zone (For example, a * specified Availability Zone in this code example). * * @param s3Client The S3 client used to create the bucket * @param bucketName The name of the bucket to be created * @param zone The region where the bucket will be created * @throws S3Exception if there's an error creating the bucket */ public static void createDirectoryBucket(S3Client s3Client, String bucketName, String zone) throws S3Exception { logger.info("Creating bucket: {}", bucketName); CreateBucketConfiguration bucketConfiguration = CreateBucketConfiguration.builder() .location(LocationInfo.builder() .type(LocationType.AVAILABILITY_ZONE) .name(zone).build()) .bucket(BucketInfo.builder() .type(BucketType.DIRECTORY) .dataRedundancy(DataRedundancy.SINGLE_AVAILABILITY_ZONE) .build()) .build(); try { CreateBucketRequest bucketRequest = CreateBucketRequest.builder() .bucket(bucketName) .createBucketConfiguration(bucketConfiguration).build(); CreateBucketResponse response = s3Client.createBucket(bucketRequest); logger.info("Bucket created successfully with location: {}", response.location()); } catch (S3Exception e) { logger.error("Error creating bucket: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 CreateBucket” 中的。

以下代码示例演示如何使用 CreateMultipartUpload

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

在目录存储桶中创建分段上传。

import com.example.s3.util.S3DirectoryBucketUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest; import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; /** * This method creates a multipart upload request that generates a unique upload * ID used to track * all the upload parts. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to be uploaded * @return The upload ID used to track the multipart upload */ public static String createDirectoryBucketMultipartUpload(S3Client s3Client, String bucketName, String objectKey) { logger.info("Creating multipart upload for object: {} in bucket: {}", objectKey, bucketName); try { // Create a CreateMultipartUploadRequest CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder() .bucket(bucketName) .key(objectKey) .build(); // Initiate the multipart upload CreateMultipartUploadResponse response = s3Client.createMultipartUpload(createMultipartUploadRequest); String uploadId = response.uploadId(); logger.info("Multipart upload initiated. Upload ID: {}", uploadId); return uploadId; } catch (S3Exception e) { logger.error("Failed to create multipart upload: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }

以下代码示例演示如何使用 DeleteBucket

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

删除 S3 目录存储桶。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; /** * Deletes the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket to delete */ public static void deleteDirectoryBucket(S3Client s3Client, String bucketName) { logger.info("Deleting bucket: {}", bucketName); try { // Create a DeleteBucketRequest DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder() .bucket(bucketName) .build(); // Delete the bucket s3Client.deleteBucket(deleteBucketRequest); logger.info("Successfully deleted bucket: {}", bucketName); } catch (S3Exception e) { logger.error("Failed to delete bucket: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DeleteBucket” 中的。

以下代码示例演示如何使用 DeleteBucketEncryption

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

删除目录存储桶的加密配置。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteBucketEncryptionRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; /** * Deletes the encryption configuration from an S3 bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket */ public static void deleteDirectoryBucketEncryption(S3Client s3Client, String bucketName) { DeleteBucketEncryptionRequest deleteRequest = DeleteBucketEncryptionRequest.builder() .bucket(bucketName) .build(); try { s3Client.deleteBucketEncryption(deleteRequest); logger.info("Bucket encryption deleted for bucket: {}", bucketName); } catch (S3Exception e) { logger.error("Failed to delete bucket encryption: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }

以下代码示例演示如何使用 DeleteBucketPolicy

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

删除目录存储桶的存储桶策略。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteBucketPolicyRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getAwsAccountId; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketPolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Deletes the bucket policy for the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket */ public static void deleteDirectoryBucketPolicy(S3Client s3Client, String bucketName) { logger.info("Deleting policy for bucket: {}", bucketName); try { // Create a DeleteBucketPolicyRequest DeleteBucketPolicyRequest deletePolicyReq = DeleteBucketPolicyRequest.builder() .bucket(bucketName) .build(); // Delete the bucket policy s3Client.deleteBucketPolicy(deletePolicyReq); logger.info("Successfully deleted bucket policy"); } catch (S3Exception e) { logger.error("Failed to delete bucket policy: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DeleteBucketPolicy” 中的。

以下代码示例演示如何使用 DeleteObject

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

删除目录存储桶中的对象。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import java.nio.file.Path; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketObject; /** * Deletes an object from the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to be deleted */ public static void deleteDirectoryBucketObject(S3Client s3Client, String bucketName, String objectKey) { logger.info("Deleting object: {} from bucket: {}", objectKey, bucketName); try { // Create a DeleteObjectRequest DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder() .bucket(bucketName) .key(objectKey) .build(); // Delete the object s3Client.deleteObject(deleteObjectRequest); logger.info("Object {} has been deleted", objectKey); } catch (S3Exception e) { logger.error("Failed to delete object: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DeleteObject” 中的。

以下代码示例演示如何使用 DeleteObjects

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

删除目录存储桶中的多个对象。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.Delete; import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest; import software.amazon.awssdk.services.s3.model.DeleteObjectsResponse; import software.amazon.awssdk.services.s3.model.ObjectIdentifier; import software.amazon.awssdk.services.s3.model.S3Exception; import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketObject; /** * Deletes multiple objects from the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKeys The list of keys (names) of the objects to be deleted */ public static void deleteDirectoryBucketObjects(S3Client s3Client, String bucketName, List<String> objectKeys) { logger.info("Deleting objects from bucket: {}", bucketName); try { // Create a list of ObjectIdentifier List<ObjectIdentifier> identifiers = objectKeys.stream() .map(key -> ObjectIdentifier.builder().key(key).build()) .toList(); // Create a Delete object Delete delete = Delete.builder() .objects(identifiers) .build(); // Create a DeleteObjectsRequest DeleteObjectsRequest deleteObjectsRequest = DeleteObjectsRequest.builder() .bucket(bucketName) .delete(delete) .build(); // Delete the objects DeleteObjectsResponse deleteObjectsResponse = s3Client.deleteObjects(deleteObjectsRequest); deleteObjectsResponse.deleted().forEach(deleted -> logger.info("Deleted object: {}", deleted.key())); } catch (S3Exception e) { logger.error("Failed to delete objects: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DeleteObjects” 中的。

以下代码示例演示如何使用 GetBucketEncryption

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

获取目录存储桶的加密配置。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetBucketEncryptionRequest; import software.amazon.awssdk.services.s3.model.GetBucketEncryptionResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.ServerSideEncryptionRule; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; /** * Retrieves the encryption configuration for an S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @return The type of server-side encryption applied to the bucket (e.g., * AES256, aws:kms) */ public static String getDirectoryBucketEncryption(S3Client s3Client, String bucketName) { try { // Create a GetBucketEncryptionRequest GetBucketEncryptionRequest getRequest = GetBucketEncryptionRequest.builder() .bucket(bucketName) .build(); // Retrieve the bucket encryption configuration GetBucketEncryptionResponse response = s3Client.getBucketEncryption(getRequest); ServerSideEncryptionRule rule = response.serverSideEncryptionConfiguration().rules().get(0); String encryptionType = rule.applyServerSideEncryptionByDefault().sseAlgorithmAsString(); logger.info("Bucket encryption algorithm: {}", encryptionType); logger.info("KMS Customer Managed Key ID: {}", rule.applyServerSideEncryptionByDefault().kmsMasterKeyID()); logger.info("Bucket Key Enabled: {}", rule.bucketKeyEnabled()); return encryptionType; } catch (S3Exception e) { logger.error("Failed to get bucket encryption: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 GetBucketEncryption” 中的。

以下代码示例演示如何使用 GetBucketPolicy

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

获取目录存储桶的策略。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetBucketPolicyRequest; import software.amazon.awssdk.services.s3.model.GetBucketPolicyResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getAwsAccountId; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketPolicy; /** * Retrieves the bucket policy for the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @return The bucket policy text */ public static String getDirectoryBucketPolicy(S3Client s3Client, String bucketName) { logger.info("Getting policy for bucket: {}", bucketName); try { // Create a GetBucketPolicyRequest GetBucketPolicyRequest policyReq = GetBucketPolicyRequest.builder() .bucket(bucketName) .build(); // Retrieve the bucket policy GetBucketPolicyResponse response = s3Client.getBucketPolicy(policyReq); // Print and return the policy text String policyText = response.policy(); logger.info("Bucket policy: {}", policyText); return policyText; } catch (S3Exception e) { logger.error("Failed to get bucket policy: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 GetBucketPolicy” 中的。

以下代码示例演示如何使用 GetObject

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

从目录存储桶中获取对象。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.GetObjectResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketObject; /** * Retrieves an object from the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to be retrieved * @return The retrieved object as a ResponseInputStream */ public static boolean getDirectoryBucketObject(S3Client s3Client, String bucketName, String objectKey) { logger.info("Retrieving object: {} from bucket: {}", objectKey, bucketName); try { // Create a GetObjectRequest GetObjectRequest objectRequest = GetObjectRequest.builder() .key(objectKey) .bucket(bucketName) .build(); // Retrieve the object as bytes ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest); byte[] data = objectBytes.asByteArray(); // Print object contents to console String objectContent = new String(data, StandardCharsets.UTF_8); logger.info("Object contents: \n{}", objectContent); return true; } catch (S3Exception e) { logger.error("Failed to retrieve object: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); return false; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 GetObject” 中的。

以下代码示例演示如何使用 GetObjectAttributes

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

从目录存储桶获取对象属性。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetObjectAttributesRequest; import software.amazon.awssdk.services.s3.model.GetObjectAttributesResponse; import software.amazon.awssdk.services.s3.model.ObjectAttributes; import software.amazon.awssdk.services.s3.model.S3Exception; import java.nio.file.Path; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketObject; /** * Retrieves attributes for an object in the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to retrieve attributes for * @return True if the object attributes are successfully retrieved, false * otherwise */ public static boolean getDirectoryBucketObjectAttributes(S3Client s3Client, String bucketName, String objectKey) { logger.info("Retrieving attributes for object: {} from bucket: {}", objectKey, bucketName); try { // Create a GetObjectAttributesRequest GetObjectAttributesRequest getObjectAttributesRequest = GetObjectAttributesRequest.builder() .bucket(bucketName) .key(objectKey) .objectAttributes(ObjectAttributes.E_TAG, ObjectAttributes.STORAGE_CLASS, ObjectAttributes.OBJECT_SIZE) .build(); // Retrieve the object attributes GetObjectAttributesResponse response = s3Client.getObjectAttributes(getObjectAttributesRequest); logger.info("Attributes for object {}:", objectKey); logger.info("ETag: {}", response.eTag()); logger.info("Storage Class: {}", response.storageClass()); logger.info("Object Size: {}", response.objectSize()); return true; } catch (S3Exception e) { logger.error("Failed to retrieve object attributes: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); return false; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 GetObjectAttributes” 中的。

以下代码示例演示如何使用 HeadBucket

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

检查指定的 S3 目录存储桶是否存在并且可以访问。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.HeadBucketRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; /** * Checks if the specified S3 directory bucket exists and is accessible. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket to check * @return True if the bucket exists and is accessible, false otherwise */ public static boolean headDirectoryBucket(S3Client s3Client, String bucketName) { logger.info("Checking if bucket exists: {}", bucketName); try { // Create a HeadBucketRequest HeadBucketRequest headBucketRequest = HeadBucketRequest.builder() .bucket(bucketName) .build(); // If the bucket doesn't exist, the following statement throws NoSuchBucketException, // which is a subclass of S3Exception. s3Client.headBucket(headBucketRequest); logger.info("Amazon S3 directory bucket: \"{}\" found.", bucketName); return true; } catch (S3Exception e) { logger.error("Failed to access bucket: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 HeadBucket” 中的。

以下代码示例演示如何使用 HeadObject

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

获取目录存储桶中对象的元数据。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.HeadObjectRequest; import software.amazon.awssdk.services.s3.model.HeadObjectResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import java.nio.file.Path; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketObject; /** * Retrieves metadata for an object in the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to retrieve metadata for * @return True if the object exists, false otherwise */ public static boolean headDirectoryBucketObject(S3Client s3Client, String bucketName, String objectKey) { logger.info("Retrieving metadata for object: {} from bucket: {}", objectKey, bucketName); try { // Create a HeadObjectRequest HeadObjectRequest headObjectRequest = HeadObjectRequest.builder() .bucket(bucketName) .key(objectKey) .build(); // Retrieve the object metadata HeadObjectResponse response = s3Client.headObject(headObjectRequest); logger.info("Amazon S3 object: \"{}\" found in bucket: \"{}\" with ETag: \"{}\"", objectKey, bucketName, response.eTag()); logger.info("Content-Type: {}", response.contentType()); logger.info("Content-Length: {}", response.contentLength()); logger.info("Last Modified: {}", response.lastModified()); return true; } catch (S3Exception e) { logger.error("Failed to retrieve object metadata: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); return false; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 HeadObject” 中的。

以下代码示例演示如何使用 ListDirectoryBuckets

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

列出所有目录存储桶。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.ListDirectoryBucketsRequest; import software.amazon.awssdk.services.s3.model.ListDirectoryBucketsResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; /** * Lists all S3 directory buckets and no general purpose buckets. * * @param s3Client The S3 client used to interact with S3 * @return A list of bucket names */ public static List<String> listDirectoryBuckets(S3Client s3Client) { logger.info("Listing all directory buckets"); try { // Create a ListBucketsRequest ListDirectoryBucketsRequest listDirectoryBucketsRequest = ListDirectoryBucketsRequest.builder().build(); // Retrieve the list of buckets ListDirectoryBucketsResponse response = s3Client.listDirectoryBuckets(listDirectoryBucketsRequest); // Extract bucket names List<String> bucketNames = response.buckets().stream() .map(Bucket::name) .collect(Collectors.toList()); return bucketNames; } catch (S3Exception e) { logger.error("Failed to list buckets: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 ListDirectoryBuckets” 中的。

以下代码示例演示如何使用 ListMultipartUploads

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

列出目录存储桶中的分段上传。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.ListMultipartUploadsRequest; import software.amazon.awssdk.services.s3.model.ListMultipartUploadsResponse; import software.amazon.awssdk.services.s3.model.MultipartUpload; import software.amazon.awssdk.services.s3.model.S3Exception; import java.io.IOException; import java.nio.file.Path; import java.util.List; import static com.example.s3.util.S3DirectoryBucketUtils.abortDirectoryBucketMultipartUploads; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.multipartUploadForDirectoryBucket; /** * Lists multipart uploads for the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @return A list of MultipartUpload objects representing the multipart uploads */ public static List<MultipartUpload> listDirectoryBucketMultipartUploads(S3Client s3Client, String bucketName) { logger.info("Listing in-progress multipart uploads for bucket: {}", bucketName); try { // Create a ListMultipartUploadsRequest ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder() .bucket(bucketName) .build(); // List the multipart uploads ListMultipartUploadsResponse response = s3Client.listMultipartUploads(listMultipartUploadsRequest); List<MultipartUpload> uploads = response.uploads(); for (MultipartUpload upload : uploads) { logger.info("In-progress multipart upload: Upload ID: {}, Key: {}, Initiated: {}", upload.uploadId(), upload.key(), upload.initiated()); } return uploads; } catch (S3Exception e) { logger.error("Failed to list multipart uploads: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); return List.of(); // Return an empty list if an exception is thrown } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 ListMultipartUploads” 中的。

以下代码示例演示如何使用 ListObjectsV2

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

列出目录存储桶中的对象。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; import software.amazon.awssdk.services.s3.model.ListObjectsV2Response; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.S3Object; import java.nio.file.Path; import java.util.List; import java.util.stream.Collectors; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketObject; /** * Lists objects in the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @return A list of object keys in the bucket */ public static List<String> listDirectoryBucketObjectsV2(S3Client s3Client, String bucketName) { logger.info("Listing objects in bucket: {}", bucketName); try { // Create a ListObjectsV2Request ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder() .bucket(bucketName) .build(); // Retrieve the list of objects ListObjectsV2Response response = s3Client.listObjectsV2(listObjectsV2Request); // Extract and return the object keys return response.contents().stream() .map(S3Object::key) .collect(Collectors.toList()); } catch (S3Exception e) { logger.error("Failed to list objects: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } }
  • 有关API详细信息,请参阅《AWS SDK for Java 2.x API参考资料》中的 ListObjectsV2

以下代码示例演示如何使用 ListParts

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

列出目录存储桶中分段上传的各个部分。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.ListPartsRequest; import software.amazon.awssdk.services.s3.model.ListPartsResponse; import software.amazon.awssdk.services.s3.model.Part; import software.amazon.awssdk.services.s3.model.S3Exception; import java.io.IOException; import java.nio.file.Path; import java.util.List; import static com.example.s3.util.S3DirectoryBucketUtils.abortDirectoryBucketMultipartUploads; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.multipartUploadForDirectoryBucket; /** * Lists the parts of a multipart upload for the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object being uploaded * @param uploadId The upload ID used to track the multipart upload * @return A list of Part representing the parts of the multipart upload */ public static List<Part> listDirectoryBucketMultipartUploadParts(S3Client s3Client, String bucketName, String objectKey, String uploadId) { logger.info("Listing parts for object: {} in bucket: {}", objectKey, bucketName); try { // Create a ListPartsRequest ListPartsRequest listPartsRequest = ListPartsRequest.builder() .bucket(bucketName) .uploadId(uploadId) .key(objectKey) .build(); // List the parts of the multipart upload ListPartsResponse response = s3Client.listParts(listPartsRequest); List<Part> parts = response.parts(); for (Part part : parts) { logger.info("Uploaded part: Part number = \"{}\", etag = {}", part.partNumber(), part.eTag()); } return parts; } catch (S3Exception e) { logger.error("Failed to list parts: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); return List.of(); // Return an empty list if an exception is thrown } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 ListParts” 中的。

以下代码示例演示如何使用 PutBucketEncryption

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

将存储桶加密设置为目录存储桶。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.kms.KmsClient; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.PutBucketEncryptionRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.ServerSideEncryption; import software.amazon.awssdk.services.s3.model.ServerSideEncryptionByDefault; import software.amazon.awssdk.services.s3.model.ServerSideEncryptionConfiguration; import software.amazon.awssdk.services.s3.model.ServerSideEncryptionRule; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createKmsClient; import static com.example.s3.util.S3DirectoryBucketUtils.createKmsKey; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.scheduleKeyDeletion; /** * Sets the default encryption configuration for an S3 bucket as SSE-KMS. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param kmsKeyId The ID of the customer-managed KMS key */ public static void putDirectoryBucketEncryption(S3Client s3Client, String bucketName, String kmsKeyId) { // Define the default encryption configuration to use SSE-KMS. For directory // buckets, AWS managed KMS keys aren't supported. Only customer-managed keys // are supported. ServerSideEncryptionByDefault encryptionByDefault = ServerSideEncryptionByDefault.builder() .sseAlgorithm(ServerSideEncryption.AWS_KMS) .kmsMasterKeyID(kmsKeyId) .build(); // Create a server-side encryption rule to apply the default encryption // configuration. For directory buckets, the bucketKeyEnabled field is enforced // to be true. ServerSideEncryptionRule rule = ServerSideEncryptionRule.builder() .bucketKeyEnabled(true) .applyServerSideEncryptionByDefault(encryptionByDefault) .build(); // Create the server-side encryption configuration for the bucket ServerSideEncryptionConfiguration encryptionConfiguration = ServerSideEncryptionConfiguration.builder() .rules(rule) .build(); // Create the PutBucketEncryption request PutBucketEncryptionRequest putRequest = PutBucketEncryptionRequest.builder() .bucket(bucketName) .serverSideEncryptionConfiguration(encryptionConfiguration) .build(); // Set the bucket encryption try { s3Client.putBucketEncryption(putRequest); logger.info("SSE-KMS Bucket encryption configuration set for the directory bucket: {}", bucketName); } catch (S3Exception e) { logger.error("Failed to set bucket encryption: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 PutBucketEncryption” 中的。

以下代码示例演示如何使用 PutBucketPolicy

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

将存储桶策略应用于目录存储桶。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.PutBucketPolicyRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getAwsAccountId; /** * Sets the following bucket policy for the specified S3 directory bucket. *<pre> * { * "Version": "2012-10-17", * "Statement": [ * { * "Sid": "AdminPolicy", * "Effect": "Allow", * "Principal": { * "AWS": "arn:aws:iam::<ACCOUNT_ID>:root" * }, * "Action": "s3express:*", * "Resource": "arn:aws:s3express:us-west-2:<ACCOUNT_ID>:bucket/<DIR_BUCKET_NAME> * } * ] * } * </pre> * This policy grants all S3 directory bucket actions to identities in the same account as the bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param policyText The policy text to be applied */ public static void putDirectoryBucketPolicy(S3Client s3Client, String bucketName, String policyText) { logger.info("Setting policy on bucket: {}", bucketName); logger.info("Policy: {}", policyText); try { PutBucketPolicyRequest policyReq = PutBucketPolicyRequest.builder() .bucket(bucketName) .policy(policyText) .build(); s3Client.putBucketPolicy(policyReq); logger.info("Bucket policy set successfully!"); } catch (S3Exception e) { logger.error("Failed to set bucket policy: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 PutBucketPolicy” 中的。

以下代码示例演示如何使用 PutObject

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

将对象放入目录存储桶。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.awscore.exception.AwsErrorDetails; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.PutObjectRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import java.io.UncheckedIOException; import java.nio.file.Path; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; /** * Puts an object into the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to be placed in the bucket * @param filePath The path of the file to be uploaded */ public static void putDirectoryBucketObject(S3Client s3Client, String bucketName, String objectKey, Path filePath) { logger.info("Putting object: {} into bucket: {}", objectKey, bucketName); try { // Create a PutObjectRequest PutObjectRequest putObj = PutObjectRequest.builder() .bucket(bucketName) .key(objectKey) .build(); // Upload the object s3Client.putObject(putObj, filePath); logger.info("Successfully placed {} into bucket {}", objectKey, bucketName); } catch (UncheckedIOException e) { throw S3Exception.builder().message("Failed to read the file: " + e.getMessage()).cause(e) .awsErrorDetails(AwsErrorDetails.builder() .errorCode("ClientSideException:FailedToReadFile") .errorMessage(e.getMessage()) .build()) .build(); } catch (S3Exception e) { logger.error("Failed to put object: {}", e.getMessage(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 PutObject” 中的。

以下代码示例演示如何使用 UploadPart

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

上传目录存储桶的分段上传的一部分。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.CompletedPart; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.UploadPartRequest; import software.amazon.awssdk.services.s3.model.UploadPartResponse; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import static com.example.s3.util.S3DirectoryBucketUtils.abortDirectoryBucketMultipartUploads; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; /** * This method creates part requests and uploads individual parts to S3. * While it uses the UploadPart API to upload a single part, it does so * sequentially to handle multiple parts of a file, returning all the completed * parts. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to be uploaded * @param uploadId The upload ID used to track the multipart upload * @param filePath The path to the file to be uploaded * @return A list of uploaded parts * @throws IOException if an I/O error occurs */ public static List<CompletedPart> multipartUploadForDirectoryBucket(S3Client s3Client, String bucketName, String objectKey, String uploadId, Path filePath) throws IOException { logger.info("Uploading parts for object: {} in bucket: {}", objectKey, bucketName); int partNumber = 1; List<CompletedPart> uploadedParts = new ArrayList<>(); ByteBuffer bb = ByteBuffer.allocate(1024 * 1024 * 5); // 5 MB byte buffer // Read the local file, break down into chunks and process try (RandomAccessFile file = new RandomAccessFile(filePath.toFile(), "r")) { long fileSize = file.length(); int position = 0; // Sequentially upload parts of the file while (position < fileSize) { file.seek(position); int read = file.getChannel().read(bb); bb.flip(); // Swap position and limit before reading from the buffer UploadPartRequest uploadPartRequest = UploadPartRequest.builder() .bucket(bucketName) .key(objectKey) .uploadId(uploadId) .partNumber(partNumber) .build(); UploadPartResponse partResponse = s3Client.uploadPart( uploadPartRequest, RequestBody.fromByteBuffer(bb)); // Build the uploaded part CompletedPart uploadedPart = CompletedPart.builder() .partNumber(partNumber) .eTag(partResponse.eTag()) .build(); // Add the uploaded part to the list uploadedParts.add(uploadedPart); // Log to indicate the part upload is done logger.info("Uploaded part number: {} with ETag: {}", partNumber, partResponse.eTag()); bb.clear(); position += read; partNumber++; } } catch (S3Exception e) { logger.error("Failed to list parts: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } return uploadedParts; }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 UploadPart” 中的。

以下代码示例演示如何使用 UploadPartCopy

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

根据源对象的大小创建复制分段,然后将各个段复制到目录存储桶。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CompletedPart; import software.amazon.awssdk.services.s3.model.HeadObjectRequest; import software.amazon.awssdk.services.s3.model.HeadObjectResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.UploadPartCopyRequest; import software.amazon.awssdk.services.s3.model.UploadPartCopyResponse; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import static com.example.s3.util.S3DirectoryBucketUtils.abortDirectoryBucketMultipartUploads; import static com.example.s3.util.S3DirectoryBucketUtils.completeDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.multipartUploadForDirectoryBucket; /** * Creates copy parts based on source object size and copies over individual * parts. * * @param s3Client The S3 client used to interact with S3 * @param sourceBucket The name of the source bucket * @param sourceKey The key (name) of the source object * @param destinationBucket The name of the destination bucket * @param destinationKey The key (name) of the destination object * @param uploadId The upload ID used to track the multipart upload * @return A list of completed parts */ public static List<CompletedPart> multipartUploadCopyForDirectoryBucket(S3Client s3Client, String sourceBucket, String sourceKey, String destinationBucket, String destinationKey, String uploadId) { // Get the object size to track the end of the copy operation HeadObjectRequest headObjectRequest = HeadObjectRequest.builder() .bucket(sourceBucket) .key(sourceKey) .build(); HeadObjectResponse headObjectResponse = s3Client.headObject(headObjectRequest); long objectSize = headObjectResponse.contentLength(); logger.info("Source Object size: {}", objectSize); // Copy the object using 20 MB parts long partSize = 20 * 1024 * 1024; // 20 MB long bytePosition = 0; int partNum = 1; List<CompletedPart> uploadedParts = new ArrayList<>(); while (bytePosition < objectSize) { long lastByte = Math.min(bytePosition + partSize - 1, objectSize - 1); logger.info("Part Number: {}, Byte Position: {}, Last Byte: {}", partNum, bytePosition, lastByte); try { UploadPartCopyRequest uploadPartCopyRequest = UploadPartCopyRequest.builder() .sourceBucket(sourceBucket) .sourceKey(sourceKey) .destinationBucket(destinationBucket) .destinationKey(destinationKey) .uploadId(uploadId) .copySourceRange("bytes=" + bytePosition + "-" + lastByte) .partNumber(partNum) .build(); UploadPartCopyResponse uploadPartCopyResponse = s3Client.uploadPartCopy(uploadPartCopyRequest); CompletedPart part = CompletedPart.builder() .partNumber(partNum) .eTag(uploadPartCopyResponse.copyPartResult().eTag()) .build(); uploadedParts.add(part); bytePosition += partSize; partNum++; } catch (S3Exception e) { logger.error("Failed to copy part number {}: {} - Error code: {}", partNum, e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } } return uploadedParts; }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 UploadPartCopy” 中的。

场景

以下代码示例演示如何为 S3 目录存储桶创建URL预签名存储桶并获取对象。

SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

生成GETURL用于访问 S3 目录存储桶中的对象的预签名。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.presigner.S3Presigner; import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest; import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest; import java.nio.file.Path; import java.time.Duration; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Presigner; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketObject; /** * Generates a presigned URL for accessing an object in the specified S3 * directory bucket. * * @param s3Presigner The S3 presigner client used to generate the presigned URL * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to access * @return A presigned URL for accessing the specified object */ public static String generatePresignedGetURLForDirectoryBucket(S3Presigner s3Presigner, String bucketName, String objectKey) { logger.info("Generating presigned URL for object: {} in bucket: {}", objectKey, bucketName); try { // Create a GetObjectRequest GetObjectRequest getObjectRequest = GetObjectRequest.builder() .bucket(bucketName) .key(objectKey) .build(); // Create a GetObjectPresignRequest GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder() .signatureDuration(Duration.ofMinutes(10)) // Presigned URL valid for 10 minutes .getObjectRequest(getObjectRequest) .build(); // Generate the presigned URL PresignedGetObjectRequest presignedGetObjectRequest = s3Presigner.presignGetObject(getObjectPresignRequest); // Get the presigned URL String presignedURL = presignedGetObjectRequest.url().toString(); logger.info("Presigned URL: {}", presignedURL); return presignedURL; } catch (S3Exception e) { logger.error("Failed to generate presigned URL: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 GetObject” 中的。