先上一张上传成功截图

运行代码如下 

# !/home/***/anaconda3/bin/python #选择合适的python版本
"""
    xpan upload
    include:
        precreate
        upload
        create
"""
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from pprint import pprint
from openapi_client.api import fileupload_api
import openapi_client
import hashlib

def precreate():
    """
    precreate
    """
    #    Enter a context with an instance of the API client
    with openapi_client.ApiClient() as api_client:
        # Create an instance of the API class
        api_instance = fileupload_api.FileuploadApi(api_client)
        access_token = "***"  #填入自己的access_token 
        path = "/apps/***/test.jpg"  # str | 对于一般的第三方软件应用,路径以 "/apps/your-app-name/" 开头。对于小度等硬件应用,路径一般 "/来自:小度设备/" 开头。对于定制化配置的硬件应用,根据配置情况进行填写。
        
        block_list =  ''
        file_path = "/***/test.jpg" #要上传的文件的真实路径
        with open(file_path, 'rb') as f:
            data = f.read()
            file_md5 = hashlib.md5(data).hexdigest() #获取上传文件md5
            block_list =  block_list+'["{}"]'.format(file_md5) #放入block_list
            print(block_list)

        size = os.path.getsize(file_path) #获取上传文件大小
        print(size)

        isdir = 0  # int | isdir 
        autoinit = 1  # int | autoinit
        rtype = 3  # int | rtype (optional)

        try:
            api_response = api_instance.xpanfileprecreate(
                access_token, path, isdir, size, autoinit, block_list, rtype=rtype)
            pprint(api_response)
            uploadid = api_response['uploadid'] #获取预上传返回的uploadid,传给upload和create函数
        except openapi_client.ApiException as e:
            print("Exception when calling FileuploadApi->xpanfileprecreate: %s\n" % e)
        return access_token, path, isdir, size, uploadid, block_list, rtype, file_path


def upload(uploadid,path,file_path, access_token):
    """
    upload
    """
    # Enter a context with an instance of the API client
    with openapi_client.ApiClient() as api_client:
        # Create an instance of the API class
        api_instance = fileupload_api.FileuploadApi(api_client)
        partseq = "0"  # str |
        type = "tmpfile"  # str |
        try:
            file = open(file_path, 'rb') 
        except Exception as e:
            print("Exception when open file: %s\n" % e)
            exit(-1)

        # example passing only required values which don't have defaults set
        # and optional values
        try:
            api_response = api_instance.pcssuperfile2(
                access_token, partseq, path, uploadid, type, file=file)
            pprint(api_response)
        except openapi_client.ApiException as e:
            print("Exception when calling FileuploadApi->pcssuperfile2: %s\n" % e)


def create(access_token, path, isdir, size, uploadid, block_list,rtype):
    """
    create
    """
    # Enter a context with an instance of the API client
    with openapi_client.ApiClient() as api_client:
        # Create an instance of the API class
        api_instance = fileupload_api.FileuploadApi(api_client)

        # example passing only required values which don't have defaults set
        # and optional values
        try:
            api_response = api_instance.xpanfilecreate(
                access_token, path, isdir, size, uploadid, block_list, rtype=rtype)
            pprint(api_response)
        except openapi_client.ApiException as e:
            print("Exception when calling FileuploadApi->xpanfilecreate: %s\n" % e)


if __name__ == '__main__':
    access_token, path, isdir, size, uploadid, block_list, rtype, file_path = precreate()
    upload(uploadid,path,file_path, access_token)
    create(access_token, path, isdir, size, uploadid, block_list,rtype)

把access_token,文件路径,上传路径修改为自己的,运行即可

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