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