how to store the csv file into google cloud store bucket using web2py code

102 views Asked by At

I got the csv file from web2py application now i have to store this same file into google cloud store bucket manually(bucket is already created in google Developers Console)through my web2py application code

1

There are 1 answers

0
Eustace On

I don't know how you handle your stuff in web2py but to store a file in a bucket I do this:

def store_file(self, filename, content):
    bucket = '/' + os.environ.get('BUCKET_NAME', app_identity.get_default_gcs_bucket_name())
    filename = bucket + '/' + filename.
    try:
        gcs_file = gcs.open(filename, 'w', content_type='text/plain')
        gcs_file.write(content)
        gcs_file.close()
    except Exception, e:
        logging.exception(e)
        try:
            gcs.delete(filename)
        except gcs.NotFoundError:
            logging.info("Could not delete file " + filename)
    logging.info("Created a file " + filename)
    return filename

The content variable in my case is from cStringIO.StringIO().getValue()

More info / example here