mediafileupload python example

Solutions on MaxInterview for mediafileupload python example by the best coders in the world

showing results for - "mediafileupload python example"
Leonie
19 Feb 2017
1from __future__ import print_function
2import pickle
3import os.path
4from googleapiclient.http import MediaFileUpload
5from googleapiclient.discovery import build
6from google_auth_oauthlib.flow import InstalledAppFlow
7from google.auth.transport.requests import Request
8
9# If modifying these scopes, delete the file token.pickle.
10SCOPES = ['https://www.googleapis.com/auth/drive']
11
12
13def main():
14    """Shows basic usage of the Drive v3 API.
15    Prints the names and ids of the first 10 files the user has access to.
16    """
17    creds = None
18    # The file token.pickle stores the user's access and refresh tokens, and is
19    # created automatically when the authorization flow completes for the first
20    # time.
21    if os.path.exists('token.pickle'):
22        with open('token.pickle', 'rb') as token:
23            creds = pickle.load(token)
24    # If there are no (valid) credentials available, let the user log in.
25    if not creds or not creds.valid:
26        if creds and creds.expired and creds.refresh_token:
27            creds.refresh(Request())
28        else:
29            flow = InstalledAppFlow.from_client_secrets_file(
30                'credentials.json', SCOPES)
31            creds = flow.run_local_server(port=0)
32        # Save the credentials for the next run
33        with open('token.pickle', 'wb') as token:
34            pickle.dump(creds, token)
35
36    service = build('drive', 'v3', credentials=creds)
37
38    media = MediaFileUpload(
39        'big.jpeg',
40        mimetype='image/jpeg',
41        resumable=True
42    )
43    request = service.files().create(
44        media_body=media,
45        body={'name': 'Big', 'parents': ['<your folder Id>']}
46    )
47    response = None
48    while response is None:
49        status, response = request.next_chunk()
50        if status:
51            print("Uploaded %d%%." % int(status.progress() * 100))
52    print("Upload Complete!")
53
54
55if __name__ == '__main__':
56    main()
57