about 8 years ago

Amazon web service 是個相當有名的雲端服務,
這次試著使用 Python 來存取 Amazon s3 資料。

請先註冊開啟 Amazon s3 服務

1. 請先安裝 boto (Python interface to Amazon Web Services)

$ sudo pip install boto 

2. 取得 Amazon s3 access key & secrect key

進入 Amazon 帳戶設定,Create New User 後會取得一組 access & secrect key,
但還無法存取,點選 User 內的 Attatch Policy,勾選 AmazonS3FullAccess

3. 至 Amazon s3 建立一個 bucket

4. import s3 connection

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

conn = S3Connection(${yourAccessKey}, ${yourSecrectKey})
bucket = conn.get_bucket(${bucketname})

fileKey = Key(bucket)
fileKey.key = ${fileName}
# if you want to get file in folder -> fileKey.key = '${folderName}/${fileName}'

fileKey.set_contents_from_string(${changeString})
fileKey.make_public()

更多 boto 用法參考文件

← 雲端平台服務:Heroku ( Schdeuler ) iOS : Facebook ComponentKit 試用 →