Last active
August 5, 2021 19:57
-
-
Save swanjson/a9d2271ab01dfdd42d2c6772630ee802 to your computer and use it in GitHub Desktop.
Local Blob Upload
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Functionalized Write to Blob for dealing with several files:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import os #Used to access environment variables\n", | |
"from azure.storage.blob import BlobServiceClient, __version__" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"container_name = 'dealinput' #NAME OF CONTAINER TO WRITE TO (example: 'dealinput')\n", | |
"blob_dir = 'ExtractMapTaxonomyStep' #PATH TO BLOB LOCATION WHERE CSV WILL BE UPLOADED (example: 'AEDW_Updates_Raw')\n", | |
"connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')\n", | |
"# print(connect_str) #TO MAKE SURE CONNECTION STRING IS WORKING\n", | |
"\n", | |
"# Create the BlobServiceClient object which will be used to get a blob client\n", | |
"blob_service_client = BlobServiceClient.from_connection_string(connect_str)\n", | |
"\n", | |
"def writeToBlob(newFileToBeUploaded, newNewBlobFileName): #ARGS ARE (1(Path to the file on local storage), 2(name you wish file to appear as on blob storage))\n", | |
" fileToBeUploaded, newBlobFileName = '',''\n", | |
" print('setting up names...')\n", | |
" fileToBeUploaded = newFileToBeUploaded #PATH TO FILE WHERE INPUT CSV IS LOCATED (example: './featureMatrix.csv', if located in local storage. One can also upload a written csv/parquet from a python file) \n", | |
" newBlobFileName = newNewBlobFileName #WHAT YOU WANT THE FILE TO BE NAMED ON BLOB (example: 'newCSVonBlob.csv')\n", | |
" completeBlobPathWithFileName = blob_dir + '/' + newBlobFileName\n", | |
" blob_client = blob_service_client.get_blob_client(container=container_name, blob=completeBlobPathWithFileName)\n", | |
" print('fileToBeUploaded: ' + fileToBeUploaded)\n", | |
" print('newBlobFileName: ' + newBlobFileName)\n", | |
" print('finshing naming...')\n", | |
" # Create a blob client using the local file name as the name for the blob\n", | |
" blob_client = blob_service_client.get_blob_client(container=container_name, blob=completeBlobPathWithFileName)\n", | |
" with open(fileToBeUploaded, \"rb\") as data:\n", | |
" print('writing to blob...')\n", | |
" blob_client.upload_blob(data, overwrite=True)\n", | |
" print('DONE! Written to blob.')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"setting up names...\n", | |
"fileToBeUploaded: ./temp/AEDW_Updates.parquet\n", | |
"newBlobFileName: AEDW_Updates.parquet\n", | |
"finshing naming...\n", | |
"writing to blob...\n", | |
"DONE! Written to blob.\n" | |
] | |
} | |
], | |
"source": [ | |
"pathToLocalFile = './temp/AEDW_Updates.parquet'\n", | |
"fileToBeNamedOnBlob = 'AEDW_Updates.parquet'\n", | |
"writeToBlob(pathToLocalFile, fileToBeNamedOnBlob)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python (deal)", | |
"language": "python", | |
"name": "deal" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment