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 3 years have passed since last update.

B2 Cloud StorageのS3互換APIをboto3から試してみた

Posted at

Backblaze B2 Cloud StorageがS3互換のAPIに対応したとのことなので、boto3から接続してみました。

必要なもの

  • Backblazeのアカウント

バケット作成・バケット一覧・ファイルアップロードを試してみる

1. boto3.client経由

test1.py
BUCKET = 'sirotosiko-b2-test1'
MYFILE = 'test.txt'

s3 = boto3.client('s3',
endpoint_url = 'https://s3.us-west-002.backblazeb2.com',
aws_access_key_id = '<b2_keyId>',
aws_secret_access_key = '<b2_appKey>')

res = s3.create_bucket(Bucket=BUCKET)

res = s3.list_buckets()
for bucket in res['Buckets']:
    print(bucket['Name'])

with open(MYFILE, 'rb') as data:
    s3.upload_fileobj(data, BUCKET, MYFILE)
$ python test1.py 
sirotosiko-b2-test1

2. boto3. resource経由

test2.py
import boto3

BUCKET = 'sirotosiko-b2-test2'
MYFILE = 'test.txt'

s3 = boto3.resource('s3',
endpoint_url = 'https://s3.us-west-002.backblazeb2.com',
aws_access_key_id = '<b2_keyId>',
aws_secret_access_key = '<b2_appKey>')

mybucket = s3.create_bucket(Bucket=BUCKET)

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

mybucket.upload_file(MYFILE, MYFILE)
$ python test2.py 
s3.Bucket(name='sirotosiko-b2-test1')
s3.Bucket(name='sirotosiko-b2-test2')

3. 作成されたバケットとファイルの確認

作成したバケットの一覧

bucket_after.png

アップロードしたファイルの一覧

file_after.png file_after2.png

雑感

  • そのままboto3から違和感なくアクセスできました。
  • GCP AnthosのAWS対応とか、どんどんクラウドの垣根がなくなっていく感じがします。
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?