Upload the image file to Google Drive with python. Implement it quietly according to Quickstart.
$ pip install --upgrade google-api-python-client
Add the part to upload the jpg by referring to Quickstart sample code.
The code below uploads a file called "sample.jpg " in the same directory after getting the credentials.
google_drive_uploadjpg.py
# -*- coding: utf-8 -*-
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import requests
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
#https in quickstart://www.googleapis.com/auth/drive.metadata.readonly but not upload permission
#Use the following of FullScope that can also be uploaded(I searched for SCOPE that can only upload, but I couldn't find it...)
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
#Application name entered when creating the project
APPLICATION_NAME = 'Drive API Quickstart'
def get_credentials():
"""
(quickstart.Japanese translation of py)
Store valid user authentication(Locally stored credentials)Get from
If nothing is stored locally or if the stored credentials are invalid
Get new credentials with OAuth2 flow
(Japanese translation up to here)
・ If you execute it as it is, the browser will open automatically and you will be taken to the authentication screen.
・ When executing python--noauth_local_The URL is displayed on the console with the webserver option
When you enter the displayed URL in the browser, the authentication screen is displayed, and enter the code that appears after authentication on the console.
:return:Obtained credentials
"""
# ~/.credentials/drive-python-quickstart.Save credentials in json
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'drive-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
print('Storing credentials to ' + credential_path)
return credentials
def upload_jpg(credentials, jpgfile):
url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=media"
size = os.path.getsize(jpgfile)
headers = {
'Host':'www.googleapis.com',
'Content-Type':'image/jpeg',
'Content-Length': str(size),
'Authorization': 'Bearer ' + credentials.access_token,
}
f = open(jpgfile, "rb")
requests.post(url, headers=headers,data=f)
return
def main():
#Get credentials
credentials = get_credentials()
#jpgfile Upload(REST)
upload_jpg(credentials, "sample.jpg ")
if __name__ == '__main__':
main()
python google_drive_uploadjpg.py
If the image has been uploaded to Google Drive by executing, it is okay.
Upload is a REST API, so you can also upload with curl.
uploadjpg_curl.sh
curl -X POST \
-H 'Host: www.googleapis.com' \
-H 'Content-Type: image/jpeg' \
-H 'Content-Length: number_of_bytes_in_file' \
-H 'Authorization: Bearer your_auth_token' \
-T sample.jpg \
https://www.googleapis.com/upload/drive/v3/files?uploadType=media
For number_of_bytes_in_file, enter the size of sample.jpg For your_auth_token, enter the access_token described in ~ / .credentials / drive-python-quickstart.json.
I put it below. Putting your client_secret.json in the same directory should work. https://github.com/yakan10/google_drive_uploadjpg
It is almost a Japanese translation of Quickstart Step1.
Recommended Posts