Skip to content

百度网盘API上传空文件报错errno: 2 #264

@lue-trim

Description

@lue-trim

Please make sure of the following things

  • I have read the documentation.
    我已经阅读了文档

  • I'm sure there are no duplicate issues or discussions.
    我确定没有重复的issue或讨论。

  • I'm sure it's due to OpenList and not something else(such as Network ,Dependencies or Operational).
    我确定是OpenList的问题,而不是其他原因(例如网络依赖操作)。

  • I'm sure this issue is not fixed in the latest version.
    我确定这个问题在最新版本中没有被修复。

OpenList Version / OpenList 版本

v4.0.0

Driver used / 使用的存储驱动

百度网盘

Describe the bug / 问题描述

近日发现百度网盘通过API直接上传空文件会报错

{'code': 500, 'message': 'req: [https://pan.baidu.com/rest/2.0/xpan/file] ,errno: 2, refer to https://pan.baidu.com/union/doc/', 'data': None}

百度开放平台官方文档对此错误码的描述是“参数错误”
其他存储还没试过,但是本地存储上传空文件是允许的,可以正常上传,希望可以统一一下

{'code': 200, 'message': 'success', 'data': None}

Reproduction / 复现链接

测试代码如下:

import asyncio, traceback, json
from aiohttp import ClientSession, ClientTimeout
from urllib.parse import quote

class Session():

    async def request(self, **kwargs):
        '发送请求'
        try:
            async with ClientSession(timeout=ClientTimeout(total=None)) as session:
                async with session.request(**kwargs) as res:
                    response = await res.json()
                    assert res.ok
        except Exception:
            print(f"Request Error: {traceback.format_exc()}")
            return {"code":500, "data":None}
        else:
            print(f"Response: {response}")
            return response

    async def get_alist_token(self, settings_alist:dict):
        '获取alist管理token'
        url = f"{settings_alist['url_alist']}/api/auth/login/hash"
        params = {
            "username": settings_alist['username'],
            "password": settings_alist['password'].lower()
        }
        headers = {
            'Content-Type': 'application/json'
        }

        # 请求API
        response_json = await self.request(method="post", url=url, data=json.dumps(params), headers=headers)
        # 获取结果
        if response_json['code'] == 200:
            return response_json['data']['token']
        else:
            return ""

    async def upload_alist(self, settings_alist:dict, token:str, filename:str, dest_filename:str):
        '流式上传文件'
        dest_filename = quote(dest_filename) # URL编码

        # 请求参数
        url = f"{settings_alist['url_alist']}/api/fs/put"
        headers = {
            "Authorization": token,
            "File-Path": dest_filename,
            "As-Task": "True",
            "Content-Type": "application/octet-stream",
        }

        # 打开文件
        with open(filename, 'rb') as f:
            response_json = await self.request(method="put", url=url, data=f, headers=headers)
        
        return response_json

async def __test():
    settings = {
        'url_alist': 'http://localhost:5244',
        'username': '********',
        'password': '********',
        }
    session = Session()

    # 传到百度
    token = await session.get_alist_token(settings)
    dest_filename = "/baidu/test/test.txt"
    # touch empty.txt
    res = await session.upload_alist(settings, token, "empty.txt", dest_filename)

    # 传到本地
    dest_filename = "/local/test/test.txt"
    res = await session.upload_alist(settings, token, "empty.txt", dest_filename)


if __name__ == "__main__":
    asyncio.run(__test())

Config / 配置

百度网盘配置

Image

本地配置

Image

Logs / 日志

上述代码的输出:

Response: {'code': 200, 'message': 'success', 'data': {'token': '**********'}}
Response: {'code': 500, 'message': 'req: [https://pan.baidu.com/rest/2.0/xpan/file] ,errno: 2, refer to https://pan.baidu.com/union/doc/', 'data': None}
Response: {'code': 200, 'message': 'success', 'data': None}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions