qcloud_bucket.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding=utf-8
  2. # appid 已在配置中移除,请在参数 Bucket 中带上 appid。Bucket 由 BucketName-APPID 组成
  3. # 1. 设置用户配置, 包括 secretId,secretKey 以及 Region
  4. import datetime
  5. import random
  6. import hashlib
  7. from qcloud_cos import CosConfig
  8. from qcloud_cos import CosS3Client
  9. from qcloud_cos import CosServiceError
  10. from qcloud_cos import CosClientError
  11. import sys
  12. import logging
  13. import requests
  14. import configs
  15. # 日志
  16. image_upload_log = configs.parse_log_dir + "/image_log.txt"
  17. logging.basicConfig(level=logging.INFO,
  18. format="[%(asctime)s, %(levelname)s]-%(filename)s-%(lineno)s: %(message)s",
  19. filename=image_upload_log)
  20. secret_id = "AKIDC9pETRbZfWBbmhoglkT4PUJGzRjmj3Ia" # "云 API 密钥 SecretId";
  21. secret_key = "C6jlX4LKfleGdmfQvGNgj74lESRpBIEJ" # "云 API 密钥 SecretKey";
  22. APPID = '1302712961'
  23. TIMEOUT = 30
  24. token = None # 使用临时密钥需要传入Token,默认为空,可不填
  25. scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填
  26. addr = ".cos." + configs.region + ".myqcloud.com" # 后缀拼接
  27. cos_config = CosConfig(Region=configs.region,
  28. SecretId=secret_id,
  29. SecretKey=secret_key,
  30. Token=token) # 获取配置对象
  31. client = CosS3Client(cos_config) # 获取客户端对象
  32. def upload_img_to_qcloud(param_qcloud):
  33. put_key, localfile = param_qcloud
  34. # 高级上传接口(推荐)
  35. # 根据文件大小自动选择简单上传或分块上传,分块上传具备断点续传功能。
  36. response = client.upload_file(
  37. Bucket=configs.public_bucket,
  38. LocalFilePath=localfile, # 本地文件的路径
  39. Key=put_key, # 上传到桶之后的文件名
  40. # PartSize=10,
  41. PartSize=1, # 上传分成几部分
  42. MAXThread=10, # 支持最多的线程数
  43. EnableMD5=False # 是否支持MD5
  44. )
  45. # print(response['ETag'])
  46. def filestream_upload(param_qcloud):
  47. """
  48. file upload:file byte
  49. 文件流简单上传(不支持超过5G的文件,推荐使用下方高级上传接口)
  50. img upload addrs="http://{}.cos.{}/{}".format(public_bucket,region,ucloud_name)
  51. http://zxhx-pro-1302712961.cos.ap-beijing.myqcloud.com/xueguan/uploadfiles/wording/5fc8c620c6a4dcc23e55a5d0/image4.png"
  52. :return:
  53. """
  54. put_key, fileurl = param_qcloud
  55. filebyte = requests.get(fileurl).content
  56. response = client.put_object(
  57. Bucket=configs.public_bucket,
  58. Body=filebyte,
  59. Key=put_key, # 文件名
  60. StorageClass='STANDARD',
  61. EnableMD5=False
  62. )
  63. def img_inbucket_count(wordid, flag='w'):
  64. """
  65. 统计腾讯云桶中某个wordid下的图片个数
  66. flag:
  67. 'w':word解析中的图片存放的腾讯云地址
  68. 'p':图片、pdf类结构化图片存放的腾讯云地址
  69. 一份试卷在第一步解析中,腾讯云地址只有一种情况
  70. :return:
  71. """
  72. key_d = {"w": "zyk/uploadfiles/wording/", "p": "imgpaper/lqy_upload/"}
  73. try:
  74. obj = client.list_objects(
  75. Bucket=configs.public_bucket,
  76. Prefix='{}'.format(key_d[flag])+ wordid,
  77. # Marker=Marker,
  78. )
  79. if 'Contents' in obj:
  80. return obj['Contents']
  81. except:
  82. pass
  83. return ""
  84. if __name__ == "__main__":
  85. localfile1 = r"C:\Users\Python\Desktop\test\5fc0a554e3e93cca5b9e2193\word\media\image9.png"
  86. put_key2 = "zyk/uploadfiles/wording/5fc0a554e3e93cca5b9e2193/image9.png"
  87. pic_name = "99.png"
  88. put_key1 = "https://" + str(configs.public_bucket) + ".cos." + configs.region + ".myqcloud.com/" + pic_name
  89. # put_key2 = "http://zxhx-1302712961.cos.ap-shanghai.myqcloud.com/" + pic_name
  90. # response = client.upload_file(
  91. # Bucket=public_bucket,
  92. # LocalFilePath=localfile1, # 本地文件的路径
  93. # Key=put_key2, # 上传到桶之后的文件名
  94. # # PartSize=10,
  95. # # MAXThread=10,
  96. # # EnableMD5=False
  97. # )
  98. # print(response)
  99. # print("http://{0}.cos.{1}.myqcloud.com/{2}".format(public_bucket, region, put_key2))
  100. # get_object list_objects
  101. 'http://zxhx-1302712961.cos.ap-shanghai.myqcloud.com/imgpaper/lqy_upload/img_hebei_4.png'
  102. obj = client.list_objects(
  103. Bucket=configs.public_bucket,
  104. # Key=put_key2
  105. # Prefix='zyk/uploadfiles/wording/5fc64a0a4994183dda7e74b9',
  106. Prefix='imgpaper/lqy_upload',
  107. # Marker=Marker,
  108. )
  109. objects = {
  110. "Quiet": "true",
  111. "Object": [item["Key"] for item in obj['Contents']]
  112. }
  113. print(obj['Contents'])
  114. # print(response['ETag']) # 可以与Web资源关联的记号
  115. #### 字节流简单上传
  116. # response = client.put_object(
  117. # Bucket=bucket,
  118. # Body=b'bytes',
  119. # Key='picture.jpg',
  120. # EnableMD5=False
  121. # )
  122. # print(response['ETag'])