Skip to content

Commit

Permalink
Fix #1 - Problems uploading media
Browse files Browse the repository at this point in the history
There are two problems uploading media to GCS:

1- wrongUrlForUpload:

Google's API documentation [1] now indicates that there are two
different URLs for uploads, one for metadata requests and another one
for the media upload requests.

This patch updates the URL following yarcherny's suggestion from the bug
report fixing the issue.

2- request.put doesn't seem to accept "bytearray" structure anymore, so
we'll be passing a "memoryview" instead.  We also change from
"bytes((data)" to "data.tobytes()" in "GCSObjFile" because it doesn't
work properly on Python 2.7.

[1]: https://cloud.google.com/storage/docs/json_api/v1/objects/insert
  • Loading branch information
Akrog committed Nov 26, 2016
1 parent f620c0e commit 9d418be
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions gcs_client/base.py
Expand Up @@ -28,6 +28,7 @@ class GCS(object):
_required_attributes = ['credentials']

_URL = 'https://www.googleapis.com/storage/v1/b'
_URL_UPLOAD = 'https://www.googleapis.com/upload/storage/v1/b'

def __init__(self, credentials, retry_params=None):
"""Base GCS initialization.
Expand Down
6 changes: 3 additions & 3 deletions gcs_client/gcs_object.py
Expand Up @@ -227,7 +227,7 @@ class GCSObjFile(object):
Instances support context manager behavior.
"""
_URL = base.Fillable._URL + '/%s/o/%s'
_URL_UPLOAD = base.Fillable._URL + '/%s/o'
_URL_UPLOAD = base.Fillable._URL_UPLOAD + '/%s/o'

def __init__(self, bucket, name, credentials, mode='r', chunksize=None,
retry_params=None, generation=None):
Expand Down Expand Up @@ -465,7 +465,7 @@ def read(self, size=None):

data = self._buffer.read(size)
self._offset += len(data)
return bytes(data)
return data.tobytes()

@common.retry
def _get_data(self, size, begin=0):
Expand Down Expand Up @@ -547,4 +547,4 @@ def read(self, size=None):
remaining -= len(data)

self._size -= size
return result
return memoryview(result)

0 comments on commit 9d418be

Please sign in to comment.