2013-05-21 11 views
5

Google AppsドメインドライブサービスからすべてのファイルをダウンロードしてエクスポートできるPythonの簡単なスクリプトに取り組んでいます。サービスアカウントを使用してドライブセッションを作成できました。リストクエリからJSON出力を取得しています。ドライブAPIからPythonスクリプトを使用してファイルをダウンロードする方法

https://developers.google.com/drive/manage-downloads

問題は、この機能は、コンテンツと呼ばれる別のJSON出力を返すということですが、私はHDD上のファイルをローカルに保存する方法を見つけ出すことはできません。また、私はこの記事によるとダウンロード機能を定義しました。 Pythonスクリプトの中で使用でき、CURLと同様に使用されるurllib/urllib2が見つかったら、CURLを調べていました。私がして、リモートファイルを読むためにurllib2のを使用しようとする。しかし:

remote_file = urllib2.urlopen(download_url).read() 

私はUnathorized401エラーを取得します。

したがって、urllib2は動作していますが、保存された資格情報は使用されていないようです。

だから、urllib/2を使用して承認されたクエリを作成するにはどうすればよいですか?または、スクリプト内からローカルにファイルを格納する正しい方法は何ですか?ファイルをローカルに保存するのに役立ついくつかの他のpythonまたはgoogle固有のライブラリがありますか?

ありがとうございます。

編集:Google APIクライアントライブラリを使用しています。問題は、関数download_fileがJSON出力を返すが、ファイルをローカルストレージに保存できないということです。

私はこのような何かを試してみました:

def download_file(service, drive_file): 
    """Download a file's content. 

    Args: 
      service: Drive API service instance. 
      drive_file: Drive File instance. 

    Returns: 
      File's content if successful, None otherwise. 
    """ 
    download_url = drive_file.get('downloadUrl') 
    if download_url: 
      resp, content = service._http.request(download_url) 
      if resp.status == 200: 
        print 'Status: %s' % resp 
        #return content 
        title = drive_file.get('title') 
        path = './data/'+title 
        file = open(path, 'wb') 
       # remote_file = urllib2.urlopen(download_url).authorize().read() 
        file.write(content.read()) 
      else: 
        print 'An error occurred: %s' % resp 
        return None 
    else: 
      # The file doesn't have any content stored on Drive. 
      return None 

これは、HDD上のファイルを作成しますが、コンテンツを読み込もうとするとき、それは失敗します。ローカルディスクへの書き込みに適したコンテンツの処理方法はわかりません。

EDIT2:

[OK]を、ので、私は最終的にそれを把握。私の間違いは、私はコンテンツにread()関数を使用しようとしていたということでした。私はちょうどfile.write(内容)を使用する必要があります。

答えて

6

the articleからこのスクリプトを試すことができます。 Google APIs Client Library for Pythonを使用してください。

from apiclient import errors 
# ... 

def download_file(service, drive_file): 
    """Download a file's content. 

    Args: 
    service: Drive API service instance. 
    drive_file: Drive File instance. 

    Returns: 
    File's content if successful, None otherwise. 
    """ 
    download_url = drive_file.get('downloadUrl') 
    if download_url: 
     resp, content = service._http.request(download_url) 
    if resp.status == 200: 
     print 'Status: %s' % resp 
     return content 
    else: 
     print 'An error occurred: %s' % resp 
     return None 
    else: 
    # The file doesn't have any content stored on Drive. 
    return None 
+2

返信ありがとうございます、実際これは私が使用しているものです。しかし問題は、この関数はJSON出力だけのコンテンツを返しますが、ファイルをローカルストレージ(HDD)に保存する方法を理解できないということです。 – Beneato

0

下記のコードは、ファイルの内容をローカルファイルに保存するのに役立ちます。以下のコードでは、ファイル拡張子&を置き換えてください。

if download_url: 
    resp, content = service._http.request(download_url) 
    if resp.status == 200: 
     print ('Status: %s' % resp) 
     title = file.get('title') 
     path = './data/'+title+".csv" 
     file1 = open(path, 'wb') 
     file1.write(content) 
    else: 
     print ('An error occurred: %s' % resp) 
     return None 
else: 
    # The file doesn't have any content stored on Drive. 
    return None 
関連する問題