supload.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import boto3
  2. import json
  3. import os
  4. from botocore.exceptions import ClientError
  5. import sys
  6. import time
  7. def upload_file(file_name,in_sub_folder,bucket_name,supress_print=True):
  8. client = boto3.client('s3')
  9. fname = os.path.basename(file_name)
  10. str_dt = time.strftime("%m%d%Y")
  11. key = f'{in_sub_folder}/{str_dt}/{fname}'
  12. s3_url = f's3://{bucket_name}/{key}'
  13. try:
  14. client.upload_file(file_name, Bucket=bucket_name ,Key=key)
  15. if not supress_print:
  16. print(f'{fname}--->{key}')
  17. return True,s3_url
  18. except Exception as e:
  19. print(e)
  20. return False,s3_url
  21. def is_already_exist(file_name,in_sub_folder,bucket_name):
  22. fname = os.path.basename(file_name)
  23. str_dt = time.strftime("%m%d%Y")
  24. key = f'{in_sub_folder}/{str_dt}/{fname}'
  25. client = boto3.client('s3')
  26. try:
  27. client.head_object(Bucket=bucket_name,Key=key)
  28. except ClientError as e:
  29. return int(e.response['Error']['Code']) != 404
  30. return True
  31. if __name__ == "__main__":
  32. print(upload_file('tes.txt','kapowautostorerhoaiindia/google-scrape/05202020','rhoaiautomationindias3'))