Esempio n. 1
0
    def __init__(self, host=None, access_id=None, secret_access_key=None):
        '''
        args like:
            host = "storage.aliyun.com"
            access_id = "nf0vweubkp3wozejft46l5m4"
            secret_access_key = "64Z0v2Bc9asbZTMHVS2wFHbKNvI="
        '''

        self.set_key(host, access_id, secret_access_key)
        self._async = AsyncCall(logging, threads=2)
        self._acl_buff = {}
Esempio n. 2
0
    def __init__(self, host = None, access_id = None, secret_access_key = None):
        '''
        args like:
            host = "storage.aliyun.com"
            access_id = "nf0vweubkp3wozejft46l5m4"
            secret_access_key = "64Z0v2Bc9asbZTMHVS2wFHbKNvI="
        '''

        self.set_key(host, access_id, secret_access_key)
        self._async = AsyncCall(logging, threads = 2)
        self._acl_buff = {}
Esempio n. 3
0
class AliyunOSS():
    def __init__(self, host = None, access_id = None, secret_access_key = None):
        '''
        args like:
            host = "storage.aliyun.com"
            access_id = "nf0vweubkp3wozejft46l5m4"
            secret_access_key = "64Z0v2Bc9asbZTMHVS2wFHbKNvI="
        '''

        self.set_key(host, access_id, secret_access_key)
        self._async = AsyncCall(logging, threads = 2)
        self._acl_buff = {}

    def set_key(self, host, access_id, secret_access_key):
        self.is_init_key = access_id and secret_access_key
        self._api = OssAPI(host, access_id, secret_access_key)

    def _exceptHandler(self, e):
        import traceback
        traceback.print_exc()

    def create_bucket(self, callback, bucket, acl = '', headers = {}):
        adapter = _ResultAdapter("PutBucket",
                                 self._api.create_bucket,
                                 GetObject)
        callable = Callable(adapter,
                            (bucket, acl, headers),
                            resultHandler = callback,
                            exceptHandler = self._exceptHandler)
        logging.info('Calling PutBucket')
        self._async.add(callable)

    def delete_bucket(self, callback, bucket):
        adapter = _ResultAdapter("DeleteBucket",
                                 self._api.delete_bucket,
                                 GetObject)
        callable = Callable(adapter,
                            (bucket,),
                            resultHandler = callback,
                            exceptHandler = self._exceptHandler)
        logging.info('Calling DeleteBucket')
        self._async.add(callable)

    def delete_object(self, callback, bucket, object, headers = {}):
        adapter = _ResultAdapter("DeleteObject",
                                 self._api.delete_object,
                                 GetObject)
        callable = Callable(adapter,
                            (bucket, object, headers),
                            resultHandler = callback,
                            exceptHandler = self._exceptHandler)
        logging.info('Calling DeleteObject [%s]' % object)
        self._async.add(callable)

    def get_service(self, callback):
        adapter = _ResultAdapter("GetService",
                                 self._api.get_service,
                                 GetServiceXml)
        callable = Callable(adapter, None,
                            resultHandler = callback,
                            exceptHandler = self._exceptHandler)
        logging.info('Calling GetService')
        self._async.add(callable)

    def get_bucket(self, callback, bucket,
                   prefix = '', marker = '',
                   delimiter = '/', max_key = '',
                   headers = {}):
        '''
        prefix  限定返回的object key必须以prefix作为前缀。
                注意使用prefix查询时,返回的key中仍会包含prefix。 
        max-keys  限定此次返回object的最大数,如果不设定,默认为1000,
                  max-keys取值不能大于1000。 
        marker  设定结果从marker之后按字母排序的第一个开始返回。 
        delimiter 是一个用于对Object名字进行分组的字符。
                  所有名字包含指定的前缀且第一次出现delimiter字符之间的object
                  作为一组元素——CommonPrefixes。
        '''
        adapter = _ResultAdapter("GetBucket",
                         self._api.get_bucket,
                         GetBucketXml)
        callable = Callable(adapter,
                            (bucket, prefix, marker, delimiter, max_key),
                            resultHandler = callback,
                            exceptHandler = self._exceptHandler)
        logging.info('Calling GetBucket [%s-%s]' % (bucket, prefix))
        self._async.add(callable)

    def get_bucket_acl(self, callback, bucket):
        buf = self._acl_buff.get(bucket)
        if buf:
            return callback(buf)
        adapter = _ResultAdapter("GetBucketAcl",
                                 self._api.get_bucket_acl,
                                 GetBucketAclXml)
        def _callable(res):
            self._acl_buff[bucket] = res
            callable(res)
        adapter_callable = Callable(adapter,
                            (bucket,),
                            resultHandler = _callable,
                            exceptHandler = self._exceptHandler)
        logging.info('Calling GetBucketAcl [%s]' % bucket)
        self._async.add(adapter_callable)

    def get_object_to_file(self, callback, bucket, object, filename, headers = {}):
        adapter = _ResultAdapter("GetObject",
                                 self._api.get_object_to_file,
                                 GetObject)
        callable = Callable(adapter,
                            (bucket, object, filename),
                            resultHandler = callback,
                            exceptHandler = self._exceptHandler)
        logging.info('Calling GetObject [%s\%s]' % (bucket, object))
        self._async.add(callable)

    def put_object_from_string(self, callback, bucket, object, input_content,
                               content_type = OssAPI.DefaultContentType, headers = {}):
        adapter = _ResultAdapter("PutObject",
                                 self._api.put_object_from_string,
                                 GetObject)
        callable = Callable(adapter,
                            (bucket, object, input_content, content_type, headers),
                            resultHandler = callback,
                            exceptHandler = self._exceptHandler)
        logging.info('Calling PutObject [%s\%s]' % (bucket, object))
        self._async.add(callable)

    def put_object_from_file(self, callback, bucket, object, filename,
                               content_type = OssAPI.DefaultContentType, headers = {}):
        adapter = _ResultAdapter("PutObject",
                                 self._api.put_object_from_file,
                                 GetObject)
        callable = Callable(adapter,
                            (bucket, object, filename, content_type, headers),
                            resultHandler = callback,
                            exceptHandler = self._exceptHandler)
        logging.info('Calling PutObject [%s\%s]' % (bucket, object))
        self._async.add(callable)

    def head_object(self, callback, bucket, object, headers = {}):
        adapter = _ResultAdapter("HeadObject",
                         self._api.head_object,
                         GetObject)

        callable = Callable(adapter,
                            (bucket, object,),
                            resultHandler = callback,
                            exceptHandler = self._exceptHandler)
        logging.info('Calling HeadObject [%s\%s]' % (bucket, object))
        self._async.add(callable)
