Google Drive api Python - 上传到文件夹后失去权限

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

希望有人可以帮助我,因为我正在努力弄清楚为什么会发生这种情况。我有一个文件想要上传到 Google 云端硬盘上的特定文件夹。该代码循环访问返回的列表,直到与文件夹名称匹配并检索 ID。然后使用它上传到文件夹。这是第一次,而且只有第一次,效果很好。一旦我将一个文件上传到该文件夹,在进一步尝试时,返回的文件/文件夹列表会排除有问题的文件夹。如果我删除文件夹内容(而不是文件夹)并清空垃圾箱,它就会再次工作。非常感谢。

def get_files_and_creds(folder_name):
    try:
        creds = main()
        service = build("drive", "v3", credentials=creds)
        results = (
            service.files()
            .list(pageSize=10, fields="nextPageToken, files(id, name, mimeType, size, parents, modifiedTime)")
            .execute()
        )
        items = results.get("files", [])

        if not items:
            logging.error("No files found.")
            return
        for item in items:
            if item['name'] == folder_name:
                logging.debug("File list:"+str(item['name'])+":"+str(item['id']))
                folder_id = item['id']
            else:
                logging.info("No matching folder name:"+folder_name)
                return False
    except HttpError as e:
        logging.error("get_files:"+str(e))
        return False
    return folder_id, creds

def file_upload(upload_file, folder_name):
    try:
        logging.debug("folder name:"+str(folder_name)+" Filename:"+str(upload_file))
        data = get_files_and_creds(folder_name)
        folder_id = data[0]
        creds = data[1]
        logging.debug("FolderID:"+str(folder_id)+str(creds))
        service = build("drive", "v3", credentials=creds)
        file_metadata = {
            'name': os.path.basename(upload_file),
            'mimeType': '*/*',
            'parents': [folder_id]
            }
        media = MediaFileUpload(upload_file,
                                mimetype='*/*',
                                resumable=True)
        file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
        logging.debug('File ID: ' + file.get('id'))
    except Exception as e:
        logging.error("file_upload"+str(e))
python google-drive-api
1个回答
0
投票

您的函数存在问题

get_files_and_creds
,如果第一项不是目标文件夹,它将在第一次迭代中返回
False

为避免出现这种情况,请返回

folder_id
creds
(如果找到文件夹)。这是更新的代码:

def get_files_and_creds(folder_name):
    try:
        creds = main()
        service = build("drive", "v3", credentials=creds)
        results = (
            service.files()
            .list(pageSize=10, fields="nextPageToken, files(id, name, mimeType, size, parents, modifiedTime)")
            .execute()
        )
        items = results.get("files", [])

        if not items:
            logging.error("No files found.")
            return
        for item in items:
            if item['name'] == folder_name:
                logging.debug("File list:"+str(item['name'])+":"+str(item['id']))
                folder_id = item['id']
                return folder_id, creds

        logging.info("No matching folder name:"+folder_name)
        return False
        
    except HttpError as e:
        logging.error("get_files:"+str(e))
        return False

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