Regularly upload files to Google Drive using the Google Drive API in Python

background

I want a script for a job that regularly uploads files to google drive with python. If you search, there are many articles that use PyDrive, but PyDrive is not well maintained, and it seems that it is not a good idea to use it now because it uses v2 of Google Drive API. This time I will implement it using GoogleDrive API.

Reference links

Python Quickstart --Grasp the atmosphere of Google Drive API --In this example, you need to log in to the console once. --As for this specification, we want to perform regular uploads without user actions such as console login, so we need to consider another authentication.

Upload file data --File upload sample --There aren't many Python samples, so it doesn't look right.

Upload file to Google Drive with Lambda (Python)

--A sample using a GCP service account is introduced. ――This qiita article was the most helpful

Implementation sample

sample.py


from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from oauth2client.service_account import ServiceAccountCredentials
import os

def uploadFileToGoogleDrive(fileName, localFilePath):
    service = getGoogleService()
    # "parents": ["****"]Replace this part with the string after the URL of the folder you created in Google Drive.
    file_metadata = {"name": fileName, "mimeType": "text/csv", "parents": ["****"] }
    media = MediaFileUpload(localFilePath, mimetype="text/csv", resumable=True)
    file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()

def getGoogleService():
    scope = ['https://www.googleapis.com/auth/drive.file']
    keyFile = 'credentials.json'
    credentials = ServiceAccountCredentials.from_json_keyfile_name(keyFile, scopes=scope)
    return build("drive", "v3", credentials=credentials, cache_discovery=False)


getGoogleService()
uploadFileToGoogleDrive("hoge", "hige.csv")

Recommended Posts

Regularly upload files to Google Drive using the Google Drive API in Python
Upload JPG file using Google Drive API in Python
Upload files to Google Drive with Lambda (Python)
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Tweet using the Twitter API in Python
Upload images to Google Drive with Python
Download files directly to Google Drive (using Google Colaboratory)
How to get the files in the [Python] folder
Try using the DropBox Core API in Python
Initial settings when using the foursquare API in python
How to load files in Google Drive with Google Colaboratory
Using the National Diet Library Search API in Python
Upload to a shared drive with Google Drive API V3
Google Drive Api Tips (Python)
To automatically send an email with an attachment using the Gmail API in Python
Continue to retrieve tweets containing specific keywords using the Streaming API in Python
A story about a Python beginner trying to get Google search results using the API
Tweet Now Playing to Twitter using the Spotify API. [Python]
Hit the Sesami API in Python
[Python] Hit the Google Translation API
[Python3] Google translate google translate without using api
In the python command python points to python3.8
Hit the web API in Python
Access the Twitter API in Python
An easy way to hit the Amazon Product API in Python
How to get followers and followers from python using the Mastodon API
Hit the New Relic API in Python to get the server status
Python beginners hit the unofficial API of Google Play Music to play music
[SEO] Flow / sample code when using Google Analytics API in Python
Mouse operation using Windows API in Python
Create an application that just searches using the Google Custom Search API with Python 3.3.1 in Bottle
Get Google Fit API data in Python
Get Youtube data in Python using Youtube Data API
Try hitting the YouTube API in Python
Download files in any format using Python
Creating Google Spreadsheet using Python / Google Data API
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Implemented in Dataflow to copy the hierarchy from Google Drive to Google Cloud Storage
Download the images and videos included in the tweets you liked on Twitter and upload them to Google Drive
How to display Map using Google Map API (Android)
How to use the C library in Python
First steps to try Google CloudVision in Python
[Python] Get the files in a folder with Python
Convert FBX files to ASCII <-> BINARY in Python
Procedure to use TeamGant's WEB API (using python)
Summary of how to import files in Python 3
[Python] Let's execute the module regularly using schedule
Run Google Analytics API (core v3) in python
Get image URL using Flickr API in Python
To dynamically replace the next method in python
I tried using the Google Cloud Vision API
Draw graphs in Julia ... Leave the graphs to Python
Tips for hitting the ATND API in Python
Let's judge emotions using Emotion API in Python
The trick to write flatten concisely in python
Pre-process the index in Python using Solr's ScriptUpdateProcessor
Anonymous upload of images using Imgur API (using Python)
[WP REST API v2] Upload images in Python
What to do to get google spreadsheet in python
[Python] I will upload the FTP to the FTP server.