Created
April 24, 2022 08:28
-
-
Save h3xagn/7e98c11ec710a009e82a261557c3b947 to your computer and use it in GitHub Desktop.
Build ETL from device to cloud: https://h3xagn.com
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
| def upload_data_to_azure(json_filename, json_data, transformed_filename, transformed_data): | |
| """Upload to Azure Blob Storage""" | |
| # set upload flags to False | |
| has_json_uploaded = False | |
| has_csvgz_uploaded = False | |
| # get connection string from .env file | |
| connect_str = os.getenv("AZURE_STORAGE_CONNECTION_STRING") | |
| # create a blob client using the local file name as the name for the blob | |
| blob_service_client = BlobServiceClient.from_connection_string(connect_str) | |
| # define blob container and paths | |
| blob_client_json = blob_service_client.get_blob_client(container="device-data", blob=f"raw/{json_filename}") | |
| blob_client_csvgz = blob_service_client.get_blob_client(container="device-data", blob=f"processed/{transformed_filename}") | |
| # upload raw JSON file | |
| try: | |
| json_content_setting = ContentSettings(content_type="application/json") | |
| blob_client_json.upload_blob(json.dumps(json_data, ensure_ascii=False).encode("utf-8"), overwrite=True, content_settings=json_content_setting) | |
| has_json_uploaded = True | |
| log.info(f"--- JSON file uploaded to Azure: '{json_filename}'.") | |
| except: | |
| log.error(f"*** JSON file NOT uploaded to Azure: '{json_filename}'.") | |
| # upload processed compressed CSV file | |
| try: | |
| csvgz_content_setting = ContentSettings(content_type="application/x-gzip") | |
| # TODO: Investigate directly uploading transformed_data data frame | |
| # read compressed CSV file from disk | |
| with open(f"{base_dir}/data/processed/{transformed_filename}", "rb") as data: | |
| blob_client_csvgz.upload_blob(data, overwrite=True, content_settings=csvgz_content_setting) | |
| has_csvgz_uploaded = True | |
| log.info(f"--- Transformed file uploaded to Azure: '{transformed_filename}'.") | |
| except: | |
| log.error(f"*** Transformed file NOT uploaded to Azure: '{transformed_filename}'.") | |
| return has_json_uploaded, has_csvgz_uploaded |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment