某日想导出对象存储某个bucket里的一些文件,发觉使用s3cmd无法正常ls,由于bucket太大。
改使用boto列出文件名称再下载,记录一下
import boto
import boto.s3.connection
class CephS3():
def __init__(self):
access_key = '你的key'
secret_key = '你的secret'
IP = '192.168.1.1'
PORT = 7480
self.conn = boto.connect_s3(
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
host=IP,
port=PORT,
is_secure=False, # 不验证ssl
calling_format=boto.s3.connection.OrdinaryCallingFormat(),
)
def get_bucket(self):
# 获取存储桶
for bucket in self.conn.get_all_buckets():
print ("{name}\t{created}".format(name = bucket.name,created = bucket.creation_date))
def list_bucket_file(self):
#列出指定bucket内的文件名称
bucket = self.conn.get_bucket('resbucket0')
for file in bucket.list():
print(file.name.encode('utf-8'))
obj=CephS3()
obj.get_bucket()
obj.list_bucket_file()