clean_unfinished_multipart_upload.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import boto3, json
  2. # Get unfinished multipart upload id from s3
  3. def get_uploaded_list(s3_client, Des_bucket, Des_key, MaxRetry):
  4. NextKeyMarker = ''
  5. IsTruncated = True
  6. multipart_uploaded_list = []
  7. while IsTruncated:
  8. IsTruncated = False
  9. for retry in range(MaxRetry + 1):
  10. try:
  11. print(f'Getting unfinished upload id list {retry} retry {Des_bucket}/{Des_key}...')
  12. list_multipart_uploads = s3_client.list_multipart_uploads(
  13. Bucket=Des_bucket,
  14. Prefix=Des_key,
  15. MaxUploads=1000,
  16. KeyMarker=NextKeyMarker
  17. )
  18. IsTruncated = list_multipart_uploads["IsTruncated"]
  19. NextKeyMarker = list_multipart_uploads["NextKeyMarker"]
  20. if "Uploads" in list_multipart_uploads:
  21. for i in list_multipart_uploads["Uploads"]:
  22. if i["Key"] == Des_key:
  23. multipart_uploaded_list.append({
  24. "Key": i["Key"],
  25. "Initiated": i["Initiated"],
  26. "UploadId": i["UploadId"]
  27. })
  28. print(f'Unfinished upload, Key: {i["Key"]}, Time: {i["Initiated"]}')
  29. break # 退出重试循环
  30. except Exception as e:
  31. print(f'Fail to list multipart upload {str(e)}')
  32. if retry >= MaxRetry:
  33. print(f'Fail MaxRetry list multipart upload {str(e)}')
  34. return []
  35. else:
  36. time.sleep(5 * retry)
  37. return multipart_uploaded_list
  38. # Main
  39. if __name__ == '__main__':
  40. session = boto3.session.Session(profile_name='zhy')
  41. s3_des_client = session.client('s3')
  42. Des_bucket = 's3-migration-test-nx'
  43. multipart_uploaded_list = get_uploaded_list(s3_des_client, Des_bucket, "", 3)
  44. for clean_i in multipart_uploaded_list:
  45. try:
  46. s3_des_client.abort_multipart_upload(
  47. Bucket=Des_bucket,
  48. Key=clean_i["Key"],
  49. UploadId=clean_i["UploadId"]
  50. )
  51. except Exception as e:
  52. print(f'Fail to clean {str(e)}')
  53. multipart_uploaded_list = []
  54. print('CLEAN UNFINISHED UPLOAD FINISHED: ', Des_bucket)