Browse Source

12.30上传文件

master
huangqizhen 1 week ago
commit
30dd804d4c
  1. 108
      upload.py

108
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)
Loading…
Cancel
Save