boto-updater.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import boto3
  2. import os.path
  3. # Arguments
  4. folder_to_sync = './test/data'
  5. bucket_target = 'my-bucket'
  6. # Create connection
  7. s3 = boto3.client('s3', use_ssl=False, endpoint_url="http://172.17.0.2:9000", aws_access_key_id="minio", aws_secret_access_key="miniokey")
  8. # Reset bucket
  9. # Get file list
  10. # Source : https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory
  11. local_file_list = os.listdir(folder_to_sync)
  12. bucket_file_list = [obj['Key'] for obj in s3.list_objects(Bucket=bucket_target)['Contents']]
  13. # Upload
  14. print("Uploading new files ...")
  15. for file_name in local_file_list:
  16. print(file_name, end=' ')
  17. file_path = folder_to_sync + '/' + file_name
  18. if file_name in bucket_file_list:
  19. # Todo : compare modification dates
  20. print('(Not updated)')
  21. elif os.path.isfile(file_path):
  22. res = s3.upload_file(file_path, bucket_target, file_name)
  23. print('(New file)')
  24. else:
  25. print('(Skipped)')
  26. print("Done")
  27. # Delete files
  28. print("Deleting removed files ...")
  29. for file_name in bucket_file_list:
  30. print(file_name, end=' ')
  31. if file_name in local_file_list:
  32. print('(Still present)')
  33. else:
  34. s3.delete_object(Bucket=bucket_target, Key=file_name)
  35. print('(Deleted)')
  36. print("Done")