Beispiel #1
0
    def _upload_slice_data(self, request, file_content, session, offset, retry=3):
        """串行分片第二步, 上传数据分片

        :param request:
        :param file_content:
        :param session:
        :param offset:
        :return:
        """
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        auth = cos_auth.Auth(self._cred)
        expired = int(time.time()) + self._expired_period
        sign = auth.sign_more(bucket, cos_path, expired)

        http_header = dict()
        http_header['Authorization'] = sign
        http_header['User-Agent'] = self._config.get_user_agent()

        http_body = dict()
        http_body['op'] = 'upload_slice_data'
        http_body['filecontent'] = file_content
        http_body['session'] = session
        http_body['offset'] = str(offset)
        if request.sha1_content is not None:
            http_body['sha'] = request.sha1_content

        timeout = self._config.get_timeout()

        for _ in range(retry):
            ret = self.send_request('POST', bucket, cos_path, headers=http_header, files=http_body, timeout=timeout)
            if ret['code'] == 0:
                return ret
        else:
            return ret
Beispiel #2
0
    def list_folder(self, request):
        """list目录

        :param request:
        :return:
        """
        assert isinstance(request, ListFolderRequest)
        check_params_ret = self._check_params(request)
        if check_params_ret is not None:
            return check_params_ret

        http_body = dict()
        http_body['op'] = 'list'
        http_body['num'] = request.get_num()

        http_body['context'] = request.get_context()

        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        list_path = request.get_cos_path() + request.get_prefix()
        expired = int(time.time()) + self._expired_period
        sign = auth.sign_more(bucket, list_path, expired)

        http_header = dict()
        http_header['Authorization'] = sign
        http_header['User-Agent'] = self._config.get_user_agent()

        timeout = self._config.get_timeout()
        return self.send_request('GET',
                                 bucket,
                                 list_path,
                                 headers=http_header,
                                 params=http_body,
                                 timeout=timeout)
Beispiel #3
0
    def _upload_slice_finish(self, request, session, filesize):
        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        expired = int(time.time()) + self._expired_period
        sign = auth.sign_more(bucket, cos_path, expired)

        http_header = dict()
        http_header['Authorization'] = sign
        http_header['User-Agent'] = self._config.get_user_agent()

        http_body = dict()
        http_body['op'] = "upload_slice_finish"
        http_body['session'] = session
        http_body['filesize'] = str(filesize)
        if request.sha1_list is not None:
            http_body['sha'] = request.sha1_list[-1]["datasha"]
        timeout = self._config.get_timeout()

        return self.send_request('POST',
                                 bucket,
                                 cos_path,
                                 headers=http_header,
                                 files=http_body,
                                 timeout=timeout)
Beispiel #4
0
    def create_folder(self, request):
        """创建目录

        :param request:
        :return:
        """
        assert isinstance(request, CreateFolderRequest)
        check_params_ret = self._check_params(request)
        if check_params_ret is not None:
            return check_params_ret

        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        expired = int(time.time()) + self._expired_period
        sign = auth.sign_more(bucket, cos_path, expired)

        http_header = dict()
        http_header['Authorization'] = sign
        http_header['Content-Type'] = 'application/json'
        http_header['User-Agent'] = self._config.get_user_agent()

        http_body = dict()
        http_body['op'] = 'create'
        http_body['biz_attr'] = request.get_biz_attr()

        timeout = self._config.get_timeout()
        return self.send_request('POST',
                                 bucket,
                                 cos_path,
                                 headers=http_header,
                                 data=json.dumps(http_body),
                                 timeout=timeout)
Beispiel #5
0
    def stat_base(self, request):
        """获取文件和目录的属性

        :param request:
        :return:
        """
        check_params_ret = self._check_params(request)
        if check_params_ret is not None:
            return check_params_ret

        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        expired = int(time.time()) + self._expired_period
        sign = auth.sign_more(bucket, cos_path, expired)

        http_header = dict()
        http_header['Authorization'] = sign
        http_header['User-Agent'] = self._config.get_user_agent()

        http_body = dict()
        http_body['op'] = 'stat'

        timeout = self._config.get_timeout()
        return self.send_request('GET',
                                 bucket,
                                 cos_path,
                                 headers=http_header,
                                 params=http_body,
                                 timeout=timeout)
