[PYTHON] How to upload to a shared drive using pydrive

Although there were many uploads to personal G Drive, I posted it because there was no article about shared G-Drive. First post. Way.

What you need

--GCP OAuth ID / Password --Access the GCP console --Create a project if there is no project - API & Services > Credential - +Create Credentials > OAuth Client ID --Create Oauth Client ID

pip install PyDrive

--Put the following script settings.yaml in your working directory

settings.yaml



client_config_backend: settings
client_config:
  client_id: <ID>
  client_secret: <PASS>

save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json

get_refresh_token: True

oauth_scope:
  - https://www.googleapis.com/auth/drive.file
  - https://www.googleapis.com/auth/drive.install

Create folder

test.py


from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

drive_id = "0XXXXXXXXXXk9PVA"
folder_id = "17GBXXXXXXXsCeYFAiUhykVTvuht"

file_metadata = {
'title': directory,
'mimeType': 'application/vnd.google-apps.folder',
'parents': [{
  'id': folder_id,
  'kind': 'drive#fileLink',
}],
}
f = drive.CreateFile(file_metadata)
f.Upload(param={'supportsTeamDrives': True})

This is miso f.Upload(param={'supportsTeamDrives': True})

File upload

test.py


file_name = "test.csv"
file_metadata = {
        'title': "filename_at_gdrive.csv",
        'mimeType': 'text/csv',
        'parents': [{
            'id': folder_id,
            'kind': 'drive#fileLink',
            }],
    }
    f = drive.CreateFile(file_metadata)
    # use SetContentFile for attach and upload
    f.SetContentFile(file_name)
    # always apply param when upload
    f.Upload(param={'supportsTeamDrives': True})

Get folder / file information

test.py


query_list = [
"title='{}'".format("2020.csv"),
"mimeType = 'application/vnd.google-apps.folder'",
"'{}' in parents".format(folder_id),
"trashed=false",
]
query = " and ".join(query_list)

# always specifiy team drive when accessing shared drive
file_list = drive.ListFile({
'q':query,
'supportsAllDrives':True,
'corpora': "teamDrive",
'teamDriveId': drive_id,
'includeTeamDriveItems': "true",
'supportsTeamDrives': "true",
}).GetList()

References

gdrive api method

credentails

How to get folder id

How to store files under folder

support all drive

How to store in a shared driver

mimetype

gdrive query acquisition method

Recommended Posts

How to upload to a shared drive using pydrive
Upload to a shared drive with Google Drive API V3
How to draw a graph using Matplotlib
How to install a package using a repository
How to code a drone using image recognition
How to uninstall a module installed using setup.py
How to set up a Python environment using pyenv
How to make a Python package using VS Code
How to call a function
Upload a file to Dropbox
How to hack a terminal
How to transpose a 2D array using only python [Note]
How to generate a query using the IN operator in Django
How to install python using anaconda
How to make a Japanese-English translation
How to write a Python class
How to put a symbolic link
How to create a Conda package
How to make a crawler --Advanced
Upload packages to PyPI using tokens
How to make a recursive function
How to create a virtual bridge
How to make a deadman's switch
How to create a Dockerfile (basic)
[Blender] How to make a Blender plugin
How to delete a Docker container
How to make a crawler --Basic
How to create a config file
How to paste a CSV file into an Excel file using Pandas
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
How to format a table using Pandas apply, pivot and swaplevel
How to save only a part of a long video using OpenCV
How to update a Tableau packaged workbook data source using Python
How to create a CSV dummy file containing Japanese using Faker
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
How to generate a new loggroup in CloudWatch using python within Lambda
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
How to split and save a DataFrame
How to divide and process a data frame using the groupby function
How to build a sphinx translation environment
How to create a git clone folder
How to make a model for object detection using YOLO in 3 hours
How to add a package with PyCharm
[Python] How to make a class iterable
How to set up SVM using Optuna
[Colab] How to copy a huge dataset
[Python] How to invert a character string
How to set xg boost using Optuna
[Ubuntu] How to execute a shell script
How to search Google Drive with Google Colaboratory
How to fix the initial population with a genetic algorithm using DEAP
How to create a repository from media
How to download youtube videos using pytube3
How to choose a Seaborn color palette
How to test on a Django-authenticated page
How to run a Maya Python script
Upload images to Google Drive with Python
How to automatically upload .gpx files to Strava
[2015/11/19] How to register a service locally using the python SDK on naoqi os
How to quickly create a morphological analysis environment using Elasticsearch on macOS Sierra
[Circuit x Python] How to find the transfer function of a circuit using Lcapy