3

写真を Google ドライブにアップロードするスクリプトを作成したいと考えています。Google Document List API を数時間掘り下げた後。gdata-python-client 2.0.17(最新) を選択してスクリプトをビルドします。ファイルをコレクションにアップロードできないことを除いて、すべてがうまく機能します。ここに例外があります。

トレースバック (最新の呼び出しが最後):
  ファイル「./upload.py」の 27 行目
    アップロード (sys.argv[1])
  ファイル「./upload.py」、22 行目、アップロード
    client.create_resource(p、コレクション=f、メディア=ms)
  ファイル "/usr/local/lib/python2.7/dist-packages/gdata/docs/client.py"、300 行目、create_resource 内
    return uploader.upload_file(create_uri, entry, **kwargs)
  ファイル "/usr/local/lib/python2.7/dist-packages/gdata/client.py"、1090 行目、upload_file 内
    start_byte, self.file_handle.read(self.chunk_size))
  ファイル "/usr/local/lib/python2.7/dist-packages/gdata/client.py"、1048 行目、upload_chunk 内
    エラーを発生させる
gdata.client.RequestError: サーバーの応答: 400、<errors xmlns='http://schemas.google.com/g/2005'><error><domain>GData</domain><code>InvalidEntryException</code ><internalReason>申し訳ありませんが、サーバー エラーが発生しました。もう一度やり直してください。</internalReason></error></errors>

gdata のソース コードをハッキングした後、デバッグ用にいくつかの情報を出力します。

>
範囲: バイト 0 ~ 524287/729223
送信先: https://docs.google.com/feeds/upload/create-session/default/private/full/folder%3A0B96cfHivZx6ddGFwYXVCbzc4U3M/contents?upload_id=AEnB2UqnYRFTOyCCIGIESUIctWg6hvQIHY4JRMnL-CUQhHii3RGMFWZ12a7lXWd1hgVzhd8Fmhwfwzhd1Fmhwfwzhd1Vql
>
範囲: バイト 524288 ~ 729222/729223
送信先: https://docs.google.com/feeds/upload/create-session/default/private/full/folder%3A0B96cfHivZx6ddGFwYXVCbzc4U3M/contents?upload_id=AEnB2UqnYRFTOyCCIGIESUIctWg6hvQIHY4JRMnL-CUQhHii3RGMFWZ12a7lXWd1hgVzhd8Fmhwfwzhd1Fmhwfwzhd1Vql

PUTファイルの最後の部分で発生する例外。

4

1 に答える 1

1

新しい Google Drive API v2 を試してみることをお勧めします。これにより、メディア アップロードのサポートが向上し、はるかに簡単になります: https://developers.google.com/drive/v2/reference/files/insert

承認されたサービス インスタンスを取得したら、次のように新しいファイルを挿入するだけです。

from apiclient import errors
from apiclient.http import MediaFileUpload
# ...

def insert_file(service, title, description, parent_id, mime_type, filename):
  """Insert new file.

  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.
    filename: Filename of the file to insert.
  Returns:
    Inserted file metadata if successful, None otherwise.
  """
  media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)
  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:
    print 'An error occured: %s' % error
    return None
于 2012-07-10T16:40:24.757 に答える