ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

【python】PyDrive をアップグレードしたら Google ドライブにアップロードする処理が失敗するようになって困った

PyDrive を使った古いプログラムを改修してライブラリをアップグレードしたら、 Google ドライブにアップロードする処理が失敗するようになって困ったためここに回避方法をメモします。

症状

問題の python スクリプトを実行すると、処理が失敗しました。
ログを確認してみると、httplib2 で RedirectMissingLocation という例外が発生していました。

    File "/usr/local/lib/python3.7/site-packages/pydrive/files.py", line 285, in Upload
      self._FilesInsert(param=param)
    File "/usr/local/lib/python3.7/site-packages/pydrive/auth.py", line 75, in _decorated
      return decoratee(self, *args, **kwargs)
    File "/usr/local/lib/python3.7/site-packages/pydrive/files.py", line 369, in _FilesInsert
      http=self.http)
    File "/usr/local/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
      return wrapped(*args, **kwargs)
    File "/usr/local/lib/python3.7/site-packages/googleapiclient/http.py", line 822, in execute
      _, body = self.next_chunk(http=http, num_retries=num_retries)
    File "/usr/local/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
      return wrapped(*args, **kwargs)
    File "/usr/local/lib/python3.7/site-packages/googleapiclient/http.py", line 983, in next_chunk
      headers=headers)
    File "/usr/local/lib/python3.7/site-packages/oauth2client/transport.py", line 175, in new_request
      redirections, connection_type)
    File "/usr/local/lib/python3.7/site-packages/oauth2client/transport.py", line 282, in request
      connection_type=connection_type)
    File "/usr/local/lib/python3.7/site-packages/httplib2/__init__.py", line 1702, in request
      conn, authority, uri, request_uri, method, body, headers, redirections, cachekey,
    File "/usr/local/lib/python3.7/site-packages/httplib2/__init__.py", line 1445, in _request
      _("Redirected but the response is missing a Location: header."), response, content,
  httplib2.error.RedirectMissingLocation: Redirected but the response is missing a Location: header.

原因

httplib2 ライブラリが v0.16.0 以上になると、ファイルサイズが大きいものをアップロードする際に今回のエラーになるようです。
github.com

回避方法

上記のサイトに httplib2 の 0.15.0 バージョンにダウングレードすると回避できるという記載があったので、やってみたらうまく行きました。

It should either force to use 0.15.0 version of httplib2 or adapt the breaking changes introduced in the new version.
A temporary fix for those using the client library would be to add httplib2==0.15.0 in requirements,txt of the project

requirements.txt に以下を追加しました。

httplib2==0.15.0