从共享文件夹获取文件 [英] Getting files from shared folder

查看:88
本文介绍了从共享文件夹获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从共享文件夹访问文件,然后下载它们.问题是我最终只能得到一个入门"文件,而我不知道如何解决这个问题.我尝试了几件事,包括在files().get函数下进行查询,但没有成功.这是我的代码:

I'd like to access the files from a shared folder and then download them. The problem is that I only end up getting a "Getting Started" file and I don't know how to solve this. I tried several things, including making a query under the files().get function but with no success. This is my code:

from google.oauth2 import service_account
import googleapiclient.discovery
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http


scopes = ['https://www.googleapis.com/auth/drive.metadata.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('server_secret.json', scopes)

http_auth = credentials.authorize(Http())
service = build('drive', 'v3', http=http_auth)

results = service.files().list(
    pageSize=10, fields="nextPageToken, files(id, name)").execute()

files = results.get('files', [])

for f in files:
    print(f)

推荐答案

假设您引用的文件夹具有唯一的名称,则可以尝试以下操作:

Assuming the folder you refer to has a unique name, you could try something like the following:

def get_folder(name):
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http)
    results = service.files().list(
        pageSize=10,
        q=("name = '{0}'".format(name) +
           " and mimeType = 'application/vnd.google-apps.folder'"),
        corpora="user",
        fields="nextPageToken, files(id, name, webContentLink, " +
               "createdTime, modifiedTime)").execute()
    items = results.get('files', [])
    if not items:
        return None
    return items[0]

此函数指定与Drive文件夹相对应的mime类型,并返回与给定名称匹配的第一个结果.如果您的文件夹名称不是唯一的,则可以对其进行修改以返回每个匹配的结果.

This function specifies the mime type corresponding to a Drive folder and returns the first result that matches the given name. If your folder name is not unique then you can modify it to return every matching result.

def get_folder_contents(folder_id):
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http)

    results = service.files().list(
        q=("'{0}' in parents".format(folder_id)),
        corpora="user",
        fields="nextPageToken, files(id, name, webContentLink, " +
               "createdTime, modifiedTime)").execute()
    items = results.get('files', [])
    if not items:
        print('No files found.')
    return items

一旦知道Drive文件夹的 id ,您就可以查询在 parents 中具有该 id 的所有项目.使用这两个功能,您可以执行以下操作:

Once you know the id of the Drive folder, you can query for all the items that have that id in parents. With these two functions you then could do something like:

folder = get_folder('my_folder_name')
folder_items = get_folder_contents(folder['id'])

for item in folder_items:
    # decide if you should download
    my_download_function(url=item['webContentLink'])

这篇关于从共享文件夹获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