"""Stages data from S3 to Snowflake.""" from snowflake import connector from social_analytics import config from social_analytics.connectors import sentry def stage_data_from_s3_to_snowflake(): """Stage data from AWS S3 bucket to snowflake.""" try: snowflake_context = connector.connect( account=config.SNOWFLAKE_ACCOUNT, user=config.SNOWFLAKE_USER, password=config.SNOWFLAKE_PASSWORD, database=config.SNOWFLAKE_DATABASE, schema=config.SNOWFLAKE_SCHEMA, warehouse=config.SNOWFLAKE_WAREHOUSE, role=config.SNOWFLAKE_ROLE ) if config.ENVIRONMENT == config.QA_ENVIRONMENT: snowflake_context.cursor().execute( """copy into FACTS.QA.FACT_SOCIAL_PROFILE_METRICS FROM %s credentials = (aws_key_id=%s, aws_secret_key=%s)""", ( config.AWS_S3_BUCKET, config.AWS_ACCESS_KEY, config.AWS_SECRET_KEY)) elif config.ENVIRONMENT == config.PROD_ENVIRONMENT: snowflake_context.cursor().execute( """copy into FACTS.PROD.FACT_SOCIAL_PROFILE_METRICS FROM %s credentials = (aws_key_id=%s, aws_secret_key=%s)""", ( config.AWS_S3_BUCKET, config.AWS_ACCESS_KEY, config.AWS_SECRET_KEY)) except Exception as ex: sentry_client = sentry.get_client() sentry_client.captureMessage( message='Snowflake staging error', stack=True, extra={ 'message': 'Error during staging', 'errors': str(ex), 'status': 204})