ossUtils.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import oss2
  2. import traceback
  3. from BaseDataMaintenance.common.Utils import log
  4. import time
  5. import os
  6. current_path = os.path.dirname(__file__)
  7. def does_bucket_exist(bucket):
  8. try:
  9. bucket.get_bucket_info()
  10. except oss2.exceptions.NoSuchBucket:
  11. return False
  12. except:
  13. raise
  14. return True
  15. import hashlib
  16. def getMDFFromFile(path):
  17. _length = 0
  18. try:
  19. _md5 = hashlib.md5()
  20. with open(path,"rb") as f:
  21. while True:
  22. data = f.read(4096)
  23. if not data:
  24. break
  25. _length += len(data)
  26. _md5.update(data)
  27. return _md5.hexdigest(),_length
  28. except Exception as e:
  29. traceback.print_exc()
  30. return None,_length
  31. def uploadFileByPath(bucket,filepath,uploadpath,headers=None):
  32. try:
  33. start_time = time.time()
  34. log("uploading file of %s"%filepath)
  35. with open(filepath,"rb") as f:
  36. bucket.put_object(uploadpath,f,headers=headers)
  37. log("upload file of %s takes %ds"%(filepath,time.time()-start_time))
  38. return True
  39. except Exception as e:
  40. traceback.print_exc()
  41. log("upload object failed of %s"%(filepath))
  42. return False
  43. def deleteObject(bucket,objectName):
  44. try:
  45. bucket.delete_object(objectName)
  46. except Exception as e:
  47. log("delete object failed of %s"%objectName)
  48. def downloadFile(bucket,objectPath,localPath,retry=3):
  49. for i in range(retry):
  50. try:
  51. # bucket.get_object_to_file(objectPath, localPath)
  52. oss2.resumable_download(bucket, objectPath, localPath,
  53. store=oss2.ResumableDownloadStore(),
  54. num_threads=1)
  55. return True
  56. except oss2.exceptions.NotFound as e:
  57. log("file not found:%s"%(objectPath))
  58. raise e
  59. except Exception as e:
  60. traceback.print_exc()
  61. log("download object failed of %s"%str(objectPath))
  62. return False
  63. def getBucketConnect():
  64. from BaseDataMaintenance.dataSource.source import getAuth,getConnect_ots,is_internal
  65. auth = getAuth()
  66. if is_internal:
  67. bucket_url = "http://oss-cn-hangzhou-internal.aliyuncs.com"
  68. else:
  69. bucket_url = "http://oss-cn-hangzhou.aliyuncs.com"
  70. attachment_bucket_name = "attachment-hub"
  71. log("-----===---bucket_url:%s"%(bucket_url))
  72. bucket = oss2.Bucket(auth,bucket_url,attachment_bucket_name)
  73. ots_client = getConnect_ots()
  74. return bucket,ots_client
  75. bucket,ots_client = getBucketConnect()
  76. def test_download(filemd5):
  77. from BaseDataMaintenance.model.ots.attachment import attachment,attachment_filemd5,attachment_path,attachment_filetype
  78. atta = attachment({attachment_filemd5:filemd5})
  79. atta.fix_columns(ots_client,[attachment_path,attachment_filetype],True)
  80. _filetype = atta.getProperties().get(attachment_filetype)
  81. _path = "%s/%s/%s.%s"%(os.path.dirname(__file__),"download",filemd5,_filetype)
  82. object_path = atta.getProperties().get(attachment_path)
  83. try:
  84. downloadFile(bucket,object_path,_path)
  85. except Exception as e:
  86. print(str(e))
  87. return _path
  88. if __name__=="__main__":
  89. # print(getMDFFromFile('1623894475151.pdf'))
  90. # test_download("0852ca62c6e3da56a89a02ed4af87724")
  91. print(bucket.sign_url("GET","0015//20220623/2022-06-22/WGH001018/1655926900020.png",86500*30))
  92. print(time.strftime("%Y-%m-%d",time.localtime(1658655178)))