From 30dd804d4cbe9123d4f435e015c3a2df0df19bf0 Mon Sep 17 00:00:00 2001 From: huangqizhen <15552608129@163.com> Date: Tue, 30 Dec 2025 10:31:08 +0800 Subject: [PATCH] =?UTF-8?q?12.30=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- upload.py | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 upload.py diff --git a/upload.py b/upload.py new file mode 100644 index 0000000..a992522 --- /dev/null +++ b/upload.py @@ -0,0 +1,108 @@ +def apply_lease(client, category_id, file_name, file_md5, file_size, workspace_id): + """ + 从阿里云百炼服务申请文件上传租约。 + + 参数: + client (bailian20231229Client): 客户端(Client)。 + category_id (str): 类目ID。 + file_name (str): 文件名称。 + file_md5 (str): 文件的MD5值。 + file_size (int): 文件大小(以字节为单位)。 + workspace_id (str): 业务空间ID。 + + 返回: + 阿里云百炼服务的响应。 + """ + headers = {} + request = bailian_20231229_models.ApplyFileUploadLeaseRequest( + file_name=file_name, + md_5=file_md5, + size_in_bytes=file_size, + ) + runtime = util_models.RuntimeOptions() + return client.apply_file_upload_lease_with_options(category_id, workspace_id, request, headers, runtime) + + +import requests +from urllib.parse import urlparse + + +def upload_file(pre_signed_url, file_path): + """ + 将本地文件上传至临时存储。 + + 参数: + pre_signed_url (str): 上传租约中的URL。 + file_path (str): 文件本地路径。 + + 返回: + 阿里云百炼服务的响应。 + """ + try: + # 设置请求头 + headers = { + "X-bailian-extra": "请替换为您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param.Headers中X-bailian-extra字段的值", + "Content-Type": "请替换为您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param.Headers中Content-Type字段的值(返回空值时,传空值即可)" + } + + # 读取文件并上传 + with open(file_path, 'rb') as file: + # 下方设置请求方法用于文件上传,需与您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param中Method字段的值一致 + response = requests.put(pre_signed_url, data=file, headers=headers) + + # 检查响应状态码 + if response.status_code == 200: + print("File uploaded successfully.") + else: + print(f"Failed to upload the file. ResponseCode: {response.status_code}") + + except Exception as e: + print(f"An error occurred: {str(e)}") + + +if __name__ == "__main__": + pre_signed_url_or_http_url = "请替换为您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param中Url字段的值" + + # 将本地文件上传至临时存储 + file_path = "请替换为您需要上传文件的实际本地路径(以Linux为例:/xxx/xxx/阿里云百炼系列手机产品介绍.docx)" + upload_file(pre_signed_url_or_http_url, file_path) + + + +def add_file(client: bailian20231229Client, lease_id: str, parser: str, category_id: str, workspace_id: str): + """ + 将文件添加到阿里云百炼服务的指定类目中。 + + 参数: + client (bailian20231229Client): 客户端(Client)。 + lease_id (str): 租约ID。 + parser (str): 用于文件的解析器。 + category_id (str): 类目ID。 + workspace_id (str): 业务空间ID。 + + 返回: + 阿里云百炼服务的响应。 + """ + headers = {} + request = bailian_20231229_models.AddFileRequest( + lease_id=lease_id, + parser=parser, + category_id=category_id, + ) + runtime = util_models.RuntimeOptions() + return client.add_file_with_options(workspace_id, request, headers, runtime) +def describe_file(client, workspace_id, file_id): + """ + 获取文件的基本信息。 + + 参数: + client (bailian20231229Client): 客户端(Client)。 + workspace_id (str): 业务空间ID。 + file_id (str): 文件ID。 + + 返回: + 阿里云百炼服务的响应。 + """ + headers = {} + runtime = util_models.RuntimeOptions() + return client.describe_file_with_options(workspace_id, file_id, headers, runtime) \ No newline at end of file