Beispiel #6
0
    def del_base(self, request):
        check_params_ret = self._check_params(request)
        if check_params_ret != None:
            return check_params_ret

        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        sign = auth.sign_once(bucket, cos_path)

        http_header = {}
        http_header['Authorization'] = sign
        http_header['Content-Type'] = 'application/json'
        http_header['User-Agent'] = self._config.get_user_agent()

        http_body = {}
        http_body['op'] = 'delete'

        timeout = self._config.get_timeout()
        return self.send_request('POST',
                                 bucket,
                                 cos_path,
                                 headers=http_header,
                                 data=json.dumps(http_body),
                                 timeout=timeout)
Beispiel #7
0
    def del_base(self, request):
        """删除文件或者目录, is_file_op为True表示是文件操作

        :param request:
        :return:
        """
        check_params_ret = self._check_params(request)
        if check_params_ret is not None:
            return check_params_ret

        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        sign = auth.sign_once(bucket, cos_path)

        http_header = dict()
        http_header['Authorization'] = sign
        http_header['Content-Type'] = 'application/json'
        http_header['User-Agent'] = self._config.get_user_agent()

        http_body = {'op': 'delete'}

        timeout = self._config.get_timeout()
        return self.send_request('POST',
                                 bucket,
                                 cos_path,
                                 headers=http_header,
                                 data=json.dumps(http_body),
                                 timeout=timeout)
Beispiel #8
0
    def _upload_slice_control(self, request):
        """串行分片第一步, 上传控制分片

        :param request:
        :return:
        """
        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        expired = int(time.time()) + self._expired_period
        sign = auth.sign_more(bucket, cos_path, expired)

        http_header = dict()
        http_header['Authorization'] = sign
        http_header['User-Agent'] = self._config.get_user_agent()

        local_path = request.get_local_path()
        file_size = os.path.getsize(local_path)
        slice_size = request.get_slice_size()
        biz_atrr = request.get_biz_attr()

        http_body = dict()
        http_body['op'] = 'upload_slice_init'
        if request.enable_sha1:
            http_body['sha'] = request.sha1_list[-1]["datasha"]
            http_body['uploadparts'] = json.dumps(request.sha1_list)
        http_body['filesize'] = str(file_size)
        http_body['slice_size'] = str(slice_size)
        http_body['biz_attr'] = biz_atrr
        http_body['insertOnly'] = str(request.get_insert_only())

        timeout = self._config.get_timeout()
        return self.send_request('POST', bucket, cos_path, headers=http_header, files=http_body, timeout=timeout)
Beispiel #9
0
    def update_folder(self, request):
        assert isinstance(request, UpdateFolderRequest)
        check_params_ret = self._check_params(request)
        if check_params_ret != None:
            return check_params_ret

        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        sign = auth.sign_once(bucket, cos_path)

        http_header = {}
        http_header['Authorization'] = sign
        http_header['Content-Type'] = 'application/json'
        http_header['User-Agent'] = self._config.get_user_agent()

        http_body = {}
        http_body['op'] = 'update'
        http_body['biz_attr'] = request.get_biz_attr()

        timeout = self._config.get_timeout()
        return self.send_request('POST',
                                 bucket,
                                 cos_path,
                                 headers=http_header,
                                 data=json.dumps(http_body),
                                 timeout=timeout)
Beispiel #10
0
    def _upload_slice_control(self, request):
        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        expired = int(time.time()) + self._expired_period
        sign = auth.sign_more(bucket, cos_path, expired)

        http_header = {}
        http_header['Authorization'] = sign
        http_header['User-Agent'] = self._config.get_user_agent()

        local_path = request.get_local_path()
        sha1_digest = self._sha1_file(local_path)
        file_size = os.path.getsize(local_path)
        slice_size = request.get_slice_size()
        biz_atrr = request.get_biz_attr()

        http_body = {}
        http_body['op'] = 'upload_slice'
        http_body['sha'] = sha1_digest
        http_body['filesize'] = str(file_size)
        http_body['slice_size'] = str(slice_size)
        http_body['biz_attr'] = request.get_biz_attr()
        http_body['insertOnly'] = str(request.get_insert_only())

        timeout = self._config.get_timeout()
        return self.send_request('POST',
                                 bucket,
                                 cos_path,
                                 headers=http_header,
                                 files=http_body,
                                 timeout=timeout)
