6

Google Apps ドメイン ドライブ サービスからすべてのファイルをダウンロードしてエクスポートできる Python の単純なスクリプトに取り組んでいます。サービス アカウントを使用してドライブ セッションを作成でき、リスト クエリから JSON 出力を取得しています。この記事に従ってダウンロード機能も定義しました。

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

問題は、この関数がコンテンツと呼ばれる別の JSON 出力を返すことですが、ファイルをローカルの HDD に保存する方法がわかりません。Pythonスクリプト内で使用できるかどうかCURLを調べていたところ、CURLと同様に使用する必要があるurllib/urllib2が見つかりました。しかし、urllib2 を使用してリモート ファイルを読み取ろうとすると、次のようになります。

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

401 Error Unathorizedが表示されます。

したがって、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:

わかりました、それで私はついにそれを理解します。私の間違いは、コンテンツで関数 read() を使用しようとしたことです。file.write(content)を使用するだけです。

4

2 に答える 2

6

この記事のスクリプトを試すことができます。また、Python 用の Google API クライアント ライブラリを使用することを忘れないでください。

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
于 2013-05-21T07:48:57.130 に答える
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
于 2015-12-09T08:58:49.827 に答える