Esempio n. 4
0
class AliyunOSS():
    def __init__(self, host=None, access_id=None, secret_access_key=None):
        '''
        args like:
            host = "storage.aliyun.com"
            access_id = "nf0vweubkp3wozejft46l5m4"
            secret_access_key = "64Z0v2Bc9asbZTMHVS2wFHbKNvI="
        '''

        self.set_key(host, access_id, secret_access_key)
        self._async = AsyncCall(logging, threads=2)
        self._acl_buff = {}

    def set_key(self, host, access_id, secret_access_key):
        self.is_init_key = access_id and secret_access_key
        self._api = OssAPI(host, access_id, secret_access_key)

    def _exceptHandler(self, e):
        import traceback
        traceback.print_exc()

    def create_bucket(self, callback, bucket, acl='', headers={}):
        adapter = _ResultAdapter("PutBucket", self._api.create_bucket,
                                 GetObject)
        callable = Callable(adapter, (bucket, acl, headers),
                            resultHandler=callback,
                            exceptHandler=self._exceptHandler)
        logging.info('Calling PutBucket')
        self._async.add(callable)

    def delete_bucket(self, callback, bucket):
        adapter = _ResultAdapter("DeleteBucket", self._api.delete_bucket,
                                 GetObject)
        callable = Callable(adapter, (bucket, ),
                            resultHandler=callback,
                            exceptHandler=self._exceptHandler)
        logging.info('Calling DeleteBucket')
        self._async.add(callable)

    def delete_object(self, callback, bucket, object, headers={}):
        adapter = _ResultAdapter("DeleteObject", self._api.delete_object,
                                 GetObject)
        callable = Callable(adapter, (bucket, object, headers),
                            resultHandler=callback,
                            exceptHandler=self._exceptHandler)
        logging.info('Calling DeleteObject [%s]' % object)
        self._async.add(callable)

    def get_service(self, callback):
        adapter = _ResultAdapter("GetService", self._api.get_service,
                                 GetServiceXml)
        callable = Callable(adapter,
                            None,
                            resultHandler=callback,
                            exceptHandler=self._exceptHandler)
        logging.info('Calling GetService')
        self._async.add(callable)

    def get_bucket(self,
                   callback,
                   bucket,
                   prefix='',
                   marker='',
                   delimiter='/',
                   max_key='',
                   headers={}):
        '''
        prefix  限定返回的object key必须以prefix作为前缀。
                注意使用prefix查询时,返回的key中仍会包含prefix。 
        max-keys  限定此次返回object的最大数,如果不设定,默认为1000,
                  max-keys取值不能大于1000。 
        marker  设定结果从marker之后按字母排序的第一个开始返回。 
        delimiter 是一个用于对Object名字进行分组的字符。
                  所有名字包含指定的前缀且第一次出现delimiter字符之间的object
                  作为一组元素——CommonPrefixes。
        '''
        adapter = _ResultAdapter("GetBucket", self._api.get_bucket,
                                 GetBucketXml)
        callable = Callable(adapter,
                            (bucket, prefix, marker, delimiter, max_key),
                            resultHandler=callback,
                            exceptHandler=self._exceptHandler)
        logging.info('Calling GetBucket [%s-%s]' % (bucket, prefix))
        self._async.add(callable)

    def get_bucket_acl(self, callback, bucket):
        buf = self._acl_buff.get(bucket)
        if buf:
            return callback(buf)
        adapter = _ResultAdapter("GetBucketAcl", self._api.get_bucket_acl,
                                 GetBucketAclXml)

        def _callable(res):
            self._acl_buff[bucket] = res
            callable(res)

        adapter_callable = Callable(adapter, (bucket, ),
                                    resultHandler=_callable,
                                    exceptHandler=self._exceptHandler)
        logging.info('Calling GetBucketAcl [%s]' % bucket)
        self._async.add(adapter_callable)

    def get_object_to_file(self,
                           callback,
                           bucket,
                           object,
                           filename,
                           headers={}):
        adapter = _ResultAdapter("GetObject", self._api.get_object_to_file,
                                 GetObject)
        callable = Callable(adapter, (bucket, object, filename),
                            resultHandler=callback,
                            exceptHandler=self._exceptHandler)
        logging.info('Calling GetObject [%s\%s]' % (bucket, object))
        self._async.add(callable)

    def put_object_from_string(self,
                               callback,
                               bucket,
                               object,
                               input_content,
                               content_type=OssAPI.DefaultContentType,
                               headers={}):
        adapter = _ResultAdapter("PutObject", self._api.put_object_from_string,
                                 GetObject)
        callable = Callable(
            adapter, (bucket, object, input_content, content_type, headers),
            resultHandler=callback,
            exceptHandler=self._exceptHandler)
        logging.info('Calling PutObject [%s\%s]' % (bucket, object))
        self._async.add(callable)

    def put_object_from_file(self,
                             callback,
                             bucket,
                             object,
                             filename,
                             content_type=OssAPI.DefaultContentType,
                             headers={}):
        adapter = _ResultAdapter("PutObject", self._api.put_object_from_file,
                                 GetObject)
        callable = Callable(adapter,
                            (bucket, object, filename, content_type, headers),
                            resultHandler=callback,
                            exceptHandler=self._exceptHandler)
        logging.info('Calling PutObject [%s\%s]' % (bucket, object))
        self._async.add(callable)

    def head_object(self, callback, bucket, object, headers={}):
        adapter = _ResultAdapter("HeadObject", self._api.head_object,
                                 GetObject)

        callable = Callable(adapter, (
            bucket,
            object,
        ),
                            resultHandler=callback,
                            exceptHandler=self._exceptHandler)
        logging.info('Calling HeadObject [%s\%s]' % (bucket, object))
        self._async.add(callable)