Beispiel #11
0
    def move_file(self, request):
        assert isinstance(request, MoveFileRequest)
        check_params_ret = self._check_params(request)
        if check_params_ret != None:
            return check_params_ret

        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        expired = int(time.time()) + self._expired_period
        sign = auth.sign_more(bucket, cos_path, expired)

        http_header = {}
        http_header['Authorization'] = sign
        http_header['Content-Type'] = 'application/json'
        http_header['User-Agent'] = self._config.get_user_agent()

        http_body = {}
        http_body['op'] = 'move'
        http_body['dest_fileid'] = request.get_dst_cos_path()
        http_body['to_over_write'] = request.get_over_write()

        timeout = self._config.get_timeout()
        return self.send_request('POST',
                                 bucket,
                                 cos_path,
                                 headers=http_header,
                                 data=json.dumps(http_body),
                                 timeout=timeout)
Beispiel #12
0
    def download_object(self, request):
        assert isinstance(request, DownloadObjectRequest)

        auth = cos_auth.Auth(self._cred)
        sign = auth.sign_download(request.get_bucket_name(), request.get_cos_path(), self._config.get_sign_expired())
        url = self.build_download_url(request.get_bucket_name(), request.get_cos_path(), sign)
        logger.info("Uri is %s" % url)

        ret = self.__download_object_url(url, request._custom_headers)
        return ret
Beispiel #13
0
    def upload_single_file(self, request):
        """ 单文件上传

        :param request:
        :return:
        """
        assert isinstance(request, UploadFileRequest)
        check_params_ret = self._check_params(request)
        if check_params_ret is not None:
            return check_params_ret

        local_path = request.get_local_path()
        file_size = os.path.getsize(local_path)
        # 判断文件是否超过单文件最大上限, 如果超过则返回错误
        # 并提示用户使用别的接口
        if file_size > self.max_single_file:
            return CosErr.get_err_msg(CosErr.NETWORK_ERROR, 'file is too big, please use upload_file interface')

        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        expired = int(time.time()) + self._expired_period
        sign = auth.sign_more(bucket, cos_path, expired)

        http_header = dict()
        http_header['Authorization'] = sign
        http_header['User-Agent'] = self._config.get_user_agent()

        with open(local_path, 'rb') as f:
            file_content = f.read()

        http_body = dict()
        http_body['op'] = 'upload'
        http_body['filecontent'] = file_content
        http_body['sha'] = FileOp._sha1_content(file_content)
        http_body['biz_attr'] = request.get_biz_attr()
        http_body['insertOnly'] = str(request.get_insert_only())

        timeout = self._config.get_timeout()
        ret = self.send_request('POST', bucket, cos_path, headers=http_header, files=http_body, timeout=timeout)

        if request.get_insert_only() != 0:
            return ret

        if ret[u'code'] == 0:
            return ret

        # try to delete object, and re-post request
        del_request = DelFileRequest(bucket_name=request.get_bucket_name(), cos_path=request.get_cos_path())
        ret = self.del_file(del_request)
        if ret[u'code'] == 0:
            return self.send_request('POST', bucket, cos_path, headers=http_header, files=http_body, timeout=timeout)
        else:
            return ret
