2

Google drive api does provide some samples to create and update spreadsheets.

However, since we have different csvs and there will be more we'll be designing. what I'm looking for is a generic solution which will take the url of the csv stored in the blobstore and save those files on Google drive.

2 Answers 2

2

My solution is like this, took help of drEdit samples:

  def get_data(self):
    blob_reader = blobstore.BlobReader(self.__blob_key__)
    data = blob_reader.read()
    fh = io.BytesIO(data)
    return fh

  def save_file(self, dest_filename, description, mime_type):
    service = self.create_drive()
    resource = {
      'title': dest_filename,
      'description': description,
      'mimeType': mime_type,
    }
    try:
      file_data = self.get_file_data()
      resource = service.files().insert(
          body=resource,
          media_body=MediaIoBaseUpload(
              file_data,
              mime_type,
              resumable=False)
      ).execute()
    except AccessTokenRefreshError:
      pass

calling save_file:

  drive.save_file('testing_drive', 'testing....', 'text/csv')

or

drive.save_file('testing_drive.csv', 'testing....', 'text/csv')

However, there one small issue with this code. Google drive API doesn't convert this file to spreadsheet while uploading hence preview doesn't work. To view the file i have to open this doc in spreadsheets which converts the file to google spreadsheet. I've seen in API documentation regarding the convert option. However, I couldn't find any way of converting the files while using RPC calls. Please suggest how can I achieve this.

Sign up to request clarification or add additional context in comments.

1 Comment

I've followed this and used mime type as application/vnd.google-apps.spreadsheet. However I'm getting following error: HttpError: <HttpError 400 when requesting googleapis.com/upload/drive/v2/… returned "Bad Request">
0

There is no generic solution that I know of, but we are really talking about 4 or 5 lines of Python to achieve this.

The DrEdit Python sample saves files to Drive from App Engine Python, using the blobstore will be a simple addition to that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.