java aws s3_创建、列出和删除 Amazon S3 存储桶 - 适用于 Java 的 AWS 开发工具包
时间: 2024-03-18 12:38:49 浏览: 61
好的,针对您的问题,以下是适用于Java的AWS开发工具包中创建、列出和删除Amazon S3存储桶的示例代码:
1. 创建S3存储桶:
```
public static void createBucket(String bucketName) {
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
if (!s3.doesBucketExistV2(bucketName)) {
try {
s3.createBucket(bucketName);
System.out.println("Bucket created successfully");
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}
} else {
System.out.println("Bucket already exists");
}
}
```
2. 列出S3存储桶:
```
public static void listBuckets() {
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
List<Bucket> buckets = s3.listBuckets();
for (Bucket bucket : buckets) {
System.out.println(bucket.getName());
}
}
```
3. 删除S3存储桶:
```
public static void deleteBucket(String bucketName) {
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
if (s3.doesBucketExistV2(bucketName)) {
try {
s3.deleteBucket(bucketName);
System.out.println("Bucket deleted successfully");
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}
} else {
System.out.println("Bucket does not exist");
}
}
```
以上代码仅作为示例,具体实现可能会因环境和要求而异。希望能对您有所帮助。
阅读全文