-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from oislen/dev
27 boto3 s3 client
- Loading branch information
Showing
18 changed files
with
388 additions
and
299 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
call python webscraper\prg_webscrape_data.py --retrieve_data --generate_master_data --generate_preaggregated_data --generate_counties_data --generate_stations_data | ||
call python webscraper\prg_webscrape_data.py --run_met_data --run_clean_data --run_master_data --run_preagg_data --run_map_data --run_points_data |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import io | ||
import boto3 | ||
import json | ||
import logging | ||
import pandas as pd | ||
from typing import Union | ||
from beartype import beartype | ||
|
||
class S3Client(): | ||
|
||
@beartype | ||
def __init__(self, sessionToken:str): | ||
# load aws config | ||
with open(sessionToken, "r") as j: | ||
aws_config = json.loads(j.read()) | ||
# connect to aws boto3 | ||
self.session = boto3.Session( | ||
aws_access_key_id=aws_config['Credentials']["AccessKeyId"], | ||
aws_secret_access_key=aws_config['Credentials']["SecretAccessKey"], | ||
aws_session_token=aws_config['Credentials']["SessionToken"], | ||
region_name="eu-west-1" | ||
) | ||
# generate boto3 s3 connection | ||
self.client = self.session.client("s3") | ||
|
||
@beartype | ||
def store( | ||
self, | ||
data:pd.DataFrame, | ||
key:str, | ||
bucket:str="irishclimateapp" | ||
): | ||
"""Stores a raw Met Eireann data file on s3. | ||
Parameters | ||
---------- | ||
directory : str | ||
The s3 key to store the Met Eireann data files | ||
bucket : str | ||
The s3 bucket storing the Met Eireann data files | ||
Returns | ||
------- | ||
""" | ||
try: | ||
logging.info(f"Storing data to S3://{bucket}/{key}") | ||
csv_buf = io.StringIO() | ||
data.to_csv(csv_buf, header=True, index=False) | ||
csv_buf.seek(0) | ||
self.client.put_object(Bucket=bucket, Body=csv_buf.getvalue(), Key=key) | ||
except Exception as e: | ||
logging.info(str(e)) | ||
|
||
@beartype | ||
def retrieve( | ||
self, | ||
key:str, | ||
bucket:str="irishclimateapp" | ||
): | ||
|
||
"""Retrieves a raw Met Eireann data from AWS s3. | ||
Parameters | ||
---------- | ||
key : str | ||
The s3 key containing the Met Eireann data file | ||
bucket : str | ||
The s3 bucket containing the Met Eireann data file | ||
Returns | ||
------- | ||
The raw Met Eireann data | ||
""" | ||
data = None | ||
try: | ||
logging.info(f"Retrieving data from S3://{bucket}/{key}") | ||
# load s3 objects into list | ||
obj = self.client.get_object(Bucket=bucket, Key=key) | ||
# decode xlsx files in body | ||
data = pd.read_csv(obj["Body"]) | ||
except Exception as e: | ||
logging.info(str(e)) | ||
return data |
This file contains 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
Oops, something went wrong.