LoginSignup
21
12

More than 5 years have passed since last update.

python boto3でS3操作

Last updated at Posted at 2016-09-18

インストール

pipか何かでboto3をインストール. 自分はcondaを使ってインストール.

$ conda install -c anaconda boto3=1.3.1

aws cliを使って、クレデンシャル情報とデフォルトの地域を登録

$ aws configure
aws_access_key_id = ACCESS_KEY_ID
aws_secret_access_key = SECRET_ACCESS_KEY
region = ap-northeast-1
output = json

なお、直接, 下記のようにファイルに追記してもok

$ vim ~/.aws/credentials
[default]
aws_access_key_id = ACCESS_KEY_ID
aws_secret_access_key = SECRET_ACCESS_KEY
$ vim ~/.aws/config:
[default]
region = ap-norteast-1
output = json

バケットリストの取得

import boto3

s3 = boto3.resource('s3')

for bucket in s3.buckets.all():
    print(bucket.name)

アップロード

import boto3

s3 = boto3.resource('s3')

data = open('test.jpg', 'rb')
s3.Bucket('my-bucket').put_object(Key='test.jpg', Body=data)

下記、リファレンス
http://boto3.readthedocs.io/en/latest/index.html

21
12
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
21
12