Search code examples
python-2.7google-app-enginegoogle-cloud-storagegcloud-pythongoogle-cloud-python

google cloud storage stores only (48 B) for each file


I am uploading binary jpeg files using html <form> on to my python webapp server hosted on Google App Engine. The server method receives the entire image (tried printing stats such as file size/type etc. successfully), but fails to write it to google cloud storage bucket. What ends up on the GCS bucket is a corrupted file of just 48 bytes.

def handleUpload(self):

  client = self._get_storage_client()
  bucket = client.get_bucket(config.CLOUD_STORAGE_BUCKET)

  results = []

  for name, fieldStorage in self.request.POST.items():
    if type(fieldStorage) is unicode:
      continue
    result = {}
    fileName = urllib.unquote(fieldStorage.filename)
    blob = bucket.blob(fileName)

    blob.upload_from_string(
        str(fieldStorage.file),fieldStorage.type)

    url = blob.public_url
    if isinstance(url, six.binary_type):
        url = url.decode('utf-8')

    results.append(result)

  return results

Solution

  • Figured it out. Actally it should be fieldStorage.file.read() instead of fieldStorage.file because it reads the file using a temporary buffer and not the whole file at once.