Skip to content

Commit

Permalink
feat: upload file to google drive
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentLvr committed Oct 2, 2023
1 parent 9ede6ca commit 5228976
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions Google Drive/Google_Drive_Upload_file.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"tags": []
},
"source": [
"**Description:** This notebook upload a file in a Google Drive account."
"**Description:** This notebook upload a file in a specific folder in a Google Drive account."
]
},
{
Expand Down Expand Up @@ -113,6 +113,7 @@
"outputs": [],
"source": [
"from google.oauth2.service_account import Credentials\n",
"from googleapiclient.http import MediaFileUpload\n",
"from googleapiclient.discovery import build"
]
},
Expand All @@ -133,7 +134,11 @@
"\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 downloaded."
"Sure, here's an improved version of the descriptions:\n",
"- `folder_id`: The unique identifier of the destination folder in Google Drive where the file will be uploaded.\n",
"- `file_name`: The designated name of the file as it will appear in Google Drive after upload.\n",
"- `file_path`: The specific location in the local system where the file to be uploaded resides.\n",
"- `file_type`: The format or extension of the file, indicating the file's type and nature."
]
},
{
Expand All @@ -147,7 +152,10 @@
"outputs": [],
"source": [
"service_account_file = 'service_account.json'\n",
"object_id = \"1NkwIJ1GysFOkRLYGZA2HP9Cb4HplEUE7\""
"folder_id = \"1fb2bHvXiX_Luepz1NJcy-w7TMcdpZAFI\"\n",
"file_name = \"New video\"\n",
"file_path = \"10XwzBEJhWAjUamqtmRL-klB_-WfQ05rj.mp4\"\n",
"file_type = \"video/mp4\""
]
},
{
Expand Down Expand Up @@ -212,13 +220,30 @@
"source": [
"def upload_file(\n",
" service,\n",
" object_id,\n",
" folder_id,\n",
" file_name,\n",
" file_path,\n",
" file_type,\n",
"):\n",
" return object_id\n",
" file_metadata = {'name': file_name, \"parents\": [folder_id]}\n",
" media = MediaFileUpload(\n",
" file_path,\n",
" mimetype=file_type\n",
" )\n",
" file = service.files().create(\n",
" body=file_metadata,\n",
" media_body=media,\n",
" fields='id'\n",
" ).execute()\n",
" print('File ID: %s' % file.get('id'))\n",
" return file\n",
"\n",
"object_id = upload_file(\n",
"file = upload_file(\n",
" service,\n",
" object_id,\n",
" folder_id,\n",
" file_name,\n",
" file_path,\n",
" file_type,\n",
")"
]
},
Expand Down Expand Up @@ -254,8 +279,16 @@
},
"outputs": [],
"source": [
"object_id"
"file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45a5142d-ad94-48e1-a2c6-bfff4fc3bc62",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 5228976

Please sign in to comment.