Horje
How to replace the file if the file already exist in google drive api in python Code Example
How to replace the file if the file already exist in google drive api in python
from __future__ import print_function
from apiclient import errors
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaFileUpload

def activateService():
    creds = None
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)
    return build('drive', 'v3', credentials=creds)

SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly',
          'https://www.googleapis.com/auth/drive.file']

myservice = activateService()

file_metadata = {'name': 'myFile.txt'}
media = MediaFileUpload("myFile.txt", mimetype="text/plain")
file = myservice.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()




Python

Related
python run scp command Code Example python run scp command Code Example
Return an RDD of grouped items. Code Example Return an RDD of grouped items. Code Example
father's day 2020 Code Example father's day 2020 Code Example
reverse words and swapcase in python Code Example reverse words and swapcase in python Code Example
triu function in numpy Code Example triu function in numpy Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7