0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MinIo(S3互換)をpythonから操作する

Posted at

目的

今作成しているサービスが、現在はオンプレで構築しているが、いずれクラウドへ移行するかもしれないので、なるべくクラウドにのっけた時に作業が少ないソリューションを選びたい。そこで、今回はlocalstackではなくMinIoでオブジェクトストレージを構築する。

導入

バイナリ

wget https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x minio
./minio server /data

Docker

docker-compose.yml
version: '3.9'

services:
  minio:
    image: quay.io/minio/minio:latest
    container_name: s3-minio
    environment:
      MINIO_ROOT_USER: admin123
      MINIO_ROOT_PASSWORD: admin123
    command: server --console-address ":9090" /data
    volumes:
      - ./minio/data:/data
    ports:
      - 9000:9000
      - 9090:9090

アクセス

https://IP:9090にアクセスする事によってログイン画面が立ち上がる
パスワードとユーザーはDocker-compose.ymlで指定したものを使う
image.png

python側

pipのアップグレード

$ pip3 install --upgrade pip

ライブラリのインストール

pip3 install boto3
pip3 install botocore

バケット作成

import boto3

s3_resource = boto3.resource(
    's3',
    endpoint_url='http://localhost:9000',
    aws_access_key_id='admin123',
    aws_secret_access_key='admin123'
)

bucket_name = "lesson"

s3_resource.create_bucket(Bucket=bucket_name)

バケット、ディレクトリ作成

import botocore
import boto3

# minioをendpointにする
s3 = boto3.resource(
    's3',
    endpoint_url='http://localhost:9000',
    aws_access_key_id='admin123',
    aws_secret_access_key='admin123'
)

# bucket名
bucket_name = "lessontest3"

#s3.create_bucket(Bucket=bucket_name)



dir = '/kokugo/'
bucket_name='lessontest3'

#result = s3.list_objects(Bucket=bucket_name, Prefix=dir)

#if not "Contents" in result:
#s3.put_object(Bucket=bucket_name, Key=dir)
s3.Object(bucket_name, dir).put()
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?