Skip to content
Permalink
Browse files

fix: MediaFileUpload error if file does not exist (#1127)

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
- [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/google-api-python-client/issues/new/choose) before writing your code!  That way we can discuss the change, evaluate designs, and agree on the general idea
- [x] Ensure the tests and linter pass
- [x] Code coverage does not decrease (if any source code was changed)
- [x] Appropriate docs were updated (if necessary)

Fixes #798 🦕
  • Loading branch information
parthea committed Dec 9, 2020
1 parent e6a1da3 commit 2c6d0297851c806ef850ca23686c51ca5878ac48
Showing with 9 additions and 3 deletions.
  1. +5 −3 googleapiclient/http.py
  2. +4 −0 tests/test_http.py
@@ -573,20 +573,22 @@ def __init__(
resumable: bool, True if this is a resumable upload. False means upload
in a single request.
"""
self._fd = None
self._filename = filename
fd = open(self._filename, "rb")
self._fd = open(self._filename, "rb")
if mimetype is None:
# No mimetype provided, make a guess.
mimetype, _ = mimetypes.guess_type(filename)
if mimetype is None:
# Guess failed, use octet-stream.
mimetype = "application/octet-stream"
super(MediaFileUpload, self).__init__(
fd, mimetype, chunksize=chunksize, resumable=resumable
self._fd, mimetype, chunksize=chunksize, resumable=resumable
)

def __del__(self):
self._fd.close()
if self._fd:
self._fd.close()

def to_json(self):
"""Creating a JSON representation of an instance of MediaFileUpload.
@@ -248,6 +248,10 @@ def test_media_file_upload_to_from_json(self):
self.assertEqual(500, new_upload.chunksize())
self.assertEqual(b"PNG", new_upload.getbytes(1, 3))

def test_media_file_upload_raises_on_file_not_found(self):
with self.assertRaises(FileNotFoundError):
MediaFileUpload(datafile("missing.png"))

def test_media_file_upload_raises_on_invalid_chunksize(self):
self.assertRaises(
InvalidChunkSizeError,

0 comments on commit 2c6d029

Please sign in to comment.