Skip to content

Instantly share code, notes, and snippets.

@leocosta037
Created September 30, 2021 17:25
Show Gist options
  • Save leocosta037/4d258b622341f40d16d523733098ef06 to your computer and use it in GitHub Desktop.
Save leocosta037/4d258b622341f40d16d523733098ef06 to your computer and use it in GitHub Desktop.
TechDay_UPE_Acesso_GCP.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "TechDay_UPE_Acesso_GCP.ipynb",
"provenance": [],
"toc_visible": true,
"mount_file_id": "1-7H3yCJ1bgQtU-UtMaddvjDh7yQgoB9a",
"authorship_tag": "ABX9TyPKFgPmBNad8yFQJEZVeTyJ",
"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/4d258b622341f40d16d523733098ef06/techday_upe_acesso_gcp.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "k_-b6fo427Dm"
},
"source": [
"# Notebook para acesso ao Google Cloud Storage do GCP"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PW1PWABnAvdi"
},
"source": [
"Material para consulta"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AkBoXxVlAxi1"
},
"source": [
"https://googleapis.dev/python/storage/latest/client.html\n",
"\n",
"https://cloud.google.com/storage/docs/reference/libraries\n",
"\n",
"https://www.thecodebuzz.com/python-upload-files-download-files-google-cloud-storage/\n",
"\n",
"https://cloud.google.com/blog/topics/developers-practitioners/serving-data-cloud-storage\n",
"\n",
"https://cloud.google.com/storage/docs/quickstart-console\n",
"\n",
"https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/storage/cloud-client\n",
"\n",
"https://cloud.google.com/storage/docs/creating-buckets\n",
"\n",
"https://cloud.google.com/storage/docs/storage-classes\n",
"\n",
"https://cloud.google.com/storage/docs/quickstart-gsutil\n",
"\n",
"https://cloud.google.com/docs/authentication/production"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lT365UVjAo8a"
},
"source": [
"# Inicialização"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Gp8UusyghtKo"
},
"source": [
"pip install gsutil"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "I2lpLmADhgVB"
},
"source": [
"pip install gcloud"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Q3yMzvI9Ya9z"
},
"source": [
"pip install google.cloud"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Cyldr_SMZwYY"
},
"source": [
"pip install google.cloud.storage"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "tHHDQzDbmtBm"
},
"source": [
"pip install --upgrade google-cloud-storage"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "XpUTjIWWc1vX"
},
"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": "B7D0zpWSB4M9"
},
"source": [
"# Carregamento"
]
},
{
"cell_type": "code",
"metadata": {
"id": "mtKdqZddizbp"
},
"source": [
"import os\n",
"os.environ[\"GOOGLE_APPLICATION_CREDENTIALS\"]=\"\" # Inserir o caminho para o arquivo JSON das credenciais"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "q2dicoBGXHwD"
},
"source": [
"# Imports the Google Cloud client library\n",
"from google.cloud import storage"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "d2iUIA4sYERj"
},
"source": [
"# Instantiates a client\n",
"storage_client = storage.Client()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "kRKUM64enFre"
},
"source": [
"# Funções"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "qRjUIVSRYIo1",
"outputId": "8ffaf1ac-bfec-4bcb-ee62-306c10e18ce4"
},
"source": [
"# The name for the new bucket\n",
"bucket_name = \"\" # Inserir o nome do bucket a ser criado\n",
"\n",
"# Creates the new bucket\n",
"bucket = storage_client.create_bucket(bucket_name)\n",
"\n",
"print(\"Bucket {} created.\".format(bucket.name))"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Bucket xavi_hernandez created.\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "P6s4zenWk9vh"
},
"source": [
"#Upload a file\n",
"\n",
"def upload_blob(bucket_name, source_file_name, destination_blob_name):\n",
" \"\"\"Uploads a file to the bucket.\"\"\"\n",
" # The ID of your GCS bucket\n",
" # bucket_name = \"your-bucket-name\"\n",
" # The path to your file to upload\n",
" # source_file_name = \"local/path/to/file\"\n",
" # The ID of your GCS object\n",
" # destination_blob_name = \"storage-object-name\"\n",
"\n",
" storage_client = storage.Client()\n",
" bucket = storage_client.bucket(bucket_name)\n",
" blob = bucket.blob(destination_blob_name)\n",
"\n",
" blob.upload_from_filename(source_file_name)\n",
"\n",
" print(\n",
" \"File {} uploaded to {}.\".format(\n",
" source_file_name, destination_blob_name\n",
" )\n",
" )\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "4Fm2SUUQlGzr"
},
"source": [
"# Sintaxe:\n",
"# upload_blob('Nome do bucket', 'Caminho do arquivo no PC', 'Caminho do arquivo no Azure blob')\n",
"upload_blob('', '', '')"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "IK49mMmol98s"
},
"source": [
"def download_blob(bucket_name, source_blob_name, destination_file_name):\n",
" \"\"\"Downloads a blob from the bucket.\"\"\"\n",
" # bucket_name = \"your-bucket-name\"\n",
" # source_blob_name = \"storage-object-name\"\n",
" # destination_file_name = \"local/path/to/file\"\n",
"\n",
" storage_client = storage.Client()\n",
"\n",
" bucket = storage_client.bucket(bucket_name)\n",
"\n",
" # Construct a client side representation of a blob.\n",
" # Note `Bucket.blob` differs from `Bucket.get_blob` as it doesn't retrieve\n",
" # any content from Google Cloud Storage. As we don't need additional data,\n",
" # using `Bucket.blob` is preferred here.\n",
" blob = bucket.blob(source_blob_name)\n",
" blob.download_to_filename(destination_file_name)\n",
"\n",
" print(\n",
" \"Blob {} downloaded to {}.\".format(\n",
" source_blob_name, destination_file_name\n",
" )\n",
" )\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "fzhDEPU6mK3q"
},
"source": [
"# Sintaxe:\n",
"# download_blob('Nome do bucket', 'Caminho do arquivo no Azure blob', 'Caminho do arquivo no PC')\n",
"download_blob('', '', '')"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment