def __init__(self, conf): self.conf = conf self.oss = OssAPI( conf['host'], conf['access_id'], conf['secret_access_key'], )
class OssFile(object): def __init__(self, conf): self.conf = conf self.oss = OssAPI( conf['host'], conf['access_id'], conf['secret_access_key'], ) def get_link(self, path): return self.conf['link'] % path def get(self, name): res = self.oss.get_object(self.conf['bucket'], name) if (res.status / 100) == 2: return res.read() return '' def put(self, name, content, type=None): if type is not None: path = '%s.%s' % (name, type) else: path = name res = self.oss.put_object_from_string(self.conf['bucket'], path, content) if (res.status / 100) == 2: return path return '' def remove(self, name): res = self.oss.delete_object(self.conf['bucket'], name) if (res.status / 100) == 2: return True return False
class OSSFile(BaseFile): def __init__(self, conf): super(OSSFile, self).__init__(conf) from oss.oss_api import OssAPI self.oss = OssAPI( conf['host'], conf['access_id'], conf['secret_access_key'], ) def get_link(self, name, width=0, height=0, ystart=0, yend=0, source=False): link = self.conf['link'] % name if source: return link format = name.split('.')[-1] if format not in ['jpg', 'jpeg', 'png', 'gif', 'bmp']: format = 'jpg' attrs = [] if width != 0: attrs.append('w_%d' % width) if height != 0: attrs.append('h_%d' % height) if attrs: if width and height: attrs.append('m_fill') attrs.append('limit_0') if attrs: return link + '?x-oss-process=image/resize,' + \ ','.join(attrs) + '/format,' + format return link def get(self, name): res = self.oss.get_object(self.conf['bucket'], name) if (res.status / 100) == 2: return res.read() return '' def put(self, name, content, type=None): name = '%s.%s' % (name, type) if type is not None else name if not name.startswith(self.conf.get('prefix')): name = self.conf.get('prefix', '') + name res = self.oss.put_object_from_string(self.conf['bucket'], name, content) if (res.status / 100) == 2: return name current_app.logger.error('oss error: \n' + res.read()) return '' def remove(self, name): res = self.oss.delete_object(self.conf['bucket'], name) return (res.status / 100) == 2
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 __init__(self, conf): super(OSSFile, self).__init__(conf) from oss.oss_api import OssAPI self.oss = OssAPI( conf['host'], conf['access_id'], conf['secret_access_key'], )
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 OSSFile(BaseFile): def __init__(self, conf): super(OSSFile, self).__init__(conf) from oss.oss_api import OssAPI self.oss = OssAPI( conf['host'], conf['access_id'], conf['secret_access_key'], ) def get_link(self, name, width=0, height=0, ystart=0, yend=0, source=False): link = self.conf['link'] % name if source: return link format = name.split('.')[-1] if format not in ['jpg', 'jpeg', 'png', 'gif', 'bmp']: format = 'jpg' attrs = [] if ystart != 0 and yend != 0: attrs.append('@0-%d-0-%da' % (ystart, yend)) if width != 0: attrs.append('%dw' % width) if height != 0: attrs.append('%dh' % height) if attrs: attrs.append('1e_1c') if attrs or format != 'gif': attrs.append('95Q') if attrs: if format == 'gif': format = 'jpg' return link + '@' + '_'.join(attrs) + '.' + format return link def get(self, name): res = self.oss.get_object(self.conf['bucket'], name) if (res.status / 100) == 2: return res.read() return '' def put(self, name, content, type=None): name = '%s.%s' % (name, type) if type is not None else name if not name.startswith(self.conf.get('prefix')): name = self.conf.get('prefix', '') + name res = self.oss.put_object_from_string(self.conf['bucket'], name, content) if (res.status / 100) == 2: return name current_app.logger.error('oss error: \n' + res.read()) return '' def remove(self, name): res = self.oss.delete_object(self.conf['bucket'], name) return (res.status / 100) == 2
def delete_file(self, file_path): oss = OssAPI(self.regional_node, self.id, self.key) res = oss.delete_object(self.bucket, file_path) if 204 == res.status: # log res_message = "\n%s\n%s" % (res.status, '删除原来文件成功') logging.info(res_message) else: # log res_message = "\n%s\n%s" % (res.status, '删除原来文件失败') logging.info(res_message)
def alioss_file_util(optype, fpath, content = ""): from oss.oss_api import OssAPI bucketname = settings["oss_bucket_name"] endpoint = settings["oss_endpoint"] accessKeyId, accessKeySecret = settings['oss_accessKeyId'], settings['oss_accessKeySecret'] oss = OssAPI(endpoint, accessKeyId, accessKeySecret) if optype == 'read': res = oss.get_object(bucketname, fpath) return res.read() or "" elif optype == 'write': res = oss.put_object_from_string(bucketname, fpath, content) return True return ''
class OSSFile(BaseFile): def __init__(self, conf): super(OSSFile, self).__init__(conf) from oss.oss_api import OssAPI self.oss = OssAPI( conf['host'], conf['access_id'], conf['secret_access_key'], ) def get_link(self, name, width=0, height=0, ystart=0, yend=0, source=False): link = self.conf['link'] % name if source: return link format = name.split('.')[-1] if format not in ['jpg', 'jpeg', 'png', 'gif', 'bmp']: format = 'jpg' attrs = [] if width != 0: attrs.append('w_%d' % width) if height != 0: attrs.append('h_%d' % height) if attrs: attrs.append('m_fill') attrs.append('limit_0') if attrs: return link + '?x-oss-process=image/resize,' + ','.join(attrs) + '/format,' + format return link def get(self, name): res = self.oss.get_object(self.conf['bucket'], name) if (res.status / 100) == 2: return res.read() return '' def put(self, name, content, type=None): name = '%s.%s' % (name, type) if type is not None else name if not name.startswith(self.conf.get('prefix')): name = self.conf.get('prefix', '') + name res = self.oss.put_object_from_string(self.conf['bucket'], name, content) if (res.status / 100) == 2: return name current_app.logger.error('oss error: \n' + res.read()) return '' def remove(self, name): res = self.oss.delete_object(self.conf['bucket'], name) return (res.status / 100) == 2
class OSSFile(BaseFile): def __init__(self, conf): super(OSSFile, self).__init__(conf) from oss.oss_api import OssAPI self.oss = OssAPI( conf['host'], conf['access_id'], conf['secret_access_key'], ) def get_link(self, name, width=0, height=0, ystart=0, yend=0, source=False): link = self.conf['link'] % name if source: return link format = name.split('.')[-1] if format not in ['jpg', 'jpeg', 'png', 'gif', 'bmp']: format = 'jpg' attrs = [] if ystart != 0 and yend != 0: attrs.append('@0-%d-0-%da' % (ystart, yend)) if width != 0: attrs.append('%dw' % width) if height != 0: attrs.append('%dh' % height) if attrs: attrs.append('1e_1c') if attrs or format != 'gif': attrs.append('95Q') if attrs: if format == 'gif': format = 'jpg' return link + '@' + '_'.join(attrs) + '.' + format return link def get(self, name): res = self.oss.get_object(self.conf['bucket'], name) if (res.status / 100) == 2: return res.read() return '' def put(self, name, content, type=None): name = '%s.%s' % (name, type) if type is not None else name if not name.startswith(self.conf.get('prefix')): name = self.conf.get('prefix', '') + name res = self.oss.put_object_from_string(self.conf['bucket'], name, content) if (res.status / 100) == 2: return name return '' def remove(self, name): res = self.oss.delete_object(self.conf['bucket'], name) return (res.status / 100) == 2
class OssOperator(PicBaseOperator): def __init__(self): self.url = None self.filename = None self.oss = OssAPI(END_POINT, ACCESS_ID, ACCESS_KEY) def save(self, file): self.filename = sumfile(file) self.url = self.oss.put_object_from_fp(BUCKET, self.filename, file) def delete(self, filename): self.oss.delete_object(BUCKET, filename) def get_url(self): return self.url
def __init__(self): self.dirname = '' endpoint = current_app.config['OSS_ENDPOINT'] key_id = current_app.config['KEY_ID'] key_secret = current_app.config['KEY_SECRET'] self.bucket_name = current_app.config['OSS_BUCKET_NAME'] self.oss = OssAPI(endpoint, key_id, key_secret)
class OSSUploader(Uploader): def __init__(self): self.dirname = '' endpoint = current_app.config['OSS_ENDPOINT'] key_id = current_app.config['KEY_ID'] key_secret = current_app.config['KEY_SECRET'] self.bucket_name = current_app.config['OSS_BUCKET_NAME'] self.oss = OssAPI(endpoint, key_id, key_secret) def _store(self, fullname, data): self.oss.put_object_from_string(self.bucket_name, fullname, data) def remove(self, filename): fullname = os.path.join(self.dirname, filename) self.oss.delete_object(self.bucket_name, fullname)
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)
def connection(self): if self._connection is None: self._connection = OssAPI( host=self.access_endpoint, access_id=self.access_key, secret_access_key=self.access_secret, port=self.access_port, is_security=self.is_security, sts_token=self.sts_token, ) return self._connection
class SeafOSSClient(object): '''Wraps a oss connection and a bucket''' def __init__(self, conf): self.conf = conf # Due to a bug in httplib we can't use https self.oss = OssAPI(conf.host, conf.key_id, conf.key) def read_object_content(self, obj_id): res = self.oss.get_object(self.conf.bucket_name, obj_id) if res.status != httplib.OK: raise GetObjectError("Failed to get object %s from bucket %s: %s %s" % ( obj_id, self.conf.bucket_name, res.status, res.reason)) return res.read()
def upload_file(self, file_data, file_path, content_type=None): """上传文件到oss服务器上 :param file_data: 文件的数据 :param file_path: 保存到OSS的路径 :return: """ oss = OssAPI(self.regional_node, self.id, self.key) expires = format_timestamp(datetime.datetime.today() + datetime.timedelta(days=+90)) header = {'expires': expires, 'Cache-Control': 'max-age=%s' % (90*24*60*60)} if content_type: res = oss.put_object_from_string(self.bucket, file_path, file_data, headers=header, content_type=content_type) else: res = oss.put_object_from_string(self.bucket, file_path, file_data) if 200 == res.status: return True, file_path else: # log res_message = "OSS ERROR\n%s\n%s" % (res.status, res.read()) logging.info(res_message) return False, u'上传文件出错!'
class SeafOSSClient(object): '''Wraps a oss connection and a bucket''' def __init__(self, conf): self.conf = conf # Due to a bug in httplib we can't use https self.oss = OssAPI(conf.host, conf.key_id, conf.key) def read_object_content(self, obj_id): res = self.oss.get_object(self.conf.bucket_name, obj_id) if res.status != httplib.OK: raise GetObjectError( "Failed to get object %s from bucket %s: %s %s" % (obj_id, self.conf.bucket_name, res.status, res.reason)) return res.read()
list_data = f.read() upload_list = list_data.splitlines() if list_data == '': break object_name = upload_list[0] total = len(upload_list) with open(list_file, 'wb') as f: f.write('/n'.join(upload_list[1:])) print str(total), 'files to upload...' try: filename = filedir + object_name oss = OssAPI(upload_endpoint, access_id, access_key) upload_id = ElementTree.fromstring( oss.init_multi_upload(upload_bucket, object_name).read()).find('UploadId').text file_size = os.path.getsize(filename) pos_list = range(0, file_size, patch_size) len_list = [patch_size] * int(file_size / patch_size) + [ file_size % patch_size ] part_id_list = range(1, len(pos_list) + 1) md5_list = range(len(pos_list)) thread_pool = [] for index, part_id in enumerate(part_id_list): upload_thread = UploadThread(oss, filename, pos_list, len_list,
def __init__(self, conf, *args, **kwargs): API.__init__(self, conf["endpoint"], conf["id"], conf["secret"]) self.timeout = 90
def __init__(self, conf): self.conf = conf # Due to a bug in httplib we can't use https self.oss = OssAPI(conf.host, conf.key_id, conf.key)
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)
list_data = f.read() upload_list = list_data.splitlines() if list_data == '': break object_name = upload_list[0] total = len(upload_list) with open(list_file, 'wb') as f: f.write('/n'.join(upload_list[1:])) print str(total), 'files to upload...' try: filename = filedir + object_name oss = OssAPI(upload_endpoint, access_id, access_key) upload_id = ElementTree.fromstring(oss.init_multi_upload(upload_bucket, object_name).read()).find('UploadId').text file_size = os.path.getsize(filename) pos_list = range(0, file_size, patch_size) len_list = [patch_size] * int(file_size/patch_size) + [file_size%patch_size] part_id_list = range(1, len(pos_list)+1) md5_list = range(len(pos_list)) thread_pool = [] for index, part_id in enumerate(part_id_list): upload_thread = UploadThread(oss, filename, pos_list, len_list, part_id_list,
def __init__(self): self.url = None self.filename = None self.oss = OssAPI(END_POINT, ACCESS_ID, ACCESS_KEY)
#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
def __init__(self, access_key, access_key_secret, bucket, endpoint): self.access_key = access_key self.access_key_secret = access_key_secret self.bucket = bucket self.endpoint = endpoint self.oss = OssAPI(endpoint, access_key, access_key_secret)
class AliOss(object): def __init__(self, access_key, access_key_secret, bucket, endpoint): self.access_key = access_key self.access_key_secret = access_key_secret self.bucket = bucket self.endpoint = endpoint self.oss = OssAPI(endpoint, access_key, access_key_secret) def put_user_avatar(self, user_id, avatar): content_type = avatar['content_type'] file_format = avatar['filename'].split('.').pop().lower() file_name = '%s_%s' % (user_id, int(time.time())) file_dir = 'avatar/user/%s.%s' % (file_name, file_format) res = self.oss.put_object_from_string(self.bucket, file_dir, avatar['body'], content_type) if res.status == 200: return file_dir def put_room_avatar(self, user_id, avatar): content_type = avatar['content_type'] file_format = avatar['filename'].split('.').pop().lower() file_name = '%s_%s' % (user_id, int(time.time())) file_dir = 'avatar/room/%s.%s' % (file_name, file_format) res = self.oss.put_object_from_string(self.bucket, file_dir, avatar['body'], content_type) if res.status == 200: return file_dir def put_validate(self, user_id, validate): content_type = validate['content_type'] file_format = validate['filename'].split('.').pop().lower() file_name = '%s_%s%s' % (user_id, int(time.time()), createNoncestr()) m = hashlib.md5() m.update(file_name) file_name = m.hexdigest() file_dir = 'validate/%s.%s' % (file_name, file_format) res = self.oss.put_object_from_string(self.bucket, file_dir, validate['body'], content_type) if res.status == 200: return file_dir def put_images(self, user_id, files): file_dirs = [] now = datetime.now() date_str = now.strftime('%Y%m%d_%H') img_dir = 'images/%s/' % date_str for file in files: file_name = file['filename'] content_type = file['content_type'] file_format = file['filename'].split('.').pop().lower() hash_str = '%s%s%s' % (int(time.time()), user_id, file_name) m = hashlib.md5() m.update(hash_str) m_digest = m.hexdigest() md5_file_name = '%s.%s' % (m_digest, file_format) object_path = img_dir + md5_file_name res = self.oss.put_object_from_string(self.bucket, object_path, file['body'], content_type) if res.status == 200: file_dirs.append(object_path) return file_dirs def put_video(self, user_id, video): content_type = video['content_type'] file_format = video['filename'].split('.').pop().lower() file_name = '%s_%s' % (user_id, int(time.time())) file_dir = 'video/%s.%s' % (file_name, file_format) res = self.oss.put_object_from_string(self.bucket, file_name, video['body'], content_type) if res.status == 200: return file_dir