Exemple #1
0
def put_file(up_token,
             key,
             file_path,
             params=None,
             mime_type='application/octet-stream',
             check_crc=False,
             progress_handler=None):
    """上传文件到七牛

    Args:
        up_token:         上传凭证
        key:              上传文件名
        file_path:        上传文件的路径
        params:           自定义变量,规格参考 http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
        mime_type:        上传数据的mimeType
        check_crc:        是否校验crc32
        progress_handler: 上传进度

    Returns:
        一个dict变量,类似 {"hash": "<Hash string>", "key": "<Key string>"}
        一个ResponseInfo对象
    """
    ret = {}
    size = os.stat(file_path).st_size
    with open(file_path, 'rb') as input_stream:
        if size > config._BLOCK_SIZE * 2:
            ret, info = put_stream(up_token, key, input_stream, size, params,
                                   mime_type, progress_handler)
        else:
            crc = file_crc32(file_path) if check_crc else None
            ret, info = _form_put(up_token, key, input_stream, params,
                                  mime_type, crc, progress_handler)
    return ret, info
Exemple #2
0
def put_file(up_token, key, file_path, params=None,
             mime_type='application/octet-stream', check_crc=False,
             progress_handler=None, upload_progress_recorder=None):
    """上传文件到七牛

    Args:
        up_token:         上传凭证
        key:              上传文件名
        file_path:        上传文件的路径
        params:           自定义变量,规格参考 http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
        mime_type:        上传数据的mimeType
        check_crc:        是否校验crc32
        progress_handler: 上传进度
        upload_progress_recorder: 记录上传进度,用于断点续传

    Returns:
        一个dict变量,类似 {"hash": "<Hash string>", "key": "<Key string>"}
        一个ResponseInfo对象
    """
    ret = {}
    size = os.stat(file_path).st_size
    with open(file_path, 'rb') as input_stream:
        if size > config._BLOCK_SIZE * 2:
            ret, info = put_stream(up_token, key, input_stream, size, params,
                                   mime_type, progress_handler,
                                   upload_progress_recorder=upload_progress_recorder,
                                   modify_time=(int)(os.path.getmtime(file_path)))
        else:
            crc = file_crc32(file_path) if check_crc else None
            ret, info = _form_put(up_token, key, input_stream, params, mime_type, crc, progress_handler)
    return ret, info
Exemple #3
0
def put_file(up_token, key, file_path, params=None,
             mime_type='application/octet-stream', check_crc=False,
             progress_handler=None, upload_progress_recorder=None):
    """上传文件到七牛

    Args:
        up_token:         上传凭证
        key:              上传文件名
        file_path:        上传文件的路径
        params:           自定义变量,规格参考 http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
        mime_type:        上传数据的mimeType
        check_crc:        是否校验crc32
        progress_handler: 上传进度
        upload_progress_recorder: 记录上传进度,用于断点续传

    Returns:
        一个dict变量,类似 {"hash": "<Hash string>", "key": "<Key string>"}
        一个ResponseInfo对象
    """
    ret = {}
    size = os.stat(file_path).st_size
    # fname = os.path.basename(file_path)
    with open(file_path, 'rb') as input_stream:
        file_name = os.path.basename(file_path)
        if size > config._BLOCK_SIZE * 2:
            ret, info = put_stream(up_token, key, input_stream, file_name, size, params,
                                   mime_type, progress_handler,
                                   upload_progress_recorder=upload_progress_recorder,
                                   modify_time=(int)(os.path.getmtime(file_path)))
        else:
            crc = file_crc32(file_path)
            ret, info = _form_put(up_token, key, input_stream, params, mime_type, crc, progress_handler, file_name)
            # ret, info = _form_put(up_token, key, input_stream, params, mime_type, crc, progress_handler)
    return ret, info
Exemple #4
0
def putfile(
        up_token, key, file_path, params=None, mime_type='application/octet-stream', check_crc=False):
    ''' put data to Qiniu
    If key is None, the server will generate one.
    data may be str or read()able object.
    '''
    crc = file_crc32(file_path) if check_crc else None
    with open(file_path, 'rb') as input_stream:
            return _put(up_token, key, input_stream, params, mime_type, crc, is_file=True)
