Skip to content

Commit

Permalink
feat: retrieve and add properties, retrieve metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentLvr committed Oct 2, 2023
1 parent 1dc8c25 commit c6f2dd6
Show file tree
Hide file tree
Showing 3 changed files with 605 additions and 1 deletion.
304 changes: 304 additions & 0 deletions Google Drive/Google_Drive_Add_properties_to_file.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "5f52edaa-24b0-47b1-ad38-2102da01e7ea",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"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
}
2 changes: 1 addition & 1 deletion Google Drive/Google_Drive_Retrieve_file_meta.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"tags": []
},
"source": [
"**Tags:** #googledrive #google #drive #retrieve #object #python #api"
"**Tags:** #googledrive #google #drive #retrieve #metadata #python #api"
]
},
{
Expand Down
Loading

0 comments on commit c6f2dd6

Please sign in to comment.