Created
February 4, 2020 16:33
-
-
Save lclpedro/b5181fab2b148937a5bf27b37a1a0526 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from src.services.bucket_s3 import BucketS3 | |
def use_service(): | |
files = BucketS3().get_read_bucket('<NOME_DO_BUCKET>') | |
print(files) | |
return files |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# !/usr/bin/env python3 | |
"""Singleton | |
Módulo que define o tipo Singleton para ser usado como metaclass | |
em outras classes do projeto | |
""" | |
class Singleton(type): | |
""" | |
Singleton é o tipo a ser usado como metaclass de um objeto | |
que se deseja ter apenas uma instância presenta durante a execução | |
da aplicação | |
""" | |
def __init__(cls, name, bases, attrs, **kwargs): | |
super().__init__(name, bases, attrs) | |
cls._instance = None | |
def __call__(cls, *args, **kwargs): | |
if cls._instance is None: | |
cls._instance = super().__call__(*args, **kwargs) | |
return cls._instance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import boto3 | |
from botocore.exceptions import ClientError, ParamValidationError | |
from src.metaclass.singleton import Singleton | |
class BucketS3(metaclass=Singleton): | |
def __init__(self): | |
region_default = os.envioronment.get('AWS_REGION_DEFAULT') | |
self.S3 = boto3.client( | |
's3', | |
region_name=region_default, | |
# access_key = os.environment.get('access_key') | |
# secret_key = os.environment.get('secret_key') | |
) | |
def get_read_bucket(self, bucket_name): | |
obj = self.S3.get_object( | |
Bucket=self.bucket_name, | |
#Key=str(key_bucket) | |
) | |
return obj | |
except (ClientError, IndexError, ParamValidationError): | |
raise BucketS3Exception(f'Data not available') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment