You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.8 KiB

  1. def apply_lease(client, category_id, file_name, file_md5, file_size, workspace_id):
  2. """
  3. :
  4. client (bailian20231229Client): Client
  5. category_id (str): ID
  6. file_name (str):
  7. file_md5 (str): MD5值
  8. file_size (int):
  9. workspace_id (str): ID
  10. :
  11. """
  12. headers = {}
  13. request = bailian_20231229_models.ApplyFileUploadLeaseRequest(
  14. file_name=file_name,
  15. md_5=file_md5,
  16. size_in_bytes=file_size,
  17. )
  18. runtime = util_models.RuntimeOptions()
  19. return client.apply_file_upload_lease_with_options(category_id, workspace_id, request, headers, runtime)
  20. import requests
  21. from urllib.parse import urlparse
  22. def upload_file(pre_signed_url, file_path):
  23. """
  24. :
  25. pre_signed_url (str): URL
  26. file_path (str):
  27. :
  28. """
  29. try:
  30. # 设置请求头
  31. headers = {
  32. "X-bailian-extra": "请替换为您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param.Headers中X-bailian-extra字段的值",
  33. "Content-Type": "请替换为您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param.Headers中Content-Type字段的值(返回空值时,传空值即可)"
  34. }
  35. # 读取文件并上传
  36. with open(file_path, 'rb') as file:
  37. # 下方设置请求方法用于文件上传,需与您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param中Method字段的值一致
  38. response = requests.put(pre_signed_url, data=file, headers=headers)
  39. # 检查响应状态码
  40. if response.status_code == 200:
  41. print("File uploaded successfully.")
  42. else:
  43. print(f"Failed to upload the file. ResponseCode: {response.status_code}")
  44. except Exception as e:
  45. print(f"An error occurred: {str(e)}")
  46. if __name__ == "__main__":
  47. pre_signed_url_or_http_url = "请替换为您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param中Url字段的值"
  48. # 将本地文件上传至临时存储
  49. file_path = "请替换为您需要上传文件的实际本地路径(以Linux为例:/xxx/xxx/阿里云百炼系列手机产品介绍.docx)"
  50. upload_file(pre_signed_url_or_http_url, file_path)
  51. def add_file(client: bailian20231229Client, lease_id: str, parser: str, category_id: str, workspace_id: str):
  52. """
  53. :
  54. client (bailian20231229Client): Client
  55. lease_id (str): ID
  56. parser (str):
  57. category_id (str): ID
  58. workspace_id (str): ID
  59. :
  60. """
  61. headers = {}
  62. request = bailian_20231229_models.AddFileRequest(
  63. lease_id=lease_id,
  64. parser=parser,
  65. category_id=category_id,
  66. )
  67. runtime = util_models.RuntimeOptions()
  68. return client.add_file_with_options(workspace_id, request, headers, runtime)
  69. def describe_file(client, workspace_id, file_id):
  70. """
  71. :
  72. client (bailian20231229Client): Client
  73. workspace_id (str): ID
  74. file_id (str): ID
  75. :
  76. """
  77. headers = {}
  78. runtime = util_models.RuntimeOptions()
  79. return client.describe_file_with_options(workspace_id, file_id, headers, runtime)