LoginSignup
8
8

More than 5 years have passed since last update.

boto で S3 アップローダー

Posted at

Googleで調べればさんざん出てくるわけではあるが。
boto でAWSにファイルアップロードするよ。

upload.py
#!/usr/bin/python 
# coding: utf8

import sys
from boto.s3.connection import S3Connection
from boto.s3.key import Key

AWS_ACCESS_KEY  = 'XXXXXXX'
AWS_SECRET_KEY  = 'YYYYYYY'
BUCKET_NAME     = 'mybucket'

conn = S3Connection(
    aws_access_key_id       = AWS_ACCESS_KEY,
    aws_secret_access_key   = AWS_SECRET_KEY)

bucket = conn.get_bucket(BUCKET_NAME)

print "connect:", bucket

if len(sys.argv) == 1:
    print "Error: no input file specified"
    sys.exit()


args = sys.argv
# arg[0] は実行スクリプト名なのでスキップ
args.pop(0)

for arg in args:
    upload_file = arg

    key     = Key(bucket)
    key.key = upload_file
    key.set_contents_from_filename(upload_file)

    # web 公開モードにする
    key.make_public()
    print "upload file:", key

実行は、アップしたいルートディレクトリから

$ python path/to/upload.py file1 file2 ...

ファイルのパーミッションとか、オプションの制御はマニュアルを参照すればいいと思うよ。

最終的には rsync 的な同期スクリプトにしたいけど、とりあえずの下書きってぇ事で。

8
8
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
8
8