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
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 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 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
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)
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