Ejemplo n.º 1
0
 def __init__(self, cow, bucket, zone=None):
     self._cow = cow
     self._bucket = bucket
     if (zone is None):
         self.zone = config.get_default('default_zone')
     else:
         self.zone = zone
Ejemplo n.º 2
0
    async def upload(self):
        """上传操作"""
        self.blockStatus = []
        if config.get_default('default_zone').up_host:
            host = config.get_default('default_zone').up_host
        else:
            host = await config.get_default(
                'default_zone').get_up_host_by_token(self.up_token,
                                                     self.hostscache_dir)

        offset = self.recovery_from_record()
        async for block in _file_iter(self.input_stream, config._BLOCK_SIZE,
                                      offset):
            length = len(block)
            crc = crc32(block)
            ret, info = await self.make_block(block, length, host)
            if ret is None and not info.need_retry():
                return ret, info
            if info.connect_failed():
                if config.get_default('default_zone').up_host_backup:
                    host = config.get_default('default_zone').up_host_backup
                else:
                    host = await config.get_default(
                        'default_zone').get_up_host_backup_by_token(
                            self.up_token, self.hostscache_dir)
            if info.need_retry() or crc != ret['crc32']:
                ret, info = await self.make_block(block, length, host)
                if ret is None or crc != ret['crc32']:
                    return ret, info

            self.blockStatus.append(ret)
            offset += length
            self.record_upload_progress(offset)

            if asyncio.iscoroutinefunction(self.progress_handler):
                await self.progress_handler(
                    ((len(self.blockStatus) - 1) * config._BLOCK_SIZE) +
                    length, self.size)
            elif (callable(self.progress_handler)):
                self.progress_handler(
                    ((len(self.blockStatus) - 1) * config._BLOCK_SIZE) +
                    length, self.size)

        return await self.make_file(host)
Ejemplo n.º 3
0
    async def _form_put(self, up_token, key, data, params, mime_type, crc, hostscache_dir=None, progress_handler=None,
                        file_name=None,
                        modify_time=None,
                        keep_last_modified=False):
        fields = {}
        if params:
            for k, v in params.items():
                fields[k] = str(v)
        if crc:
            fields['crc32'] = crc
        if key is not None:
            fields['key'] = key

        fields['token'] = up_token
        if config.get_default('default_zone').up_host:
            url = config.get_default('default_zone').up_host
        else:
            url = await config.get_default('default_zone').get_up_host_by_token(up_token, hostscache_dir)
        # name = key if key else file_name

        fname = file_name
        if not fname or not fname.strip():
            fname = 'file_name'

        # last modify time
        if modify_time and keep_last_modified:
            fields['x-qn-meta-!Last-Modified'] = rfc_from_timestamp(modify_time)

        r, info = await self._http._post_file(url, data=fields, files={'file': (fname, data, mime_type)})
        if r is None and info.need_retry():
            if info.connect_failed:
                if config.get_default('default_zone').up_host_backup:
                    url = config.get_default('default_zone').up_host_backup
                else:
                    url = await config.get_default('default_zone').get_up_host_backup_by_token(up_token, hostscache_dir)
            if hasattr(data, 'read') is False:
                pass
            elif hasattr(data, 'seek') and (not hasattr(data, 'seekable') or data.seekable()):
                data.seek(0)
            else:
                return r, info
            r, info = await self._http._post_file(url, data=fields, files={'file': (fname, data, mime_type)})

        return r, info
Ejemplo n.º 4
0
 async def change_bucket_permission(self, bucket_name=None, private=1):
     """
     设置 存储空间访问权限
     https://developer.qiniu.com/kodo/api/3946/set-bucket-private
     Args:
         bucket_name: 存储空间名
         private: 0 公开;1 私有 ,str类型
     """
     if not bucket_name:
         bucket_name = self._bucket
     url = "{0}/private?bucket={1}&private={2}".format(
         config.get_default("default_uc_host"), bucket_name, private)
     return await self._post(url)
Ejemplo n.º 5
0
 async def bucket_domain(self, bucket_name=None):
     """
     获取存储空间域名列表
     Args:
         bucket_name: 存储空间名
     """
     if not bucket_name:
         bucket_name = self._bucket
     options = {
         'tbl': bucket_name,
     }
     url = "{0}/v6/domain/list?tbl={1}".format(
         config.get_default("default_api_host"), bucket_name)
     return await self._get(url, options)
Ejemplo n.º 6
0
    async def list(self, prefix=None, marker=None, limit=None, delimiter=None):
        """前缀查询:

        1. 首次请求 marker = None
        2. 无论 err 值如何,均应该先看 ret.get('items') 是否有内容
        3. 如果后续没有更多数据,err 返回 EOF,marker 返回 None(但不通过该特征来判断是否结束)
        具体规格参考:
        http://developer.qiniu.com/docs/v6/api/reference/rs/list.html

        Args:
            prefix:     列举前缀
            marker:     列举标识符
            limit:      单次列举个数限制
            delimiter:  指定目录分隔符

        Returns:
            一个dict变量,类似 {"hash": "<Hash string>", "key": "<Key string>"}
            一个ResponseInfo对象
            一个EOF信息。
        """
        options = {
            'bucket': self._bucket,
        }
        if marker is not None:
            options['marker'] = marker
        if limit is not None:
            options['limit'] = limit
        if prefix is not None:
            options['prefix'] = prefix
        if delimiter is not None:
            options['delimiter'] = delimiter

        url = '{0}/list'.format(config.get_default('default_rsf_host'))
        ret, info = await self._get(url, options)

        eof = False
        if ret and not ret.get('marker'):
            eof = True

        return ret, eof, info
Ejemplo n.º 7
0
    async def batch(self, operations):
        """批量操作:

        在单次请求中进行多个资源管理操作,具体规格参考:
        http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html

        Args:
            operations: 资源管理操作数组,可通过

        Returns:
            一个dict变量,返回结果类似:
                [
                    { "code": <HttpCode int>, "data": <Data> },
                    { "code": <HttpCode int> },
                    { "code": <HttpCode int> },
                    { "code": <HttpCode int> },
                    { "code": <HttpCode int>, "data": { "error": "<ErrorMessage string>" } },
                    ...
                ]
            一个ResponseInfo对象
        """
        url = '{0}/batch'.format(config.get_default('default_rs_host'))
        return await self._post(url, dict(op=operations))
Ejemplo n.º 8
0
 async def _rs_do(self, operation, *args):
     return await self._server_do(config.get_default('default_rs_host'),
                                  operation, *args)