本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
建立、列出及刪除Amazon S3值區
Amazon S3 上的每個物件 (檔案) 都必須位在儲存貯體中。儲存貯體代表物件集合 (容器)。每個儲存貯體都必須具有獨一無二的索引鍵 (名稱)。如需值區及其組態的詳細資訊,請參閱使用指南中的Amazon Simple Storage Service使用Amazon S3值區。
注意
最佳實務
建議您在Amazon S3值區上啟用AbortIncompleteMultipartUpload生命週期規則。
此規則指示 Amazon S3 中止啟動後未在指定天數內完成的分段上傳。超過設定的時間限制時,Amazon S3 會中止上傳,然後刪除未完成的上傳資料。
如需詳細資訊,請參閱Amazon Simple Storage Service使用指南中的具有版本控制的值區的生命週期組態。
注意
這些程式碼片段假設您瞭解基本資料,並使用中的資訊設定了預設認AWS證SDK 的單一登入存取設定。
建立 儲存貯體
建立CreateBucketRequestcreateBucket
方法。使用 S3Client
來執行其他操作,例如列出或刪除儲存貯體,如稍後範例所示。
匯入
import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.HeadBucketRequest; import software.amazon.awssdk.services.s3.model.HeadBucketResponse; import software.amazon.awssdk.services.s3.model.ListBucketsRequest; import software.amazon.awssdk.services.s3.model.ListBucketsResponse; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; import software.amazon.awssdk.services.s3.waiters.S3Waiter;
Code
首先創建一個 S3 客戶端。
Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build();
進行建立儲存貯體的請求。
import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.HeadBucketRequest; import software.amazon.awssdk.services.s3.model.HeadBucketResponse; import software.amazon.awssdk.services.s3.model.ListBucketsRequest; import software.amazon.awssdk.services.s3.model.ListBucketsResponse; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; import software.amazon.awssdk.services.s3.waiters.S3Waiter; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class S3BucketOps { public static void main(String[] args) { Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build(); String bucket = "bucket" + System.currentTimeMillis(); System.out.println(bucket); createBucket(s3, bucket); performOperations(s3, bucket); } // Create a bucket by using a S3Waiter object public static void createBucket(S3Client s3Client, String bucketName) { try { S3Waiter s3Waiter = s3Client.waiter(); CreateBucketRequest bucketRequest = CreateBucketRequest.builder() .bucket(bucketName) .build(); s3Client.createBucket(bucketRequest); HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder() .bucket(bucketName) .build(); // Wait until the bucket is created and print out the response. WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait); waiterResponse.matched().response().ifPresent(System.out::println); System.out.println(bucketName + " is ready"); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
請參閱(詳見)的完整實例
列出儲存貯體
建立 ListBucketsRequestlistBuckets
方法來檢索存儲桶的列表。如果請求成功,ListBucketsResponse
匯入
import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.HeadBucketRequest; import software.amazon.awssdk.services.s3.model.HeadBucketResponse; import software.amazon.awssdk.services.s3.model.ListBucketsRequest; import software.amazon.awssdk.services.s3.model.ListBucketsResponse; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; import software.amazon.awssdk.services.s3.waiters.S3Waiter;
Code
首先創建一個 S3 客戶端。
Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build();
進行列出儲存貯體的請求。
// List buckets ListBucketsRequest listBucketsRequest = ListBucketsRequest.builder().build(); ListBucketsResponse listBucketsResponse = s3.listBuckets(listBucketsRequest); listBucketsResponse.buckets().stream().forEach(x -> System.out.println(x.name()));
請參閱(詳見)的完整實例
刪除 儲存貯體
刪除 Amazon S3 儲存貯體之前,您必須先確保儲存貯體是空的,否則服務會傳回錯誤。如果您有具版本控制的儲存貯體,則必須一併刪除該儲存貯體中的任何版本控制物件。
刪除值區中的物件
建立 ListObjectsV2RequestlistObjects
方法擷取值區中的物件清單。然後使用每個物件的 deleteObject
方法將其刪除。
匯入
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.DeleteObjectRequest; 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;
Code
首先創建一個 S3 客戶端。
ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create(); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .credentialsProvider(credentialsProvider) .build();
刪除儲存貯體中的所有物件。
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.DeleteObjectRequest; 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; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class S3BucketDeletion { public static void main(String[] args) throws Exception { final String usage = """ Usage: <bucket> Where: bucket - The bucket to delete (for example, bucket1).\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String bucket = args[0]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build(); deleteObjectsInBucket(s3, bucket); s3.close(); } public static void deleteObjectsInBucket(S3Client s3, String bucket) { try { // To delete a bucket, all the objects in the bucket must be deleted first. ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder() .bucket(bucket) .build(); ListObjectsV2Response listObjectsV2Response; do { listObjectsV2Response = s3.listObjectsV2(listObjectsV2Request); for (S3Object s3Object : listObjectsV2Response.contents()) { DeleteObjectRequest request = DeleteObjectRequest.builder() .bucket(bucket) .key(s3Object.key()) .build(); s3.deleteObject(request); } } while (listObjectsV2Response.isTruncated()); DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build(); s3.deleteBucket(deleteBucketRequest); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
請參閱(詳見)的完整實例
刪除空的儲存貯體
DeleteBucketRequestdeleteBucket
方法。
匯入
import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.HeadBucketRequest; import software.amazon.awssdk.services.s3.model.HeadBucketResponse; import software.amazon.awssdk.services.s3.model.ListBucketsRequest; import software.amazon.awssdk.services.s3.model.ListBucketsResponse; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; import software.amazon.awssdk.services.s3.waiters.S3Waiter;
Code
首先創建一個 S3 客戶端。
Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build();
刪除儲存貯體。
DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder() .bucket(bucket) .build(); s3.deleteBucket(deleteBucketRequest); s3.close();
請參閱(詳見)的完整實例