3
7

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

AWSのS3へPython+boto3を利用した操作

Posted at

AWSのS3を利用する機会があったのでpython+boto3からの操作を軽くまとめておく

S3へのアップロード

upS3.py
import boto3
//S3の指定等
s3_client = boto3.client('s3')
bucket='バケット名'
//ファイルの名前を指定
print "old Filename?"
oldFilename = raw_input()
print "new Filename?"
newFilename = raw_input()
//アップロードしたいファイル(oldname)をnewnameでs3に保存
s3_client.upload_file(oldFilename, bucket, newFilename)

S3の削除

名前を指定し,確認でyを入力すると削除可能

delS3.py
import boto3
//S3の指定等
s3_client = boto3.client('s3')
bucket='バケット名'
del_flag = 'n'
//ファイルの名前を指定
print "delete Filename?"
delFilename = raw_input()
print "delete[y/n]?"
del_flag = raw_input()
if del_flag=='y':
    s3_client.delete_object(Bucket=bucket, Key=delFilename)

S3の保存ファイルを表示

実行すると保存されているファイル名を表示する

show.py
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('バケット名')
for object in bucket.objects.all():
    print(object.key)

S3のファイルをダウンロード

ダウンロードしたいS3上のファイル名と保存したい名前を入力し、yを押したらダウンロード可能

dawnload.py
import boto3
s3_client = boto3.resource('s3')
bucket=s3_client.Bucket('バケット名')
dl_flag = 'n'
print "S3 Filename?"
S3_Filename = raw_input()
print "new Filename?"
local_Filename = raw_input()
print "download[y/n]?"
dl_flag = raw_input()
if dl_flag=='y':
    bucket.download_file(S3_Filename,local_Filename)
3
7
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
3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?