Last active
October 8, 2021 17:43
-
-
Save leocosta037/d31bbc5c1d875e2563bf3878e7cd6290 to your computer and use it in GitHub Desktop.
TechDay_UPE_Acesso_Azure.ipynb
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "TechDay_UPE_Acesso_Azure.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"toc_visible": true, | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/leocosta037/d31bbc5c1d875e2563bf3878e7cd6290/techday_upe_acesso_azure.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "HxQXiY7uQTQE" | |
}, | |
"source": [ | |
"# Notebook para acesso ao Storage de Blobs do Azure" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Mo4kEG_yQ1X-" | |
}, | |
"source": [ | |
"Material para consulta" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "UH6IvEyRQ15V" | |
}, | |
"source": [ | |
"https://docs.microsoft.com/pt-br/azure/storage/blobs/storage-quickstart-blobs-python\n", | |
"\n", | |
"https://docs.microsoft.com/pt-br/azure/storage/blobs/storage-blobs-introduction\n", | |
"\n", | |
"https://pypi.org/project/azure-storage-blob/\n", | |
"\n", | |
"https://docs.microsoft.com/pt-br/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient?view=azure-python\n", | |
"\n", | |
"https://docs.microsoft.com/pt-br/python/api/azure-storage-blob/azure.storage.blob.blobclient?view=azure-python#upload-blob-data--blob-type--blobtype-blockblob---blockblob----length-none--metadata-none----kwargs-\n", | |
"\n", | |
"https://github.com/Azure/azure-storage-python/tree/master/azure-storage-blob\n", | |
"\n", | |
"https://medium.com/microsoftazure/guidance-for-using-azure-storage-explorer-with-azure-ad-authorization-for-azure-storage-data-access-663c2c88efb" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "jXwXFX9R_wvc" | |
}, | |
"source": [ | |
"# Inicialização" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "AaVu6PG-FhAt" | |
}, | |
"source": [ | |
"pip install azure-storage-blob" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VoWRY0YbDdb2" | |
}, | |
"source": [ | |
"import os, uuid\n", | |
"from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__\n", | |
"\n", | |
"try:\n", | |
" print(\"Azure Blob Storage v\" + __version__ + \" - Python quickstart sample\")\n", | |
"\n", | |
" # Quick start code goes here\n", | |
"\n", | |
"except Exception as ex:\n", | |
" print('Exception:')\n", | |
" print(ex)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "lKDkhmdOU1d3" | |
}, | |
"source": [ | |
"# Importação do Google Drive\n", | |
"from google.colab import drive\n", | |
"drive.mount('/content/drive')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "9i2FR-qNm3Uy" | |
}, | |
"source": [ | |
"# Carregamento" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "mlKjHP5zJoNw" | |
}, | |
"source": [ | |
"AZURE_STORAGE_CONNECTION_STRING='' # Inserir string de conexão do Azure Storage" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "8YsyjqQYJKe9" | |
}, | |
"source": [ | |
"# Create the BlobServiceClient object which will be used to create a container client\n", | |
"blob_service_client = BlobServiceClient.from_connection_string(AZURE_STORAGE_CONNECTION_STRING)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "KeLIVoJPmx5f" | |
}, | |
"source": [ | |
"# Funções" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TEPDi2YBNmuC" | |
}, | |
"source": [ | |
"# Create a unique name for the container\n", | |
"container_name = str('') # Inserir o nome do contâiner a ser criado\n", | |
"\n", | |
"# Create the container\n", | |
"container_client = blob_service_client.create_container(container_name)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Fn7IQU08Lx6-" | |
}, | |
"source": [ | |
"# Create a local directory to hold blob data\n", | |
"local_path = \"\" # Inserir o caminho da pasta onde se localiza o arquivo no PC\n", | |
"#os.mkdir(local_path)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "uCt5UC8uOKj3" | |
}, | |
"source": [ | |
"# Create a file in the local data directory to upload and download\n", | |
"#local_file_name = local_path + '/' + file\n", | |
"local_file_name = '' # Inserir o nome do arquivo no PC\n", | |
"upload_file_path = os.path.join(local_path, local_file_name)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "FXM74BuIOWs4" | |
}, | |
"source": [ | |
"'''\n", | |
"# Write text to the file\n", | |
"file = open(upload_file_path, 'w')\n", | |
"file.write(\"Qualquer coisa 123\")\n", | |
"file.close()'''" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "RMURiacVQEjI" | |
}, | |
"source": [ | |
"# 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=local_file_name)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "7BW9siuIQNoV" | |
}, | |
"source": [ | |
"print(\"\\nUploading to Azure Storage as blob:\\n\\t\" + local_file_name)\n", | |
"\n", | |
"# Upload the created file\n", | |
"with open(upload_file_path, \"rb\") as data:\n", | |
" blob_client.upload_blob(data)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "QF_dXSINLe_k" | |
}, | |
"source": [ | |
"print(\"\\nListing blobs...\")\n", | |
"\n", | |
"# List the blobs in the container\n", | |
"blob_list = container_client.list_blobs()\n", | |
"for blob in blob_list:\n", | |
" print(\"\\t\" + blob.name)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "qGIBoKLdYmcJ" | |
}, | |
"source": [ | |
"# Download the blob to a local file\n", | |
"# Add 'DOWNLOAD' before the .txt extension so you can see both files in the data directory\n", | |
"download_file_path = local_file_name\n", | |
"print(\"\\nDownloading blob to \\n\\t\" + download_file_path)\n", | |
"\n", | |
"with open(download_file_path, \"wb\") as download_file:\n", | |
" download_file.write(blob_client.download_blob().readall())" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "eRnt3o4JRJ__" | |
}, | |
"source": [ | |
"Excluir um container" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "r4E8yeofRQVo" | |
}, | |
"source": [ | |
"# Clean up\n", | |
"print(\"\\nPress the Enter key to begin clean up\")\n", | |
"input()\n", | |
"\n", | |
"print(\"Deleting blob container...\")\n", | |
"container_client.delete_container()\n", | |
"\n", | |
"print(\"Deleting the local source and downloaded files...\")\n", | |
"os.remove(upload_file_path)\n", | |
"os.remove(download_file_path)\n", | |
"os.rmdir(local_path)\n", | |
"\n", | |
"print(\"Done\")" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment