列出 Google 云端硬盘文件和文件夹

问题描述 投票:0回答:1

我想创建一个简单的 Python 脚本来递归地列出特定文件夹 ID 的文件/文件夹。

以下脚本部分有效。作为起始文件夹,我使用 ID“root”,因为这应该是 Google Drive“My Drive”的根目录。

from googleapiclient.discovery import build
from google.oauth2 import service_account

# Set up the Google Drive API service
scope = ['https://www.googleapis.com/auth/drive']
service_account_json_key = 'credentials.json'
credentials = service_account.Credentials.from_service_account_file(
    filename=service_account_json_key,
    scopes=scope)
service = build('drive', 'v3', credentials=credentials)

# Function to list files and folders recursively
def list_files_and_folders(folder_id, indent=""):
    results = service.files().list(
        q=f"'{folder_id}' in parents",
        fields="files(id, name, mimeType)").execute()
    items = results.get('files', [])

    if not items:
        print(indent + "No files or folders found.")
    else:
        for item in items:
            print(indent + f"{item['name']} ({item['id']}) - {item['mimeType']}")
            if item['mimeType'] == 'application/vnd.google-apps.folder':
                list_files_and_folders(item['id'], indent + "  ")

# Call the function to list files and folders starting from the specified folder ID
root_folder_id = 'root'
print(f"Listing files and folders recursively from root folder (ID: {root_folder_id}):")
list_files_and_folders(root_folder_id)

当我第一次尝试该脚本时,我没有收到任何结果,我不得不承认我不知道为什么。

同时,我也在测试一个Google Drive文件上传脚本。这是我从 GitHub 获得的。我将父级的 ID 更改为“root”,因为我希望在“我的云端硬盘”下创建文件夹/文件。

from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.oauth2 import service_account

# Replace 'FOLDER_NAME' with the name you want to give your new folder
folder_name = 'Test Upload 2'

# setup google drive
credentials = service_account.Credentials.from_service_account_file(
        'credentials.json', scopes=['https://www.googleapis.com/auth/drive']
    )
service = build("drive", "v3", credentials=credentials)
folder_metadata = {
    'name': folder_name,
    "parents": ['root'],
    'mimeType': 'application/vnd.google-apps.folder'
}

# create folder 
new_folder = service.files().create(body=folder_metadata).execute()

#upload file inside the folder
file_metadata = {'name': 'image2.webp', 'parents': [new_folder['id']]}
media = MediaFileUpload('image2.webp')
file = service.files().create(body=file_metadata, media_body=media).execute()

# list the file inside of the folder
results = service.files().list(q=f"'{new_folder['id']}' in parents", fields="files(name)").execute()
items = results.get('files', [])
print(f"Files inside the folder , {items}")

上传脚本工作没有问题,但在检查“我的云端硬盘”时,我无法找到文件夹/文件。

因此,我再次运行脚本来列出文件和文件夹,这次脚本“专门”返回使用上传脚本创建的文件夹和文件,但“我的云端硬盘”中没有其他内容。

  • 这怎么可能?上传的文件夹/文件在哪里?

  • 为什么我在我的云端硬盘中看不到它们?

  • 为什么脚本只列出我使用脚本上传的文件,而不列出我的云端硬盘的其余部分?

python google-api google-drive-api google-api-python-client service-accounts
1个回答
1
投票

当我第一次尝试该脚本时,我没有收到任何结果,我不得不承认我不知道为什么。

您正在使用服务帐户并从其根文件夹开始,您确定它已上传任何文件吗?

上传脚本工作没有问题,但在检查“我的云端硬盘”时,我无法找到文件夹/文件。

您再次使用服务帐户,您将文件上传到服务帐户驱动器帐户,而不是您自己的个人驱动器帐户,您将看不到它。

这怎么可能?上传的文件夹/文件在哪里?

服务帐户驱动帐户

为什么我在我的云端硬盘中看不到它们?

因为服务帐号不是你。

为什么脚本只列出我使用脚本上传的文件,而不列出我的云端硬盘的其余部分?

因为它列出的是服务帐户驱动器帐户而不是您的驱动器帐户上的文件。

递归列表文件

作为旁注。按目录取回所有文件将需要对 api 进行大量调用,至少对系统中的每个目录调用一次。更好的选择是只使用 file.list 并在本地缓冲所有内容,然后将其排序到那里的目录中。它将需要更少的 API 调用。

© www.soinside.com 2019 - 2024. All rights reserved.