File size: 1,292 Bytes
11dde70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from googleapiclient.discovery import build
from google.oauth2 import service_account
from googleapiclient.http import MediaFileUpload
import pdb
pdb.set_trace()

import gradio as gr

# 来自Google Cloud控制台的JSON凭据文件
credentials_file =  "./src/peerless-window-254907-b386b71c0d99.json"
api_version = 'v3'

# 创建服务对象
credentials = service_account.Credentials.from_service_account_file(
    credentials_file, scopes=['https://www.googleapis.com/auth/drive'])
service = build('drive', api_version, credentials=credentials)

# 列出文件
results = service.files().list().execute()
files = results.get('files', [])

print(files)
from googleapiclient.http import MediaIoBaseDownload
import io 

file_id = "1EqHciegNxZSyWJ9Nizo1QmRQEgTkgWCo"
# Get the file's metadata
file = service.files().get(fileId=file_id).execute()

pdb.set_trace()
request = service.files().get_media(fileId="1EqHciegNxZSyWJ9Nizo1QmRQEgTkgWCo")
with open(file['name'], 'wb') as file_obj:
    downloader = MediaIoBaseDownload(file_obj, request)
    done = False
    while not done:
        status, done = downloader.next_chunk()
        print(f"Download {int(status.progress() * 100)}%.")

print(f"Downloaded: {file['name']}")

pdb.set_trace()
        
# print('文件ID:%s' % response.get('id'))