From c6f2dd6b3f9712211f1855391d261607a54aafee Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Mon, 2 Oct 2023 16:20:39 +0200 Subject: [PATCH] feat: retrieve and add properties, retrieve metadata --- .../Google_Drive_Add_properties_to_file.ipynb | 304 ++++++++++++++++++ .../Google_Drive_Retrieve_file_meta.ipynb | 2 +- ...oogle_Drive_Retrieve_file_properties.ipynb | 300 +++++++++++++++++ 3 files changed, 605 insertions(+), 1 deletion(-) create mode 100644 Google Drive/Google_Drive_Add_properties_to_file.ipynb create mode 100644 Google Drive/Google_Drive_Retrieve_file_properties.ipynb diff --git a/Google Drive/Google_Drive_Add_properties_to_file.ipynb b/Google Drive/Google_Drive_Add_properties_to_file.ipynb new file mode 100644 index 0000000000..1bf9575b9d --- /dev/null +++ b/Google Drive/Google_Drive_Add_properties_to_file.ipynb @@ -0,0 +1,304 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "5f52edaa-24b0-47b1-ad38-2102da01e7ea", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "210901d4-efad-467e-9c1b-7133f99edc51", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Google Drive - Add properties to file" + ] + }, + { + "cell_type": "markdown", + "id": "c8c92f63-7e98-4c5f-94b3-6d6329887bf5", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #googledrive #google #drive #create #add #properties #python #api" + ] + }, + { + "cell_type": "markdown", + "id": "39db9889-b178-401f-8fb7-2e4fcd916542", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)" + ] + }, + { + "cell_type": "markdown", + "id": "f5a97053-b098-4ef4-a6ab-d098727800b3", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-09-27 (Created: 2023-09-27)" + ] + }, + { + "cell_type": "markdown", + "id": "b97391b8-40c8-42f0-878a-43e1a2150cf6", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook add properties to a file in a Google Drive account. Properties could be a usefull way to store data relative to automation." + ] + }, + { + "cell_type": "markdown", + "id": "a830f22b-75c1-4a80-9f96-da44a0aba953", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:**\n", + "- [Google Drive API Documentation](https://developers.google.com/drive/api/v3/reference)\n", + "- [Google Drive Python Quickstart](https://developers.google.com/drive/api/v3/quickstart/python)" + ] + }, + { + "cell_type": "markdown", + "id": "ec9d0770-1299-450b-8ae9-d09d19696af4", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "7543855b-40fc-4b9e-98ff-d65eae66422f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0fdd3dbb-6868-4921-be2c-793d7f4465e9", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "from google.oauth2.service_account import Credentials\n", + "from googleapiclient.discovery import build" + ] + }, + { + "cell_type": "markdown", + "id": "43d5e0c3-d916-4e80-a737-8c15dd67647b", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n", + "**Pre-requisite**\n", + "\n", + "You need to set up service account credentials to use Google Drive API. For detailed instructions on how to do this, please refer to the [Google Drive Python Quickstart guide](https://developers.google.com/drive/api/v3/quickstart/python).\n", + "\n", + "After creating the service account, you will get a client_email (found in your service account JSON file). Make sure to share your Google Drive or specific folder with this client_email.\n", + "\n", + "**Mandatory**\n", + "- `service_account_file`: This is the JSON file that contains your service account credentials. You'll get this file when you create a service account.\n", + "- `object_id`: Object ID to be retrieved.\n", + "- `properties`: Dict of properties to be added." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "91a1b3fd-9bd9-4fcb-81fe-85bced49782e", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "service_account_file = 'service_account.json'\n", + "object_id = \"1fb2bHvXiX_Luepz1NJcy-w7TMcdpZAFI\"\n", + "properties = {\"status\": \"Done\"}" + ] + }, + { + "cell_type": "markdown", + "id": "f7e932bf-1b45-4940-8cb4-c69d32826a13", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "05f77548-e636-4110-88e3-621729de498e", + "metadata": {}, + "source": [ + "### Connect to GCP" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5e20b7e6-ca3c-48bf-a050-c6707829911c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def gcp_connect(file_path): \n", + " # Load the service account credentials\n", + " credentials = Credentials.from_service_account_file(file_path)\n", + "\n", + " # Build the service\n", + " service = build('drive', 'v3', credentials=credentials)\n", + " return service\n", + " \n", + "service = gcp_connect(service_account_file)" + ] + }, + { + "cell_type": "markdown", + "id": "c149081a-7527-40d9-be9e-aea27274864b", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Add properties to file" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6eb6e3b5-b4ca-4349-84b1-6205ecd4d11d", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "def add_properties(\n", + " service,\n", + " object_id,\n", + " properties,\n", + "):\n", + " file = service.files().update(fileId=object_id, body={'properties': properties}).execute()\n", + " return file\n", + "\n", + "file = add_properties(\n", + " service,\n", + " object_id,\n", + " properties\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "11616427-0b9d-49bb-86a1-8478bae6b703", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "7d948195-81ae-4796-a0a9-e782b44e4c0e", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5c42e249-1bbd-4b53-a2ee-d554f015b2f2", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "file" + ] + }, + { + "cell_type": "markdown", + "id": "81063e3e-0fec-4148-b2ff-5d8b800140ae", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + " " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "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.9.6" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": {}, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Google Drive/Google_Drive_Retrieve_file_meta.ipynb b/Google Drive/Google_Drive_Retrieve_file_meta.ipynb index 4908abb5f3..dd71fad77d 100644 --- a/Google Drive/Google_Drive_Retrieve_file_meta.ipynb +++ b/Google Drive/Google_Drive_Retrieve_file_meta.ipynb @@ -30,7 +30,7 @@ "tags": [] }, "source": [ - "**Tags:** #googledrive #google #drive #retrieve #object #python #api" + "**Tags:** #googledrive #google #drive #retrieve #metadata #python #api" ] }, { diff --git a/Google Drive/Google_Drive_Retrieve_file_properties.ipynb b/Google Drive/Google_Drive_Retrieve_file_properties.ipynb new file mode 100644 index 0000000000..ebbb4611a4 --- /dev/null +++ b/Google Drive/Google_Drive_Retrieve_file_properties.ipynb @@ -0,0 +1,300 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "5f52edaa-24b0-47b1-ad38-2102da01e7ea", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "210901d4-efad-467e-9c1b-7133f99edc51", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Google Drive - Retrieve file properties" + ] + }, + { + "cell_type": "markdown", + "id": "c8c92f63-7e98-4c5f-94b3-6d6329887bf5", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #googledrive #google #drive #retrieve #properties #python #api" + ] + }, + { + "cell_type": "markdown", + "id": "39db9889-b178-401f-8fb7-2e4fcd916542", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)" + ] + }, + { + "cell_type": "markdown", + "id": "f5a97053-b098-4ef4-a6ab-d098727800b3", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-09-27 (Created: 2023-09-27)" + ] + }, + { + "cell_type": "markdown", + "id": "b97391b8-40c8-42f0-878a-43e1a2150cf6", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook retrieves properties from a file in a Google Drive account. Properties could be a usefull way to store data relative to automation." + ] + }, + { + "cell_type": "markdown", + "id": "a830f22b-75c1-4a80-9f96-da44a0aba953", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:**\n", + "- [Google Drive API Documentation](https://developers.google.com/drive/api/v3/reference)\n", + "- [Google Drive Python Quickstart](https://developers.google.com/drive/api/v3/quickstart/python)" + ] + }, + { + "cell_type": "markdown", + "id": "ec9d0770-1299-450b-8ae9-d09d19696af4", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "7543855b-40fc-4b9e-98ff-d65eae66422f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0fdd3dbb-6868-4921-be2c-793d7f4465e9", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "from google.oauth2.service_account import Credentials\n", + "from googleapiclient.discovery import build" + ] + }, + { + "cell_type": "markdown", + "id": "43d5e0c3-d916-4e80-a737-8c15dd67647b", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n", + "**Pre-requisite**\n", + "\n", + "You need to set up service account credentials to use Google Drive API. For detailed instructions on how to do this, please refer to the [Google Drive Python Quickstart guide](https://developers.google.com/drive/api/v3/quickstart/python).\n", + "\n", + "After creating the service account, you will get a client_email (found in your service account JSON file). Make sure to share your Google Drive or specific folder with this client_email.\n", + "\n", + "**Mandatory**\n", + "- `service_account_file`: This is the JSON file that contains your service account credentials. You'll get this file when you create a service account.\n", + "- `object_id`: Object ID to be retrieved." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "91a1b3fd-9bd9-4fcb-81fe-85bced49782e", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "service_account_file = 'service_account.json'\n", + "object_id = \"10XwzBEJhWAjUamqtmRL-klB_-WfQ05rj\"" + ] + }, + { + "cell_type": "markdown", + "id": "f7e932bf-1b45-4940-8cb4-c69d32826a13", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "05f77548-e636-4110-88e3-621729de498e", + "metadata": {}, + "source": [ + "### Connect to GCP" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5e20b7e6-ca3c-48bf-a050-c6707829911c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def gcp_connect(file_path): \n", + " # Load the service account credentials\n", + " credentials = Credentials.from_service_account_file(file_path)\n", + "\n", + " # Build the service\n", + " service = build('drive', 'v3', credentials=credentials)\n", + " return service\n", + " \n", + "service = gcp_connect(service_account_file)" + ] + }, + { + "cell_type": "markdown", + "id": "c149081a-7527-40d9-be9e-aea27274864b", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Retrieve file properties" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6eb6e3b5-b4ca-4349-84b1-6205ecd4d11d", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "def retrieve_file_properties(\n", + " service,\n", + " object_id,\n", + "):\n", + " file = service.files().get(fileId=object_id, fields='properties').execute()\n", + " return file\n", + "\n", + "file = retrieve_file_properties(\n", + " service,\n", + " object_id,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "11616427-0b9d-49bb-86a1-8478bae6b703", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "7d948195-81ae-4796-a0a9-e782b44e4c0e", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5c42e249-1bbd-4b53-a2ee-d554f015b2f2", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "file" + ] + }, + { + "cell_type": "markdown", + "id": "81063e3e-0fec-4148-b2ff-5d8b800140ae", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + " " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "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.9.6" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": {}, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}