s3grouper_2.py 1.4 KB

1234567891011121314151617181920212223242526272829
  1. def s3_upload(s3,data,target,out_index_num,norr, extension=".index"):
  2. keyname = '/'.join([target['prefix'],target['key']+'.'+str(out_index_num)+extension])
  3. s3uri = 's3://'+target['bucket']+'/'+keyname
  4. data.seek(0)
  5. extra_args = {'StorageClass':'REDUCED_REDUNDANCY'}
  6. if norr:
  7. extra_args = {}
  8. try:
  9. # HeadObject to see if exist (needs ListBucket or this returns 403)
  10. s3.Object(target['bucket'], keyname).load()
  11. logger.info("{} exist. Skipping...".format(s3uri))
  12. except ClientError as ce: # if does not exist, then upload
  13. if ce.response["Error"]["Code"] == "404":
  14. try:
  15. s3.Bucket(target['bucket']).upload_fileobj(data,keyname,ExtraArgs=extra_args)
  16. except ClientError as e:
  17. logger.error('Unable to upload to {}'.format(s3uri))
  18. logger.debug('Received error: {}'.format(e))
  19. sys.exit(4)
  20. logger.info("Uploaded to {}".format(s3uri))
  21. elif ce.response["Error"]["Code"] == "403":
  22. logger.error('Permission error loading {}\n Please check your IAM policy.'.format(s3uri))
  23. logger.debug('Received error: {}'.format(ce))
  24. sys.exit(7)
  25. else:
  26. logger.error('Unknown error loading {}'.format(s3uri))
  27. logger.debug('Received error: {}'.format(ce))
  28. sys.exit(7)
  29. return