LoginSignup
0
0

More than 3 years have passed since last update.

[Cloudian #7] Python(boto3)で、バケットを削除してみる

Last updated at Posted at 2020-12-18

はじめに

Cloudianは、AWSのS3完全互換のAPIを持ったオブジェクトストレージです。
前回は、Python(boto3)で、オブジェクトを削除してみました。

今回は、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)で、バケットを削除してみました。

次回も、Pythonでオブジェクトストレージ/Cloudianをいろいろ操作していきたいと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0