Exemple #1
0
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
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
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
Exemple #5
0
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 ''
Exemple #6
0
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()
Exemple #7
0
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
Exemple #8
0
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()
Exemple #9
0
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