LoginSignup
10
11

More than 3 years have passed since last update.

pydrive を使って 共有ドライブにアップロードする方法

Last updated at Posted at 2020-06-06

個人 G Drive にアップロードする系を多かったものの、共有の G-Drive に関する記事はなかったので投稿しました。
初投稿。ウェイ。

必要なモノ

  • GCP の OAuth ID / Password

    • GCPコンソールにアクセス
    • プロジェクトがなければプロジェクトを作成
    • API & Services > Credential
    • +Create Credentials > OAuth Client ID
    • Oauth Client ID を作成
  • PyDrive

    pip install PyDrive

  • 以下のスクリプト settings.yaml を作業ディレクトリに置く

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

フォルダの作成

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

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

drive_id = "0XXXXXXXXXXXXXVA" #confirm from url
folder_id = "XXXXXXXXXXXXXXXXXXXXXXXXht" #confirm from url

directory = "workdir"

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})

ここがミソ
f.Upload(param={'supportsTeamDrives': True})

ファイルのアップロード

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})

フォルダ・ファイル情報取得

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()

参考文献

type style
gdrive api 方法 https://qiita.com/akabei/items/f25e4f79dd7c2f754f0e
https://python.keicode.com/advanced/pydrive.php
credentails https://console.developers.google.com/apis/credentials?project=revcomm-common
folder id 取得方法 https://ploi.io/documentation/mysql/where-do-i-get-google-drive-folder-id#:~:text=Getting this folder ID is,be put in that folder.)
folder 配下 にファイルを格納する方法 https://developers.google.com/drive/api/v3/folder
https://stackoverflow.com/questions/46562255/python-upload-my-own-files-into-my-drive-using-pydrive-library
support all drive https://stackoverflow.com/questions/56452507/how-to-upload-file-to-group-drive-shared-drive-with-google-drive-api-v3
共有ドライバに格納する方法 https://github.com/gsuitedevs/PyDrive/issues/149
mimetype https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
gdrive query 取得方法 https://pythonhosted.org/PyDrive/filelist.html
10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11