将 DeleteBucket
与 Amazon SDK 或命令行工具结合使用
以下代码示例演示如何使用 DeleteBucket
。
操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:
- .NET
-
- Amazon SDK for .NET
-
注意
在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 /// <summary> /// Shows how to delete an Amazon S3 bucket. /// </summary> /// <param name="client">An initialized Amazon S3 client object.</param> /// <param name="bucketName">The name of the Amazon S3 bucket to delete.</param> /// <returns>A boolean value that represents the success or failure of /// the delete operation.</returns> public static async Task<bool> DeleteBucketAsync(IAmazonS3 client, string bucketName) { var request = new DeleteBucketRequest { BucketName = bucketName, }; var response = await client.DeleteBucketAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
-
有关 API 详细信息,请参阅 Amazon SDK for .NET API 参考中的 DeleteBucket。
-
- Bash
-
- Amazon CLI 及 Bash 脚本
-
注意
在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 ############################################################################### # function errecho # # This function outputs everything sent to it to STDERR (standard error output). ############################################################################### function errecho() { printf "%s\n" "$*" 1>&2 } ############################################################################### # function delete_bucket # # This function deletes the specified bucket. # # Parameters: # $1 - The name of the bucket. # Returns: # 0 - If successful. # 1 - If it fails. ############################################################################### function delete_bucket() { local bucket_name=$1 local response response=$(aws s3api delete-bucket \ --bucket "$bucket_name") # shellcheck disable=SC2181 if [[ $? -ne 0 ]]; then errecho "ERROR: AWS reports s3api delete-bucket failed.\n$response" return 1 fi }
-
有关 API 详细信息,请参阅《Amazon CLI 命令参考》中的 DeleteBucket。
-
- C++
-
- 适用于 C++ 的 SDK
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 bool AwsDoc::S3::DeleteBucket(const Aws::String &bucketName, const Aws::Client::ClientConfiguration &clientConfig) { Aws::S3::S3Client client(clientConfig); Aws::S3::Model::DeleteBucketRequest request; request.SetBucket(bucketName); Aws::S3::Model::DeleteBucketOutcome outcome = client.DeleteBucket(request); if (!outcome.IsSuccess()) { const Aws::S3::S3Error &err = outcome.GetError(); std::cerr << "Error: DeleteBucket: " << err.GetExceptionName() << ": " << err.GetMessage() << std::endl; } else { std::cout << "The bucket was deleted" << std::endl; } return outcome.IsSuccess(); }
-
有关 API 详细信息,请参阅 Amazon SDK for C++ API 参考中的 DeleteBucket。
-
- CLI
-
- Amazon CLI
-
以下命令删除名为
my-bucket
的存储桶:aws s3api delete-bucket --bucket my-bucket --region us-east-1
-
有关 API 详细信息,请参阅《Amazon CLI 命令参考》中的 DeleteBucket
。
-
- Go
-
- 适用于 Go V2 的 SDK
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 // BucketBasics encapsulates the Amazon Simple Storage Service (Amazon S3) actions // used in the examples. // It contains S3Client, an Amazon S3 service client that is used to perform bucket // and object actions. type BucketBasics struct { S3Client *s3.Client } // DeleteBucket deletes a bucket. The bucket must be empty or an error is returned. func (basics BucketBasics) DeleteBucket(bucketName string) error { _, err := basics.S3Client.DeleteBucket(context.TODO(), &s3.DeleteBucketInput{ Bucket: aws.String(bucketName)}) if err != nil { log.Printf("Couldn't delete bucket %v. Here's why: %v\n", bucketName, err) } return err }
-
有关 API 详细信息,请参阅 Amazon SDK for Go API 参考中的 DeleteBucket
。
-
- Java
-
- SDK for Java 2.x
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder() .bucket(bucket) .build(); s3.deleteBucket(deleteBucketRequest); s3.close();
-
有关 API 详细信息,请参阅 Amazon SDK for Java 2.x API 参考中的 DeleteBucket。
-
- JavaScript
-
- SDK for JavaScript (v3)
-
注意
在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 删除存储桶。
import { DeleteBucketCommand, S3Client } from "@aws-sdk/client-s3"; const client = new S3Client({}); // Delete a bucket. export const main = async () => { const command = new DeleteBucketCommand({ Bucket: "test-bucket", }); try { const response = await client.send(command); console.log(response); } catch (err) { console.error(err); } };
-
有关更多信息,请参阅 Amazon SDK for JavaScript 开发人员指南。
-
有关 API 详细信息,请参阅《Amazon SDK for JavaScript API 参考》中的 DeleteBucket。
-
- PHP
-
- 适用于 PHP 的 SDK
-
注意
在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 删除空桶。
$s3client = new Aws\S3\S3Client(['region' => 'us-west-2']); try { $this->s3client->deleteBucket([ 'Bucket' => $this->bucketName, ]); echo "Deleted bucket $this->bucketName.\n"; } catch (Exception $exception) { echo "Failed to delete $this->bucketName with error: " . $exception->getMessage(); exit("Please fix error with bucket deletion before continuing."); }
-
有关 API 的详细信息,请参阅 Amazon SDK for PHP API 参考中的 DeleteBucket。
-
- PowerShell
-
- 适用于 PowerShell 的工具
-
示例 1:此命令从存储桶“test-files”中移除所有对象和对象版本,然后删除该存储桶。在继续操作之前,该命令将提示进行确认。添加 -Force 开关可禁止确认。请注意,不能删除不为空的存储桶。
Remove-S3Bucket -BucketName test-files -DeleteBucketContent
-
有关 API 详细信息,请参阅《Amazon Tools for PowerShell Cmdlet 参考》中的 DeleteBucket。
-
- Python
-
- SDK for Python(Boto3)
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 class BucketWrapper: """Encapsulates S3 bucket actions.""" def __init__(self, bucket): """ :param bucket: A Boto3 Bucket resource. This is a high-level resource in Boto3 that wraps bucket actions in a class-like structure. """ self.bucket = bucket self.name = bucket.name def delete(self): """ Delete the bucket. The bucket must be empty or an error is raised. """ try: self.bucket.delete() self.bucket.wait_until_not_exists() logger.info("Bucket %s successfully deleted.", self.bucket.name) except ClientError: logger.exception("Couldn't delete bucket %s.", self.bucket.name) raise
-
有关 API 详细信息,请参阅《Amazon SDK for Python (Boto3) API 参考》中的 DeleteBucket。
-
- Ruby
-
- 适用于 Ruby 的 SDK
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 # Deletes the objects in an Amazon S3 bucket and deletes the bucket. # # @param bucket [Aws::S3::Bucket] The bucket to empty and delete. def delete_bucket(bucket) puts("\nDo you want to delete all of the objects as well as the bucket (y/n)? ") answer = gets.chomp.downcase if answer == "y" bucket.objects.batch_delete! bucket.delete puts("Emptied and deleted bucket #{bucket.name}.\n") end rescue Aws::Errors::ServiceError => e puts("Couldn't empty and delete bucket #{bucket.name}.") puts("\t#{e.code}: #{e.message}") raise end
-
有关 API 详细信息,请参阅 Amazon SDK for Ruby API 参考中的 DeleteBucket。
-
- Rust
-
- 适用于 Rust 的 SDK
-
注意
在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 pub async fn delete_bucket(client: &Client, bucket_name: &str) -> Result<(), Error> { client.delete_bucket().bucket(bucket_name).send().await?; println!("Bucket deleted"); Ok(()) }
-
有关 API 详细信息,请参阅《Amazon SDK for Rust API 参考》中的 DeleteBucket
。
-
- SAP ABAP
-
- SDK for SAP ABAP
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 TRY. lo_s3->deletebucket( iv_bucket = iv_bucket_name ). MESSAGE 'Deleted S3 bucket.' TYPE 'I'. CATCH /aws1/cx_s3_nosuchbucket. MESSAGE 'Bucket does not exist.' TYPE 'E'. ENDTRY.
-
有关 API 详细信息,请参阅《Amazon SDK for SAP ABAP API 参考》中的 DeleteBucket。
-
- Swift
-
- SDK for Swift
-
注意
这是预览版 SDK 的预发布文档。本文档随时可能更改。
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 public func deleteBucket(name: String) async throws { let input = DeleteBucketInput( bucket: name ) _ = try await client.deleteBucket(input: input) }
-
有关 API 详细信息,请参阅《Amazon SDK for Swift API 参考》中的 DeleteBucket
。
-
有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。