13-s3.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import logging
  2. import botocore
  3. import boto3
  4. from flask import current_app as app, g
  5. logger = logging.getLogger(__name__)
  6. def s3():
  7. if getattr(g, 's3', None):
  8. return g.s3
  9. g.s3 = boto3.client(
  10. "s3",
  11. aws_access_key_id=app.config['S3_KEY'],
  12. aws_secret_access_key=app.config['S3_SECRET'],
  13. )
  14. return g.s3
  15. def s3_resource():
  16. if getattr(g, 's3_resource', None):
  17. return g.s3_resource
  18. g.s3_resource = boto3.resource(
  19. "s3",
  20. aws_access_key_id=app.config['S3_KEY'],
  21. aws_secret_access_key=app.config['S3_SECRET'],
  22. )
  23. return g.s3_resource
  24. def get_s3_file_public_url(filename):
  25. """
  26. Get public URL or return None if does not exist.
  27. """
  28. try:
  29. s3().head_object(Bucket=app.config['S3_BUCKET'], Key=filename)
  30. except botocore.exceptions.ClientError as error:
  31. error_code = int(error.response['Error']['Code'])
  32. if error_code == 404:
  33. return None
  34. app.logger.exception('Failed to obtain information about %s '
  35. 'from S3', filename)
  36. raise
  37. return '{}{}'.format(app.config['S3_LOCATION'], filename)
  38. def upload_mp4_video_to_s3(file_path, filename):
  39. app.logger.info('Started upload of %s to S3', file_path)
  40. args = {
  41. 'ACL': 'public-read',
  42. 'ContentType': 'video/mp4',
  43. }
  44. try:
  45. s3().upload_file(file_path, app.config['S3_BUCKET'], filename,
  46. ExtraArgs=args)
  47. app.logger.info('Uploaded %s to S3', file_path)
  48. except (boto3.exceptions.S3UploadFailedError,
  49. botocore.exceptions.ClientError):
  50. app.logger.exception('Failed upload of %s to S3', file_path)
  51. raise
  52. def flush_videos_from_s3():
  53. app.logger.info('Flushing objects from S3 bucket %s',
  54. app.config['S3_BUCKET'])
  55. s3_resource().Bucket(app.config['S3_BUCKET']).objects.delete()
  56. def upload_jpeg_image_to_s3(file_path, filename):
  57. app.logger.info('Started upload of %s to S3', file_path)
  58. args = {
  59. 'ACL': 'public-read',
  60. 'ContentType': 'image/jpeg',
  61. }
  62. try:
  63. s3().upload_file(file_path, app.config['S3_BUCKET'], filename,
  64. ExtraArgs=args)
  65. app.logger.info('Uploaded %s to S3', file_path)
  66. except (boto3.exceptions.S3UploadFailedError,
  67. botocore.exceptions.ClientError):
  68. app.logger.exception('Failed upload of %s to S3', file_path)
  69. raise