Python中从缓冲区插入Google驱动器API文件

Python中从缓冲区插入Google驱动器API文件,python,python-2.7,google-drive-api,Python,Python 2.7,Google Drive Api,我试图找出如何在files().insert()方法中指定一个来自缓冲区(甚至类似文件的对象)的media\u body,而不是保存一个临时文件,然后使用MediaFileUpload插入到Google Drive中,然后删除临时文件 有没有人对此很幸运,可以为我指出正确的方向?我在appengine上使用了以下代码从缓冲区上载: def insert_file(service, title, description, parent_id, mime_type, filebody):

我试图找出如何在
files().insert()
方法中指定一个来自缓冲区(甚至类似文件的对象)的
media\u body
,而不是保存一个临时文件,然后使用
MediaFileUpload
插入到Google Drive中,然后删除临时文件


有没有人对此很幸运,可以为我指出正确的方向?

我在appengine上使用了以下代码从缓冲区上载:

    def insert_file(service, title, description, parent_id, mime_type, filebody):
        """Insert new file and deletes old file, if with same title.

        Args:
        service: Drive API service instance.
        title: Title of the file to insert, including the extension.
        description: Description of the file to insert.
        parent_id: Parent folder's ID.
        mime_type: MIME type of the file to insert.
        filebody: string, Bytes of body content.
        filename: Filename of the file to insert.
        Returns:
        Inserted file metadata if successful, None otherwise.
        """

        urlfetch.set_default_fetch_deadline(45)

        # check if file is already there
        param = {}
        param['q'] = "title = '"+title+"'"
        filelist = service.files().list(**param).execute()
        if filelist:
            for f in filelist['items']:
                file_id = f['id']
                # logging.info("file: %s - %s" % (f['title'], file_id))
                # here we could delete the file, skip trash
                service.files().delete(fileId=file_id).execute()

        media_body = MediaInMemoryUpload(filebody, mime_type)
        body = {
        'title': title,
        'description': description,
        'mimeType': mime_type
        }
        # Set the parent folder.
        if parent_id:
                body['parents'] = [{'id': parent_id}]

        try:
                file = service.files().insert(
                    body=body,
                    media_body=media_body).execute()
                return file
        except errors.HttpError, error:
            logging.info('An error occured: %s' % error)
        return None