さくらのクラウド オブジェクトストレージ
Python の中でオブジェクトストレージを利用
ここまでで、AWS s3 コマンドを使ってオブジェクトストレージが利用できるようにしました。
引き続き、Python の中でオブジェクトストレージが利用できるようにします。
Windows11 WSL2 Ubuntu Python Miniconda 仮想環境の中に
boto3 をインストールして実現します。
boto3 は AWS 公式ライブラリです。
boto3 は AWS のオブジェクトストレージに接続できます。
さくらのオブジェクトストレージは AWS s3 に準拠しています。
よって、boto3 はさくらのオブジェクトストレージに接続することができます。
できるはずです。
boto3 インストール準備
Windows11 WSL2 Ubuntu Python Miniconda 仮想環境の構築方法については
Windows11 WSL2 Ubuntu Python--
WSL2 Ubuntu Python 概要
を参照してください。
AWS CLI は入れておく必要があります。
AWS CLI を ubuntu にインストールします。
Windows11 WSL2 Ubuntu 24.04 LTS を起動します。
Ubuntu をアップデートしておきましょう。
yamada@yama:~$ sudo apt update
yamada@yama:~$ sudo apt upgrade
yamada@yama:~$ sudo apt autoremove
yamada@yama:~$ sudo shutdown -r now
AWS CLI をインストールするための推奨の方法は、インストールスクリプトです。
Linux 版を利用します。
これが簡単なようです。
yamada@yama:~$ curl -fsSL https://awscli.amazonaws.com/v2/install.sh | bash
詳細は、
AWS CLI を利用する
を参照してください。
各初期設定もしておきましょう。
この設定は boto3 と共有されています。
Miniconda もアップデートしておきます。
有効環境の Conda 全パッケージアップデート。
yamada@yama:~$ conda update --all
conda本体のみのアップデート。
yamada@yama:~$ conda update -n base -c defaults conda
boto3 インストール
今回は、Windows11 WSL2 Ubuntu Python Miniconda 仮想環境の中に boto3 をインストールします。
Miniconda に対応した boto3 のインストールファイルは conda-forge にあります。
conda-forge とは、世界中の有志(コミュニティ)が管理・提供している、
信頼性の高い conda パッケージの公開チャンネル(保存場所)のことです。
インストール時に -c conda-forge というオプションを指定して利用します。
-c なしの時は::を入れます。
yamada@yama:~$ conda install conda-forge::boto3
又は
yamada@yama:~$ conda install -c conda-forge boto3
[結果]
yamada@yama:~$ conda install conda-forge::boto3
Collecting package metadata (current_repodata.json):
- WARNING conda.models.version:get_matcher(556):
Using .* with relational operator is superfluous and deprecated
and will be removed in a future version of conda.
Your spec was 1.7.1.*, but conda is ignoring the .* and treating it as 1.7.1
done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 23.9.0
latest version: 26.7.2
Please update conda by running
$ conda update -n base -c defaults conda
Or to minimize the number of packages updated during conda update use
conda install conda=26.7.2
## Package Plan ##
environment location: /home/yamada/miniconda3
added / updated specs:
- conda-forge::boto3
The following packages will be SUPERSEDED by a higher-priority channel:
boto3 pkgs/main/linux-64::boto3-1.43.24-py3~ --> conda-forge/noarch::boto3-1.43.23-pyhd8ed1ab_0
Proceed ([y]/n)? y
Downloading and Extracting Packages:
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
boto3 のインストールが完了しました。
boto3 の利用
boto3 を利用してみます。
さくらのクラウド オブジェクトストレージにアクセスする
python ファイル boto3-1.py を作成します。
s3 オブジェクトを作成します。
# coding: utf-8
import boto3
s3 = boto3.resource('s3')
# boto3.resource()
# 高レイヤーサービスへアクセス
# s3 オブジェクトが返ってきます。
補足
boto3を使用する際、boto3.client( ) と boto3.resource( ) があります。
同じことをするにも使い方が違います。
困ります。
boto3.client( ) は辞書型データが返ってくる
boto3.resource( ) は s3 オブジェクトが返ってくる
ようです。
私は、boto3.resource( ) を使っています。
バケット一覧取得
バケット一覧を取得します。
s3.buckets.all()
でバケット一覧オブジェクトを取得して、その中身を表示します。
for bucket in s3.buckets.all():
print(bucket)
print(f"バケット名: {bucket.name}")
実行すると
s3.Bucket(name='76bucket')
バケット名: 76bucket
のようになります。
さらには、バケット名と作成日を表示させます。
bucket_list = list(s3.buckets.all())
for bucket in bucket_list:
print(f"Bucket name: {bucket.name}, created on: {bucket.creation_date}")
実行すると
Bucket name: 76bucket, created on: 2026-05-27 09:04:57.614000+00:00
のようになります。
オブジェクトストレージにファイルをアップ
書式は
s3.Bucket(bucket_name).upload_file(upload_file, obj_name)
です。
/home/yamada/public_html/images/pyplot-g.png
をバケット 76bucket に pyplot-g.png という名前でアップロードします。
pyplot-g.png は自分で勝手に作成した適当なファイルです。
s3.Bucket('76bucket').upload_file("./public_html/images/pyplot-g.png", "pyplot-g.png")
print("アップロードが完了しました...")
/home/yamada/public_html/images/pyplot-g.png
をバケット 76bucket のサブフォルダ folder-1 に pyplot-g1.png という名前でアップロードするならば
s3.Bucket('76bucket').upload_file('./public_html/images/pyplot-g.png', 'folder-1/pyplot-g1.png')
print("サブフォルダへのアップロードが完了しました。")
とします。
オブジェクトストレージからファイルをダウンロード
書式は
s3.Bucket(bucket_name).download_file(object_key, local_file_path)
です。
オブジェクトストレージバケット 76bucket の folder-1 にある pyplot-g.png を
/home/yamada/public_html/images/
にファイル名 pyplot-g1.png としてダウンロードします。
s3.Bucket('76bucket').download_file('folder-1/pyplot-g.png', './public_html/images/pyplot-g1.png')
print("ダウンロードが完了しました。")
バケット内のフォルダ一覧取得
s3.Bucket('76bucket').objects.filter(Prefix='folder-1')
でバケット 76bucket 内の フォルダ folder-1 の中にあるファイル名の一覧のオブジェクトを取得できます。
フォルダ一覧を表示します。
for obj in s3.Bucket('76bucket').objects.filter(Prefix='folder-1'):
print(obj.key)
ここまで boto3 の利用方法を個別に説明してきました。
引き続き、これらを一つのファイルにまとめて実行していきます。





