qcloud_bucket.py 5.0 KB

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