Overview
When verifying some features of S3, you often need to use some basic operations, for this reason, record these contents, easy to find at a later stage.
Script
s3_sample.py
# -*-coding:UTF-8-*-
import boto
import boto.s3.connection
import os
from boto.s3.key import Key
access_key = 'WJ1ID9FHBAR71C1TTQ0I'
secret_key = 'NTK5DNbFSYYItB6kyvkYgJ3wA3tEDe4RxJhUcKcU'
# 建立连接
print "createt connection BEGIN \n"
conn = boto.connect_s3(
aws_access_key_id = access_key,
aws_secret_access_key = secret_key,
host = '172.17.59.8',
is_secure=False, #uncomment if you are not using ssl
calling_format = boto.s3.connection.OrdinaryCallingFormat(),
)
print conn
print "createt connection END\n"
#列出所有已有的bucket,以及创建时间
print "list all the bucket in this account, empty of course"
for bucket in conn.get_all_buckets():
print "{name}\t{created}".format(
name = bucket.name,
created = bucket.creation_date,
)
print "list all buckets finished\n"
# 查找bucket是否存在
print "create bucket and list all bucket BEGIN"
bucket_name = "my-bucket"
bucket = conn.lookup(bucket_name)
if bucket == None :
print "bucket {} is not exists".format(bucket_name)
# 创建bucket
bucket = conn.create_bucket(bucket_name)
# 再次列出所有的bucket,查看是否存在新建的bucket
for bucket in conn.get_all_buckets():
print "{name}\t{created}".format(
name = bucket.name,
created = bucket.creation_date,
)
print "create bucket and list all bucket END\n"
#查看bucket中的所有对象
print "list all the OBJECTS in bucket, empty of course"
bucket = conn.lookup(bucket_name)
for key in bucket.list():
print "{name}\t{size}\t{modified}".format(
name = key.name,
size = key.size,
modified =
key.last_modified,
)
print "list all the OBJECTS in bucket END\n"
# 上传对象,对象的内容来自字符串
key_name = "my-object"
print "upload object from string ,and list it "
key = bucket.lookup(key_name)
if key == None:
print "key {} is not exists in bucket {}".format(key_name,bucket_name)
k = Key(bucket)
k.key = key_name
k.set_contents_from_string("this object is generated by string")
for key in bucket.list():
print "{name}\t{size}\t{modified}".format(
name = key.name,
size = key.size,
modified =
key.last_modified,
)
print "upload object from string ,and list it END\n "
# 上传对象,对象的内容来自文件
key_name = "my-object-from-file"
print "upload object from filename ,and list it "
filename_need_upload = "/var/log/kern.log"
key = bucket.lookup(key_name)
if key == None:
print "key {} is not exists in bucket {}".format(key_name,bucket_name)
k = Key(bucket)
k.key = key_name
k.set_contents_from_filename(filename_need_upload)
#上传完毕后,列出所有的对象,查看新上传的文件是否存在。
for key in bucket.list():
print "{name}\t{size}\t{modified}".format(
name = key.name,
size = key.size,
modified =
key.last_modified,
)
# 上传对象的同时,设置对象的元数据信息
key_name = "my-object-with-meta"
filename_need_upload = "/var/log/kern.log"
key = bucket.lookup(key_name)
if key == None:
print "key {} is not exists in bucket {}".format(key_name,bucket_name)
k = Key(bucket)
k.key = key_name
k.metadata = { 'name':'xyz', 'meta2':'value2'}
k.set_contents_from_filename(filename_need_upload)
for key in bucket.list():
print "{name}\t{size}\t{modified}".format(
name = key.name,
size = key.size,
modified =
key.last_modified,
)
# 获取对象的元数据信息
key_name = "my-object-with-meta"
k = bucket.get_key(key_name)
print "name {} \nmeta {}".format(key_name, k.metadata)
# 更新已有对象的元数据信息:
# key_name = "my-object"
# k = bucket.get_key(key_name)
# meta_add = {'meta-key':'meta data is updated afterward', 'meta2':'meta data 2'}
# set_remote_metadata第一个参数为添加的元数据信息,第二个参数为要删除的属性
# k.set_remote_metadata(meta_add, {}, True)
k = bucket.get_key(key_name)
print "name {} \nmeta {}".format(key_name, k.metadata)
print "upload object from file ,and list it END\n"
# 下载刚刚上传的对象
key_name = "my-object-from-file"
filename_of_download = "/tmp/kern.log.bak"
print "download object to file string ,and check file "
key = bucket.get_key(key_name)
key.get_contents_to_filename(filename_of_download)
if os.path.isfile(filename_of_download):
print "{} is file ,download successfully".format(filename_of_download)
print "download object to file string ,and check file END\n "
# 设置ACL
print "set acl"
bucket_name = "my-bucket"
bucket = conn.lookup(bucket_name)
print "set my-bucket's acl to private"
access_control_policy = 'private'
bucket.set_acl(access_control_policy)
print "set one object's acl to public-read"
access_control_policy = 'public-read'
key = list(bucket.list())[0]
key.set_acl(access_control_policy)
print "set acl END\n"
# 获取ACL
print "get acl"
acp = bucket.get_acl()
for grant in acp.acl.grants:
print grant.permission, grant.display_name, grant.email_address, grant.id
acp = key.get_acl()
for grant in acp.acl.grants:
print grant.permission, grant.display_name, grant.email_address, grant.id
print "get acl END\n"
# 删除bucket中的某个对象
key_name = "my-object-from-file"
bucket.delete_key(key_name)
# 删除之后查看对应的key 是否存在
for key in bucket.list():
print "{name}\t{size}\t{modified}".format(
name = key.name,
size = key.size,
modified =
key.last_modified,
)
# 删除bucket中所有的objcet
for key in bucket.list():
bucket.delete_key(key.name)
# 删除bucket
bucket_name = "my-bucket"
conn.delete_bucket(bucket_name)