Created
June 4, 2010 14:36
-
-
Save thurloat/425480 to your computer and use it in GitHub Desktop.
Getting Google Storage working in Python App Engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Bring in the boto library | |
import boto | |
#Load the configuration variables from your .boto file | |
config = boto.config | |
""" | |
FAIL. | |
"""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""" | |
Python App Engine + Google Storage. | |
Based heavily from examples given by Google | |
Instructions: | |
just rip the boto lib from the gsutil project, drop it in your python | |
App Engine project. | |
@author: Adam Thurlow <Thurloat> | |
"""" | |
import boto | |
from google.appengine.ext import webapp | |
config = boto.config | |
config.add_section('Credentials') | |
config.set('Credentials', 'gs_access_key_id', 'YOURACCESSKEY') | |
config.set('Credentials', 'gs_secret_access_key', 'YOURSECRETKEY') | |
FLAGS = ['CREATE', 'LIST_BUCKETS', 'LIST_CONTENTS','DOWNLOAD_FILE'] | |
DOWNLOAD_PATH = '/path/to/file.ext' | |
DOWNLOAD_FILENAME = 'file.ext' | |
BUCKET_NAME = 'testbucket' | |
class DownloadHandler(webapp.RequestHandler): | |
def get(self): | |
srow = self.response.out.write | |
if 'DOWNLOAD_FILE' in FLAGS: | |
uri = boto.storage_uri("%s/%s" % (BUCKET_NAME, DOWNLOAD_PATH), 'gs') | |
self.response.headers['Content-Type'] = 'application/octet-stream' | |
self.response.headers.add_header('content-disposition', 'attachment', filename = DOWNLOAD_FILENAME) | |
srow(uri.get_contents_as_string()) | |
class MainHandler(webapp.RequestHandler): | |
def get(self): | |
srow = self.response.out.write | |
srow("<h4> Testing the Google Storage API </h4>") | |
if 'CREATE' in FLAGS: | |
srow("<h2>Creating Bucket</h2>") | |
uri = boto.storage_uri(BUCKET_NAME, 'gs') | |
uri.create_bucket() | |
if 'LIST_BUCKETS' in FLAGS: | |
srow("<h2>Listing Buckets</h2>") | |
uri = boto.storage_uri('', 'gs') | |
buckets = uri.get_all_buckets(); | |
for bucket in buckets: | |
self.response.out.write(bucket.name + '<br />') | |
if 'LIST_CONTENTS' in FLAGS: | |
srow("<h2>Listing Contents of %s bucket</h2>" % BUCKET_NAME) | |
uri = boto.storage_uri(BUCKET_NAME, 'gs') | |
objs = uri.get_bucket() | |
for obj in objs: | |
srow("gs://%s/%s <br />" % (BUCKET_NAME,obj.name)) | |
_URLS = [ | |
('/', MainHandler), | |
('/download',DownloadHandler), | |
] | |
def main(argv): | |
application = webapp.WSGIApplication(_URLS, debug = True) | |
wsgiref.handlers.CGIHandler().run(application) | |
if __name__ == '__main__': | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment