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

upload.py 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding=utf-8
  2. from qcloud_cos import CosConfig
  3. from qcloud_cos import CosS3Client
  4. import sys
  5. import os
  6. import logging
  7. import getopt
  8. import hashlib
  9. import json
  10. def upload(path):
  11. response = client.upload_file(
  12. Bucket='thuai6-1314234950',
  13. LocalFilePath= path,
  14. Key = "TEST",
  15. PartSize=1,
  16. MAXThread=10,
  17. EnableMD5=False
  18. )
  19. return response
  20. def generateHashFile():
  21. file = targetfolder
  22. hashlist={}
  23. for root, dirs, files in os.walk(file):
  24. for file in files:
  25. path = os.path.join(root, file)
  26. with open(path, 'rb') as f:
  27. data = f.read()
  28. hashlist[path.replace(file)]=hashlib.md5(data)
  29. res = json.dumps(hashlist)
  30. try:
  31. opts,args = getopt.getopt(sys.argv[1:],"t:i:k:")
  32. except getopt.GetoptError:
  33. print("upload.py -d<targetfolder> -i<SecretID> -k<SecretKey>")
  34. sKey=""
  35. sId=""
  36. targetfolder=""
  37. for opt,arg in opts:
  38. if opt == "-t":
  39. targetfolder = arg
  40. elif opt == "-i":
  41. sId = arg
  42. elif opt == "-k":
  43. sKey = arg
  44. print("skey:"+sKey+" sid:"+sId+" tar:"+targetfolder)
  45. # 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息
  46. logging.basicConfig(level=logging.INFO, stream=sys.stdout)
  47. # 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成
  48. secret_id = sId # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
  49. secret_key = sKey # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
  50. region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
  51. # COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224
  52. token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048
  53. scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填
  54. config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
  55. client = CosS3Client(config)
  56. generateHashFile()
  57. #### 高级上传接口(推荐)
  58. # 根据文件大小自动选择简单上传或分块上传,分块上传具备断点续传功能。