2017-08-03 41 views
0

这里的缓冲区是相关的代码无法读取BytesIO在谷歌应用程序引擎Flex环境

import logging 


logging.getLogger('googleapicliet.discovery_cache').setLevel(logging.ERROR) 

import datetime 
import json 


from flask import Flask, render_template, request 
from flask import make_response 

from googleapiclient.discovery import build 
from googleapiclient.http import MediaIoBaseDownload 

from oauth2client.client import AccessTokenCredentials 

... 

@app.route('/callback_download') 
def userselectioncallback_with_drive_api(): 
    """ 
    Need to make it a background process 
    """ 
    logging.info("In download callback...") 

    code = request.args.get('code') 

    fileId = request.args.get('fileId') 
    logging.info("code %s", code) 
    logging.info("fileId %s", fileId) 

    credentials = AccessTokenCredentials(
     code, 
     'flex-env/1.0') 

    http = httplib2.Http() 
    http_auth = credentials.authorize(http) 

    # Exports a Google Doc to the requested MIME type and returns the exported content. Please note that the exported content is limited to 10MB. 
    # v3 does not work? over quota? 
    drive_service = build('drive', 'v3', http=http_auth) 

    drive_request = drive_service.files().export(
     fileId=fileId, 
     mimeType='application/pdf') 

    b = bytes() 
    fh = io.BytesIO(b) 
    downloader = MediaIoBaseDownload(fh, drive_request) 
    done = False 

    try: 
     while done is False: 
      status, done = downloader.next_chunk() 
      logging.log("Download %d%%.", int(status.progress() * 100)) 
    except Exception as err: 
     logging.error(err) 
     logging.error(err.__class__) 

    response = make_response(fh.getbuffer()) 
    response.headers['Content-Type'] = 'application/pdf' 
    response.headers['Content-Disposition'] = \ 
      'inline; filename=%s.pdf' % 'yourfilename' 

    return response 

它是基于驱动API的一些代码示例。我试图从谷歌驱动器导出一些文件为PDF格式。

唯一的例外来自于线

response = make_response(fh.getbuffer()) 

它抛出该异常:

TypeError: 'memoryview' object is not callable 

我如何可以检索从fh适当的PDF内容?我是否需要进一步应用一些base 64编码?

我的本地运行时是python 3.4.3

回答

0

我使用了错误的API。我应该这样做:

response = make_response(fh.getvalue())