Beispiele für S3 Directory Buckets, die SDK für Java 2.x verwendet werden - AWS SDK for Java 2.x

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Beispiele für S3 Directory Buckets, die SDK für Java 2.x verwendet werden

Die folgenden Codebeispiele zeigen Ihnen, wie Sie mithilfe von S3 Directory Buckets Aktionen ausführen und allgemeine Szenarien implementieren. AWS SDK for Java 2.x

Aktionen sind Codeauszüge aus größeren Programmen und müssen im Kontext ausgeführt werden. Aktionen zeigen Ihnen zwar, wie Sie einzelne Servicefunktionen aufrufen, aber Sie können Aktionen in den zugehörigen Szenarien im Kontext sehen.

Szenarien sind Codebeispiele, die Ihnen zeigen, wie Sie bestimmte Aufgaben ausführen, indem Sie mehrere Funktionen innerhalb eines Dienstes oder in Kombination mit anderen aufrufen AWS-Services.

Jedes Beispiel enthält einen Link zum vollständigen Quellcode, in dem Sie Anweisungen zum Einrichten und Ausführen des Codes im Kontext finden.

Erste Schritte

Das folgende Codebeispiel zeigt, wie Sie mit der Verwendung von Amazon S3 S3-Verzeichnis-Buckets beginnen.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

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; } } }

Aktionen

Das folgende Codebeispiel zeigt die VerwendungAbortMultipartUpload.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Brechen Sie einen mehrteiligen Upload in einem Verzeichnis-Bucket ab.

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; } }

Das folgende Codebeispiel zeigt die VerwendungCompleteMultipartUpload.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Schließt einen mehrteiligen Upload in einem Verzeichnis-Bucket ab.

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; } }

Das folgende Codebeispiel zeigt die VerwendungCopyObject.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Kopiert ein Objekt aus einem Verzeichnis-Bucket in einen Verzeichnis-Bucket.

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; } }
  • APIEinzelheiten finden Sie CopyObjectunter AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungCreateBucket.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Erstellen Sie einen S3-Verzeichnis-Bucket.

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; } }
  • APIEinzelheiten finden Sie CreateBucketunter AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungCreateMultipartUpload.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Erstellen Sie einen mehrteiligen Upload in einem Verzeichnis-Bucket.

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; } }

Das folgende Codebeispiel zeigt die VerwendungDeleteBucket.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Löschen Sie einen S3-Verzeichnis-Bucket.

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; } }
  • APIEinzelheiten finden Sie DeleteBucketunter AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungDeleteBucketEncryption.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Löschen Sie die Verschlüsselungskonfiguration für einen Verzeichnis-Bucket.

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; } }

Das folgende Codebeispiel zeigt die VerwendungDeleteBucketPolicy.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Löschen Sie eine Bucket-Richtlinie für einen Verzeichnis-Bucket.

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; } }

Das folgende Codebeispiel zeigt die VerwendungDeleteObject.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Löscht ein Objekt in einem Verzeichnis-Bucket.

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; } }
  • APIEinzelheiten finden Sie DeleteObjectunter AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungDeleteObjects.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Löschen Sie mehrere Objekte in einem Verzeichnis-Bucket.

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; } }
  • APIEinzelheiten finden Sie DeleteObjectsunter AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungGetBucketEncryption.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Ruft die Verschlüsselungskonfiguration eines Verzeichnis-Buckets ab.

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; } }

Das folgende Codebeispiel zeigt die VerwendungGetBucketPolicy.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Ruft die Richtlinie eines Verzeichnis-Buckets ab.

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; } }
  • APIEinzelheiten finden Sie GetBucketPolicyunter AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungGetObject.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Ruft ein Objekt aus einem Verzeichnis-Bucket ab.

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; } }
  • APIEinzelheiten finden Sie GetObjectunter AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungGetObjectAttributes.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Ruft ein Objektattribut aus einem Verzeichnis-Bucket ab.

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; } }

Das folgende Codebeispiel zeigt die VerwendungHeadBucket.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Überprüft, ob der angegebene S3-Verzeichnis-Bucket existiert und zugänglich ist.

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; } }
  • APIEinzelheiten finden Sie HeadBucketunter AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungHeadObject.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Ruft Metadaten eines Objekts in einem Verzeichnis-Bucket ab.

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; } }
  • APIEinzelheiten finden Sie HeadObjectunter AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungListDirectoryBuckets.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Listet alle Verzeichnis-Buckets auf.

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; } }

Das folgende Codebeispiel zeigt die VerwendungListMultipartUploads.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Listet mehrteilige Uploads in einem Verzeichnis-Bucket auf.

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 } }

Das folgende Codebeispiel zeigt die VerwendungListObjectsV2.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Listet Objekte in einem Verzeichnis-Bucket auf.

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; } }
  • APIEinzelheiten finden Sie unter ListObjectsV2 in der AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungListParts.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Listet Teile eines mehrteiligen Uploads in einem Verzeichnis-Bucket auf.

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 } }

Das folgende Codebeispiel zeigt die VerwendungPutBucketEncryption.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Stellen Sie die Bucket-Verschlüsselung auf einen Verzeichnis-Bucket ein.

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; } }

Das folgende Codebeispiel zeigt die VerwendungPutBucketPolicy.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Wenden Sie eine Bucket-Richtlinie auf einen Verzeichnis-Bucket an.

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; } }
  • APIEinzelheiten finden Sie PutBucketPolicyunter AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungPutObject.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Fügt ein Objekt in einen Verzeichnis-Bucket ein.

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; } }
  • APIEinzelheiten finden Sie PutObjectunter AWS SDK for Java 2.x APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungUploadPart.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Laden Sie einen Teil eines mehrteiligen Uploads für einen Verzeichnis-Bucket hoch.

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; }

Das folgende Codebeispiel zeigt die VerwendungUploadPartCopy.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Erstellen Sie Teile, die auf der Größe des Quellobjekts basieren, und kopieren Sie einzelne Teile in einen Verzeichnis-Bucket.

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; }
  • APIEinzelheiten finden Sie UploadPartCopyunter AWS SDK for Java 2.x APIReferenz.

Szenarien

Das folgende Codebeispiel zeigt, wie Sie einen URL für S3 vorsignierten Verzeichnis-Buckets erstellen und ein Objekt abrufen.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Generieren Sie eine Vorsignierung GET URL für den Zugriff auf ein Objekt in einem S3-Verzeichnis-Bucket.

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; } }