def put_object_from_file(self): # 手动上传大文件 print '上传中' oss = OssAPI(self.regional_node, self.id, self.key) print self.oss_file_config res = oss.put_object_from_file(self.bucket, 'diagnosis1.webm', './diagnosis1.webm') print '搞定' print res.reason
def save_to_oss(filename, uploadset): """将文件保存到OSS上,若保存失败,则抛出IO异常""" config = current_app.config oss = OssAPI(config.get('OSS_HOST'), config.get('OSS_KEY'), config.get('OSS_SECRET')) res = oss.put_object_from_file("xichuangzhu", filename, uploadset.config.destination + '/' + filename) if res.status != 200: raise IOError
def save_to_oss(filename, uploadset): """将文件保存到OSS上,若保存失败,则抛出IO异常""" oss = OssAPI(config.OSS_HOST, config.OSS_KEY, config.OSS_SECRET) res = oss.put_object_from_file( "xichuangzhu", filename, uploadset.config.destination + '/' + filename) status = res.status if status != 200: raise IOError
class OSSBackend(BaseBackend): """ 阿里OSS后端 """ bucket_name = None oss = None def __init__(self, access_id, secret_access_key, bucket_name, host=None): super(OSSBackend, self).__init__() self.bucket_name = bucket_name self.oss = OssAPI(host or OSS_HOST, access_id, secret_access_key) def upload(self, file_path, category): """ 上传 """ filename = os.path.basename(file_path) # 尝试创建bucket rsp = self.oss.create_bucket(self.bucket_name) if rsp.status != 200: # 说明没有创建成功 # 文档说409可能代表已经存在,但经过测试已经存在也会返回200 raise Exception('create_bucket fail: <%s> %s' % (rsp.status, rsp.read())) rsp = self.oss.put_object_from_file( self.bucket_name, os.path.join(category, filename), file_path, ) if rsp.status != 200: raise Exception('put_object_from_file fail: <%s> %s' % (rsp.status, rsp.read())) def clean(self, category, keeps): object_list = self.oss.list_objects(self.bucket_name, category + '/') delete_filename_list = filter_delete_filename_list(object_list, keeps) self.oss.delete_objects(self.bucket_name, delete_filename_list)
class OSSBackend(BaseBackend): """ 阿里OSS后端 """ bucket_name = None oss = None def __init__(self, access_id, secret_access_key, bucket_name, host=None): super(OSSBackend, self).__init__() self.bucket_name = bucket_name self.oss = OssAPI(host or OSS_HOST, access_id, secret_access_key) def upload(self, file_path, category): """ 上传 """ filename = os.path.basename(file_path) # 尝试创建bucket rsp = self.oss.create_bucket(self.bucket_name) if rsp.status != 200: # 说明没有创建成功 # 文档说409可能代表已经存在,但经过测试已经存在也会返回200 raise Exception('create_bucket fail: <%s> %s' % (rsp.status, rsp.read())) rsp = self.oss.put_object_from_file( self.bucket_name, os.path.join(category, filename), file_path, ) if rsp.status != 200: raise Exception('put_object_from_file fail: <%s> %s' % (rsp.status, rsp.read())) def clean(self, category, keeps): object_list = self.oss.list_objects(self.bucket_name, category+'/') delete_filename_list = filter_delete_filename_list(object_list, keeps) self.oss.delete_objects(self.bucket_name, delete_filename_list)
#coding=utf-8 import os from oss.oss_api import OssAPI BUCKET_NAME = "fzx-backup" oss = OssAPI("oss.aliyuncs.com", "", "") part = 'crontab.txt' part_filename = './crontab.txt' res = oss.put_object_from_file(BUCKET_NAME, part, part_filename) if res.status == 200: print "ok" else: print "not ok" #EOP