15
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

`aws login`の認証情報をboto3で使うにはひと工夫必要だよ!

Posted at

新しいaws loginが激アツですよね?

「SDKでも使えるよ!」とあったのですが、Boto3だとちょいと一工夫必要でしたので共有します。

とりあえずやってみよう

前提として、最新のAWS CLIをインストールし、aws loginでログインしておきます。

uvでプロジェクトを作ります。

Shell
uv init app
cd app

Boto3をインストールします。

Shell
uv add boto3

インストールしたライブラリーはこんな感じです。

uv tree
app v0.1.0
└── boto3 v1.41.2
    ├── botocore v1.41.2
    │   ├── jmespath v1.0.1
    │   ├── python-dateutil v2.9.0.post0
    │   │   └── six v1.17.0
    │   └── urllib3 v2.5.0
    ├── jmespath v1.0.1
    └── s3transfer v0.15.0
        └── botocore v1.41.2 (*)
(*) Package tree already displayed

S3のバケット一覧を取得するPythonスクリプトを作ります。

main.py
import boto3

client = boto3.client("s3")
result = client.list_buckets()

print(result)

実行してみましょう。

Shell
uv run main.py

エラーになります🤔

Traceback (most recent call last):
  File "/workspaces/fantastic-winner/app/main.py", line 3, in <module>
    client = boto3.client("s3")
  File "/workspaces/fantastic-winner/app/.venv/lib/python3.13/site-packages/boto3/__init__.py", line 93, in client
    return _get_default_session().client(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/workspaces/fantastic-winner/app/.venv/lib/python3.13/site-packages/boto3/session.py", line 337, in client
    return self._session.create_client(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        service_name, **create_client_kwargs
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/workspaces/fantastic-winner/app/.venv/lib/python3.13/site-packages/botocore/context.py", line 123, in wrapper
    return func(*args, **kwargs)
  File "/workspaces/fantastic-winner/app/.venv/lib/python3.13/site-packages/botocore/session.py", line 986, in create_client
    credentials = self.get_credentials()
  File "/workspaces/fantastic-winner/app/.venv/lib/python3.13/site-packages/botocore/session.py", line 522, in get_credentials
    ).load_credentials()
      ~~~~~~~~~~~~~~~~^^
  File "/workspaces/fantastic-winner/app/.venv/lib/python3.13/site-packages/botocore/credentials.py", line 2239, in load_credentials
    creds = provider.load()
  File "/workspaces/fantastic-winner/app/.venv/lib/python3.13/site-packages/botocore/credentials.py", line 2749, in load
    raise MissingDependencyException(
    ...<5 lines>...
    )
botocore.exceptions.MissingDependencyException: Missing Dependency: Using the login credential provider requires an additional dependency. You will need to pip install "botocore[crt]" before proceeding.

解決策

エラーメッセージに親切に書かれているのですが、aws loginの認証情報を使う場合は、追加でbotocore[crt]も必要です。

Shell
uv add botocore[crt]
uv tree
app v0.1.0
├── boto3 v1.41.2
│   ├── botocore v1.41.2
│   │   ├── jmespath v1.0.1
│   │   ├── python-dateutil v2.9.0.post0
│   │   │   └── six v1.17.0
│   │   ├── urllib3 v2.5.0
│   │   └── awscrt v0.28.4 (extra: crt)
│   ├── jmespath v1.0.1
│   └── s3transfer v0.15.0
│       └── botocore v1.41.2 (*)
└── botocore[crt] v1.41.2 (*)
(*) Package tree already displayed

これで動作するようになります。

ちなみに、ドキュメントにも記載がありました。ちゃんと読もうって話ですね!

image.png

15
4
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
15
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?