Beispiel #14
0
    def download_file(self, request):
        assert isinstance(request, DownloadFileRequest)

        auth = cos_auth.Auth(self._cred)
        sign = auth.sign_download(request.get_bucket_name(), request.get_cos_path(), self._config.get_sign_expired())
        url = self.build_download_url(request.get_bucket_name(), request.get_cos_path(), sign)
        logger.info("Uri is %s" % url)
        try:
            self.__download_url(url, request._local_filename)
            return {u'code': 0, u'message': "download successfully"}
        except Exception as e:
            return {u'code': 1, u'message': "download failed, exception: " + str(e)}
    def upload_single_file_from_buffer(self, request):
        """ 单文件上传

        :param request:
        :return:
        """
        assert isinstance(request, UploadFileFromBufferRequest)
        check_params_ret = self._check_params(request)
        if check_params_ret is not None:
            return check_params_ret

        data = request.get_data()
        file_size = len(data)
        # 判断文件是否超过单文件最大上限, 如果超过则返回错误
        # 并提示用户使用别的接口
        if file_size > self.max_single_file:
            return CosErr.get_err_msg(
                CosErr.NETWORK_ERROR,
                'file is too big, please use upload_file interface')

        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        expired = int(time.time()) + self._expired_period
        sign = auth.sign_more(bucket, cos_path, expired)

        http_header = dict()
        http_header['Authorization'] = sign
        http_header['User-Agent'] = self._config.get_user_agent()

        file_content = data

        http_body = dict()
        http_body['op'] = 'upload'
        http_body['filecontent'] = file_content
        http_body['sha'] = FileOp._sha1_content(file_content)
        http_body['biz_attr'] = request.get_biz_attr()
        http_body['insertOnly'] = str(request.get_insert_only())

        timeout = self._config.get_timeout()
        ret = self.send_request('POST',
                                bucket,
                                cos_path,
                                headers=http_header,
                                files=http_body,
                                timeout=timeout)
        return ret
Beispiel #16
0
    def __move_file(self, request):

        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        sign = auth.sign_once(bucket, cos_path)

        http_header = dict()
        http_header['Authorization'] = sign
        http_header['User-Agent'] = self._config.get_user_agent()

        http_body = dict()
        http_body['op'] = 'move'
        http_body['dest_fileid'] = request.dest_path
        http_body['to_over_write'] = str(1 if request.overwrite else 0)

        timeout = self._config.get_timeout()
        return self.send_request('POST', bucket, cos_path, headers=http_header, params=http_body, timeout=timeout)
Beispiel #17
0
    def _upload_slice_data(self, bucket, cos_path, file_content, session, offset):
        auth     = cos_auth.Auth(self._cred)
        expired  = int(time.time()) + self._expired_period
        sign     = auth.sign_more(bucket, cos_path, expired)

        http_header                  = {}
        http_header['Authorization'] = sign
        http_header['User-Agent']    = self._config.get_user_agent()

        http_body                = {}
        http_body['op']          = 'upload_slice'
        http_body['filecontent'] = file_content
        http_body['session']     = session
        http_body['offset']      = str(offset)

        timeout = self._config.get_timeout()
        return self.send_request('POST', bucket, cos_path, headers=http_header,
                files=http_body, timeout=timeout)
Beispiel #18
0
    def update_file(self, request):
        """更新文件

        :param request:
        :return:
        """
        assert isinstance(request, UpdateFileRequest)
        logger.debug("request: " + str(request.get_custom_headers()))
        check_params_ret = self._check_params(request)
        if check_params_ret is not None:
            return check_params_ret

        logger.debug("params verify successfully")
        auth = cos_auth.Auth(self._cred)
        bucket = request.get_bucket_name()
        cos_path = request.get_cos_path()
        sign = auth.sign_once(bucket, cos_path)

        http_header = dict()
        http_header['Authorization'] = sign
        http_header['Content-Type'] = 'application/json'
        http_header['User-Agent'] = self._config.get_user_agent()

        http_body = dict()
        http_body['op'] = 'update'

        if request.get_biz_attr() is not None:
            http_body['biz_attr'] = request.get_biz_attr()

        if request.get_authority() is not None:
            http_body['authority'] = request.get_authority()

        if request.get_custom_headers() is not None and len(
                request.get_custom_headers()) is not 0:
            http_body['custom_headers'] = request.get_custom_headers()
        logger.debug("Update Request Header: " + json.dumps(http_body))
        timeout = self._config.get_timeout()
        return self.send_request('POST',
                                 bucket,
                                 cos_path,
                                 headers=http_header,
                                 data=json.dumps(http_body),
                                 timeout=timeout)