How to Upload Files to Google Drive Using Python 3 Using Google Drive API V3 Full Project

How to Upload Files to Google Drive Using Python 3 Using Google Drive API V3 Full Project
x
How to Upload Files to Google Drive Using Python 3 Using Google Drive API V3 Full Project

Welcome folks today in this blog post we will be uploading files to google drive using the google drive api v3 in python 3. All the source code is given below in the blog. Also a step by step youtube video is shown below to follow along with the blog.

 

Retrieving Access Token

 

The first thing we need to do this retrieve the access token from the google api playground website. Go to the website and retrieve the access token

 

Writing Python Code

 

Now make a simple app.py file and write the following python code which is show below.It will actually takes this access token and upload the file to google drive using the google drive api v3. In this code we will make a simple post request to the api passing this access token as a header and also we will provide a static image to be uploaded inside the params. So just upload your file inside your project root directory.

See also  Python 3 Script to Capitalize First and Last Letters or Characters of Each Word in Text File Full Project For Beginners

 

import json
import requests
headers = {"Authorization": "Bearer ### access token ###"}
para = {
    "name": "##yourfilepath####",
}
files = {
    'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
    'file': open("./sample.png", "rb")
}
r = requests.post(
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
    headers=headers,
    files=files
)
print(r.text)

 

So replace your access token and your path to the image in respective places.

And now if you execute this python script on the command line using the command python app.py you will see this json response coming back from the api telling that your file was successfully uploaded to google drive.

 

 

So now in this response you can see it has assigned a random id to the uploaded image and also it has the name of the image also which has been uploaded and it’s mimetype also.

See also  Python 3 Web Scraping Script to Scrape the Domain Age of Website or URL Full Command Line Project For Beginners

So now if you go to google drive to check if your file is uploaded or not. Let me show you my screenshot.

 

Uploading Files to Drive Folder

 

In order to upload files to google drive folder. You can modify this python script slightly as you can add a folder name property inside the params property .Like this as shown below.

 

import requests
headers = {"Authorization": "Bearer ###youraccesstoken###"} #put ur access token after the word 'Bearer '
para = {
    "name": "dl.zip", #file name to be uploaded
    "parents": ["####yourdrivefolderid####"] # make a folder on drive in which you want to upload files; then open that folder; the last thing in present url will be folder id
}
files = {
    'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
    'file': ('application/zip',open("./dl.zip", "rb")) # replace 'application/zip' by 'image/png' for png images; similarly 'image/jpeg' (also replace your file name)
}
r = requests.post(
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
    headers=headers,
    files=files
)
print(r.text)

 

See also  Python 3 PyQt5 Script to Build Arithmetic Calculator GUI App Full Project For Beginners

 

 

Leave a Reply