2

I'm trying to upload a file using Blobstore API to Google Cloud Storage. The image uploads correctly, but then I try to process it (link it to a user). I'm getting the error:

Index out of range

This is my code:

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_file_infos('file')  # 'file' is file upload field in the form
        file_info = upload_files[0]

        #self.response.headers['Content-Type'] = 'application/x-www-form-urlencoded'
        #self.response.headers.add_header('Access-Control-Allow-Origin', '*')
        gcs_filename = file_info.gs_object_name
        file_key = blobstore.create_gs_key(gcs_filename)

        File(file=file_key, owner=utils.get_current_user(), 
                    url= images.get_serving_url(file_key)).put()

My code drops in file_info = upload_files[0] line.

Ying Li
  • 2,500
  • 2
  • 13
  • 37
Allan Porras
  • 133
  • 1
  • 11
  • it just means that upload_files has no contents. Try sending the result of len(upload_files) to the log or using a debugger and viewing the contents directly. – Paul Collingwood Dec 05 '14 at 17:11
  • len(upload_files) returns "1". – Allan Porras Dec 05 '14 at 17:19
  • I have deleted de 'file' parameter in get_file_infos('file'). Now it can get correctly the file, but I have a new issue generating the blobKey throw create_gs_key(). Says Expected BlobKey, got '{large-string}'. Any idea? – Allan Porras Dec 05 '14 at 18:20
  • I opened this thread: http://stackoverflow.com/questions/27322887/get-the-current-user-from-blobstoreuploadhandler – Allan Porras Dec 05 '14 at 19:06
  • I have a similar question for the Java Blobstore: http://stackoverflow.com/questions/27340269/how-can-i-submit-a-text-field-in-the-post-request-that-uploads-a-blob-to-the-blo Can someone please take a look and recommend a workaround? There is an active bounty worth 50 rep. – Price Dec 13 '14 at 08:01

1 Answers1

0

Where is your code that puts the file into your Google Cloud Storage bucket?

I think the problem might be these two lines, depending on how you implemented the GCS upload...

gcs_filename = file_info.gs_object_name
file_key = blobstore.create_gs_key(gcs_filename)

The gs_object_name should only return a meaningful result if the item is from GCS. This would cause create_gs_key() to fail as well if the gcs_filename is not correct.

For how to use blobstore API with Google Cloud Storage, please see this article for details - https://cloud.google.com/appengine/docs/python/blobstore/#Python_Using_the_Blobstore_API_with_Google_Cloud_Storage

Ying Li
  • 2,500
  • 2
  • 13
  • 37