Exemple #5
0
def put_file(up_token, key, file_path, params=None, mime_type='application/octet-stream', check_crc=False, progress_handler=None):
    ret = {}
    size = os.stat(file_path).st_size
    with open(file_path, 'rb') as input_stream:
        if size > config._BLOCK_SIZE * 2:
            ret, info = put_stream(up_token, key, input_stream, size, params, mime_type, progress_handler)
        else:
            crc = file_crc32(file_path) if check_crc else None
            ret, info = _form_put(up_token, key, input_stream, params, mime_type, crc, True, progress_handler)
    return ret, info
Exemple #6
0
def put_file(up_token,
             key,
             file_path,
             params=None,
             mime_type='application/octet-stream',
             check_crc=False,
             progress_handler=None):
    ret = {}
    size = os.stat(file_path).st_size
    with open(file_path, 'rb') as input_stream:
        if size > config._BLOCK_SIZE * 2:
            ret, info = put_stream(up_token, key, input_stream, size, params,
                                   mime_type, progress_handler)
        else:
            crc = file_crc32(file_path) if check_crc else None
            ret, info = _form_put(up_token, key, input_stream, params,
                                  mime_type, crc, True, progress_handler)
    return ret, info
Exemple #7
0
def putfile(up_token,
            key,
            file_path,
            params=None,
            mime_type='application/octet-stream',
            check_crc=False):
    ''' put data to Qiniu
    If key is None, the server will generate one.
    data may be str or read()able object.
    '''
    crc = file_crc32(file_path) if check_crc else None
    with open(file_path, 'rb') as input_stream:
        return _put(up_token,
                    key,
                    input_stream,
                    params,
                    mime_type,
                    crc,
                    is_file=True)
Exemple #8
0
def put_file(
    up_token,
    key,
    file_path,
    params=None,
    mime_type='application/octet-stream',
    check_crc=False,
    progress_handler=None,
    upload_progress_recorder=None,
    keep_last_modified=False,
    hostscache_dir=None,
    part_size=None,
    version=None,
    bucket_name=None,
    raise_exception=False,
):
    """上传文件到七牛

    Args:
        up_token:                 上传凭证
        key:                      上传文件名
        file_path:                上传文件的路径
        params:                   自定义变量,规格参考 http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
        mime_type:                上传数据的mimeType
        check_crc:                是否校验crc32, 已弃用
        progress_handler:         上传进度
        upload_progress_recorder: 记录上传进度,用于断点续传
        hostscache_dir:          host请求 缓存文件保存位置
        version                   分片上传版本 目前支持v1/v2版本 默认v1
        part_size                 分片上传v2必传字段 默认大小为4MB 分片大小范围为1 MB - 1 GB
        bucket_name               分片上传v2字段 空间名称
        raise_exception:          上传后自动校验key和hash, 如果不一致就报错

    Returns:
        一个dict变量,类似 {"hash": "<Hash string>", "key": "<Key string>"}
        一个ResponseInfo对象
    """
    ret = {}
    size = os.stat(file_path).st_size
    with open(file_path, 'rb') as input_stream:
        file_name = os.path.basename(file_path)
        modify_time = int(os.path.getmtime(file_path))
        if size > config.get_default('default_upload_threshold'):
            ret, info = put_stream(
                up_token,
                key,
                input_stream,
                file_name,
                size,
                hostscache_dir,
                params,
                mime_type,
                progress_handler,
                upload_progress_recorder=upload_progress_recorder,
                modify_time=modify_time,
                keep_last_modified=keep_last_modified,
                part_size=part_size,
                version=version,
                bucket_name=bucket_name)
        else:
            crc = file_crc32(file_path)
            ret, info = _form_put(up_token,
                                  key,
                                  input_stream,
                                  params,
                                  mime_type,
                                  crc,
                                  hostscache_dir,
                                  progress_handler,
                                  file_name,
                                  modify_time=modify_time,
                                  keep_last_modified=keep_last_modified)
    if raise_exception is True:
        if (ret["key"] != key) or (ret["hash"] != etag(file_path)):
            raise UploadException("数据校验不正确")
    return ret, info