committing python app engine sample showing use of bigstore with service account
review details: http://codereview.appspot.com/5845055/
This commit is contained in:
13
samples/storage_serviceaccount_appengine/app.yaml
Normal file
13
samples/storage_serviceaccount_appengine/app.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
application: bucket-list-python
|
||||
version: 1
|
||||
runtime: python27
|
||||
threadsafe: no
|
||||
api_version: 1
|
||||
|
||||
handlers:
|
||||
- url: /listing.xsl
|
||||
static_files: listing.xsl
|
||||
upload: listing.xsl
|
||||
|
||||
- url: .*
|
||||
script: main.py
|
||||
32
samples/storage_serviceaccount_appengine/listing.xsl
Executable file
32
samples/storage_serviceaccount_appengine/listing.xsl
Executable file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet version="1.0"
|
||||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
||||
xmlns:ama="http://doc.s3.amazonaws.com/2006-03-01">
|
||||
|
||||
<xsl:template match="/">
|
||||
<html>
|
||||
<body>
|
||||
<h2><a href="http://developer.google.com/storage">Google Cloud Storage</a> Content Listing for Bucket
|
||||
<xsl:value-of select="ama:ListBucketResult/ama:Name"/></h2>
|
||||
<table border="1" cellpadding="5">
|
||||
<tr bgcolor="#9acd32">
|
||||
<th>Object Name</th>
|
||||
<th>Modification Time</th>
|
||||
<th>ETag</th>
|
||||
<th>Size</th>
|
||||
<th>Storage Class</th>
|
||||
</tr>
|
||||
<xsl:for-each select="ama:ListBucketResult/ama:Contents">
|
||||
<tr>
|
||||
<td><xsl:value-of select="ama:Key"/></td>
|
||||
<td><xsl:value-of select="ama:LastModified"/></td>
|
||||
<td><xsl:value-of select="ama:ETag"/></td>
|
||||
<td><xsl:value-of select="ama:Size"/></td>
|
||||
<td><xsl:value-of select="ama:StorageClass"/></td>
|
||||
</tr>
|
||||
</xsl:for-each>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
</xsl:template>
|
||||
</xsl:stylesheet>
|
||||
97
samples/storage_serviceaccount_appengine/main.py
Normal file
97
samples/storage_serviceaccount_appengine/main.py
Normal file
@@ -0,0 +1,97 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright 2007 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
"""This application produces formatted listings for Google Cloud
|
||||
Storage buckets.
|
||||
|
||||
It takes a bucket name in the URL path and does an HTTP GET on the
|
||||
corresponding Google Cloud Storage URL to obtain a listing of the
|
||||
bucket contents. For example, if this app is invoked with the URI
|
||||
http://bucket-list.appspot.com/foo, it would remove the bucket name
|
||||
'foo', append it to the Google Cloud Storage service URI and send
|
||||
a GET request to the resulting URI. The bucket listing is returned
|
||||
in an XML document, which is prepended with a reference to an XSLT
|
||||
style sheet for human readable presentation.
|
||||
|
||||
More information about using Google App Engine apps and service accounts
|
||||
to call Google APIs can be found here:
|
||||
|
||||
<https://developers.google.com/accounts/docs/OAuth2ServiceAccount>
|
||||
<http://code.google.com/appengine/docs/python/appidentity/overview.html>
|
||||
"""
|
||||
|
||||
__author__ = 'marccohen@google.com (Marc Cohen)'
|
||||
|
||||
import httplib2
|
||||
import logging
|
||||
import os
|
||||
import pickle
|
||||
import re
|
||||
|
||||
from google.appengine.api import memcache
|
||||
from google.appengine.ext import webapp
|
||||
from google.appengine.ext.webapp import template
|
||||
from google.appengine.ext.webapp.util import run_wsgi_app
|
||||
from oauth2client.appengine import AppAssertionCredentials
|
||||
|
||||
# Constants for the XSL stylesheet and the Google Cloud Storage URI.
|
||||
XSL = '\n<?xml-stylesheet href="/listing.xsl" type="text/xsl"?>\n';
|
||||
URI = 'http://commondatastorage.googleapis.com'
|
||||
|
||||
# Obtain service account credentials and authorize HTTP connection.
|
||||
credentials = AppAssertionCredentials(
|
||||
scope='https://www.googleapis.com/auth/devstorage.read_write')
|
||||
http = credentials.authorize(httplib2.Http(memcache))
|
||||
|
||||
|
||||
class MainHandler(webapp.RequestHandler):
|
||||
|
||||
def get(self):
|
||||
try:
|
||||
# Derive desired bucket name from path after domain name.
|
||||
bucket = self.request.path
|
||||
if bucket[-1] == '/':
|
||||
# Trim final slash, if necessary.
|
||||
bucket = bucket[:-1]
|
||||
# Send HTTP request to Google Cloud Storage to obtain bucket listing.
|
||||
resp, content = http.request(URI + bucket, "GET")
|
||||
if resp.status != 200:
|
||||
# If error getting bucket listing, raise exception.
|
||||
err = 'Error: ' + str(resp.status) + ', bucket: ' + bucket + \
|
||||
', response: ' + str(content)
|
||||
raise Exception(err)
|
||||
# Edit returned bucket listing XML to insert a reference to our style
|
||||
# sheet for nice formatting and send results to client.
|
||||
content = re.sub('(<ListBucketResult)', XSL + '\\1', content)
|
||||
self.response.headers['Content-Type'] = 'text/xml'
|
||||
self.response.out.write(content)
|
||||
except Exception as e:
|
||||
self.response.headers['Content-Type'] = 'text/plain'
|
||||
self.response.set_status(404)
|
||||
self.response.out.write(str(e))
|
||||
|
||||
|
||||
def main():
|
||||
application = webapp.WSGIApplication(
|
||||
[
|
||||
('.*', MainHandler),
|
||||
],
|
||||
debug=True)
|
||||
run_wsgi_app(application)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user