Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2295 google drive create shareable public link #2332

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
369 changes: 369 additions & 0 deletions Google Drive/Google_Drive_Create_shareable_public_link.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,369 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "2701772c-fc04-408d-afd8-988f3fa7437d",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"cell_type": "markdown",
"id": "293ccf6d-e4eb-4748-b5f3-97594e8ffc4f",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# Google Drive - Create shareable public link"
]
},
{
"cell_type": "markdown",
"id": "4d21362f-0f80-4b80-afac-a3e17afea503",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #google #drive #public #link #shareable #create"
]
},
{
"cell_type": "markdown",
"id": "2c5f36e3-d5ab-463a-b6b5-69a01c5250df",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Manasvi Agarwal](https://www.linkedin.com/in/mnsv)"
]
},
{
"cell_type": "markdown",
"id": "d7765bee-1d59-47d4-853a-b9a62abdaf65",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-10-22 (Created: 2023-10-12)"
]
},
{
"cell_type": "markdown",
"id": "87728a3b-49df-4002-a975-8ba6888de26c",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook will show how to create a shareable public link from a file stored in Google Drive. This is useful for organizations to share files with external users without giving them access to the entire Google Drive."
]
},
{
"cell_type": "markdown",
"id": "4db947a0-203b-4bc6-a1f2-004a2044ddec",
"metadata": {},
"source": [
"**Pre-requisites:**\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 file with this client_email."
]
},
{
"cell_type": "markdown",
"id": "59a5bbcb-c280-4fb3-a43e-da7fe9f00e4e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n",
"- [Google Drive - API](https://developers.google.com/drive/api/reference/rest/v3)\n",
"- [Google Drive - Share files and folders](https://developers.google.com/drive/api/guides/manage-sharing#create-permission)"
]
},
{
"cell_type": "markdown",
"id": "b78290fb-aecf-4aaf-9e0f-665f509b479b",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "bc6031bd-ca9b-472f-a9e8-506d219e553a",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "b057714c-6c70-48e6-affc-c263adbcd2f5",
"metadata": {
"execution": {
"iopub.execute_input": "2023-10-22T10:45:04.300423Z",
"iopub.status.busy": "2023-10-22T10:45:04.300193Z",
"iopub.status.idle": "2023-10-22T10:45:04.303353Z",
"shell.execute_reply": "2023-10-22T10:45:04.302720Z",
"shell.execute_reply.started": "2023-10-22T10:45:04.300401Z"
},
"tags": []
},
"outputs": [],
"source": [
"from google.oauth2.service_account import Credentials\n",
"from googleapiclient.discovery import build"
]
},
{
"cell_type": "markdown",
"id": "61131364-4870-458d-8795-3bc8ad49f5ca",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n",
"- `file_id`: ID of file stored in Google Drive. You can extract it from URL of the file\n",
"- `service_account_file`: JSON file containing credentials related to service account\n",
"- `user_role`: Access level intended for shared document (can be either: reader, writer, commenter)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "18b12e15-401f-4a4d-aeeb-ffa6d4176ce2",
"metadata": {
"execution": {
"iopub.execute_input": "2023-10-22T10:45:05.671085Z",
"iopub.status.busy": "2023-10-22T10:45:05.670854Z",
"iopub.status.idle": "2023-10-22T10:45:05.675829Z",
"shell.execute_reply": "2023-10-22T10:45:05.675293Z",
"shell.execute_reply.started": "2023-10-22T10:45:05.671061Z"
},
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"file_id = \"\"\n",
"service_account_file = \"*.json\"\n",
"user_role = \"reader\""
]
},
{
"cell_type": "markdown",
"id": "d470aa54-3ddf-443b-acfb-9297addd1a77",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "52dc6fec-9ca1-4892-8add-ef480f0cd6b2",
"metadata": {},
"source": [
"### Connect to GCP"
]
},
{
"cell_type": "markdown",
"id": "c4d1610d-81f7-437c-9a6f-f89a666e57bb",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"This function will create drive API client using service account credentials"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "57c5cd2e-f604-451d-8f51-d528cbb77b69",
"metadata": {
"execution": {
"iopub.execute_input": "2023-10-22T10:45:09.142206Z",
"iopub.status.busy": "2023-10-22T10:45:09.141974Z",
"iopub.status.idle": "2023-10-22T10:45:09.157868Z",
"shell.execute_reply": "2023-10-22T10:45:09.157325Z",
"shell.execute_reply.started": "2023-10-22T10:45:09.142182Z"
},
"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": "091387ef-44d9-49ef-a13c-288461e8f645",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Create shareable public link"
]
},
{
"cell_type": "markdown",
"id": "b4112b2d-65e4-4e16-a744-1c78230f6bf8",
"metadata": {},
"source": [
"This function will return a shareable public link for the specified file"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "3be5a14c-c5a0-45ec-bda5-6bc16e483106",
"metadata": {
"execution": {
"iopub.execute_input": "2023-10-22T10:49:13.804207Z",
"iopub.status.busy": "2023-10-22T10:49:13.803897Z",
"iopub.status.idle": "2023-10-22T10:49:13.812296Z",
"shell.execute_reply": "2023-10-22T10:49:13.811390Z",
"shell.execute_reply.started": "2023-10-22T10:49:13.804166Z"
},
"tags": []
},
"outputs": [],
"source": [
"def create_shareable_public_link(file_id, user_role):\n",
"\n",
" try:\n",
" # Create permission with appropriate role\n",
" user_permission = {\n",
" 'type': 'anyone',\n",
" 'role': user_role\n",
" }\n",
" \n",
" # Create permission on given file id\n",
" service.permissions().create(fileId=file_id, body=user_permission).execute()\n",
" \n",
" file_url = 'https://drive.google.com/file/d/' + file_id + '/view?usp=sharing'\n",
" return file_url\n",
"\n",
" except Exception as error:\n",
" print(F'An error occurred: {error}')\n",
" "
]
},
{
"cell_type": "markdown",
"id": "a0589072-1d94-4c3d-aaef-cc0a70f240e0",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "c11ca80f-2442-4867-94c3-5e5537cccb7a",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Display result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3aab332-2aa8-4429-87a4-6359e3f69936",
"metadata": {
"execution": {
"iopub.execute_input": "2023-10-22T10:49:16.739315Z",
"iopub.status.busy": "2023-10-22T10:49:16.739087Z",
"iopub.status.idle": "2023-10-22T10:49:17.372859Z",
"shell.execute_reply": "2023-10-22T10:49:17.372089Z",
"shell.execute_reply.started": "2023-10-22T10:49:16.739291Z"
},
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"public_link = create_shareable_public_link(file_id, user_role)\n",
"if public_link:\n",
" print(\"Public link: {}\".format(public_link))\n",
"else:\n",
" print(\"Error creating public link\")"
]
},
{
"cell_type": "markdown",
"id": "cfb4f669-aa94-41c2-b518-e3175286c83a",
"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
}
Loading