Chapter 08

バケットを削除してみる / delete_bucket()

Akane
Akane
2021.02.21に更新

Cloudianは、AWSのS3完全互換のAPIを持ったオブジェクトストレージです。

今回は、Python(boto3)で、オブジェクトストレージのバケットを削除してみようと思います。

バケットの削除 / delete_bucket()

Cloudianに作成されているバケットを削除します。

以下の例では、バケット「pythonbucket1」を削除しています。

test.py
import boto3

client = boto3.client(
    's3',
    endpoint_url='https://xxx.yyy.com'
)


# バケット名:pythonbucket1 の削除
client.delete_bucket(
    Bucket='pythonbucket1'
)
{'ResponseMetadata': {'RequestId': '9dad3274-0e30-1dbc-a754-06bdfcde1d5e',
  'HostId': '',
  'HTTPStatusCode': 204,
  'HTTPHeaders': {'date': 'Sun, 13 Dec 2020 20:12:33 GMT',
   'x-amz-request-id': '9dad3274-0e30-1dbc-a754-06bdfcde1d5e',
   'server': 'CloudianS3'},
  'RetryAttempts': 0}}

※注意
バケットを削除するには、バケット内にオブジェクトが存在しない状態である必要があります。
バケット内にオブジェクトが存在する状態で delete_bucket()を実行すると、以下のような BucketNotEmpty 例外が発生します。

client.delete_bucket(
    Bucket='bucket1'
)
ClientError                               Traceback (most recent call last)
<ipython-input-3-0bb1849245e5> in <module>
      1 client.delete_bucket(
----> 2     Bucket='bucket1'
      3 )

~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages/botocore/> client.py in _api_call(self, *args, **kwargs)
    314                     "%s() only accepts keyword arguments." % py_operation_name)
    315             # The "self" in this scope is referring to the BaseClient.
--> 316             return self._make_api_call(operation_name, kwargs)
    317 
    318         _api_call.__name__ = str(py_operation_name)

~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
    624             error_code = parsed_response.get("Error", {}).get("Code")
    625             error_class = self.exceptions.from_code(error_code)

--> 626 raise error_class(parsed_response, operation_name)

627         else:
628             return parsed_response

ClientError: An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty

まとめ

Python(boto3)で、バケットを削除してみました。