LoginSignup
136
96

More than 5 years have passed since last update.

boto3 で デフォルトprofile以外を使う

Posted at

デフォルトの場合

import boto3

s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
    print(bucket.name)

ACCESS_KEY, SECRET_KEYを指定

ドキュメント

import boto3
from boto3.session import Session

session = Session(aws_access_key_id='<YOUR ACCESS KEY ID>',
                  aws_secret_access_key='<YOUR SECRET KEY>',
                  region_name='<REGION NAME>')

s3 = session.resource('s3')

~/.aws/credentials の profile名で指定する

Session に profile_name を渡す。

ドキュメント 見ても分からず、githubのissue で発見

import boto3
from boto3.session import Session

profile = '<YOUR_PROFILE_NAME>'
session = Session(profile_name=profile)

s3 = session.resource('s3')
136
96
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
136
96