Oh Snap!

Please turnoff your ad blocking mode for viewing your site content

Hit enter after type your search item

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

/
/
/
200 Views

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.

READ  Django 3 Send Emails To Gmail Account Using Send_Email Library 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.

READ  Django 3 Send Emails To Gmail Account Using Send_Email Library Full Project For Beginners

 

 

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.

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.

READ  Django 3 Send Emails To Gmail Account Using Send_Email Library Full Project For Beginners

 

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)

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :