How to store images ala S3 using Google app engine?

34 views
Skip to first unread message

Paul Jobs

unread,
Apr 9, 2008, 4:23:45 PM4/9/08
to google-a...@googlegroups.com
Does google app engine offer static file storage
thanks

Paul

Bjorn Tipling

unread,
Apr 9, 2008, 4:27:56 PM4/9/08
to google-a...@googlegroups.com
There's blob storage in the database, and I'm not sure if you can write
to a directory.

Ryan Mulligan

unread,
Apr 9, 2008, 4:29:46 PM4/9/08
to google-a...@googlegroups.com
Any static files you put in your app directory will get put onto Google's server. This can be used to serve static files. Checkout this page about static files:

http://code.google.com/appengine/docs/gettingstarted/staticfiles.html

it talks about stylesheets but you can do it for images too.

Rapheal Kaplan

unread,
Apr 9, 2008, 4:32:47 PM4/9/08
to google-a...@googlegroups.com
  You can not write to a directory.  You can still read files in your directory.
--

 - 'Chops

Ryan Mulligan

unread,
Apr 9, 2008, 4:38:32 PM4/9/08
to google-a...@googlegroups.com
I was going to say that you  might be able to use S3 for storage, but that doesn't seem to be the case. They've locked down sockets and URLGet's fetch doesn't seem to be able to send files via multi-part POST.

Sarath Chandra Pandurangi

unread,
Apr 9, 2008, 4:41:24 PM4/9/08
to Google App Engine
Files cannot be stored statically. But can store as blobs

http://code.google.com/appengine/docs/datastore/typesandpropertyclasses.html#BlobProperty

Bryan Donlan

unread,
Apr 9, 2008, 8:14:20 PM4/9/08
to Google App Engine


On Apr 9, 4:38 pm, "Ryan Mulligan" <r...@ryantm.com> wrote:
> I was going to say that you  might be able to use S3 for storage, but that
> doesn't seem to be the case. They've locked down sockets and URLGet's fetch
> doesn't seem to be able to send files via multi-part POST.

Use a PUT request, not a POST request, to upload to S3. POST is much
more complex, and only really useful when you want to allow a user to
do a direct upload. Here's some code that works for me, though I've
not tested it much yet:

def _method2str(method):
if method == urlfetch.GET:
return "GET"
elif method == urlfetch.POST:
return "POST"
elif method == urlfetch.HEAD:
return "HEAD"
elif method == urlfetch.PUT:
return "PUT"
elif method == urlfetch.DELETE:
return "DELETE"
else:
raise ValueException("Unknown method %s" % str(method))

def s3fetch(bucket, path, payload = None, method = urlfetch.GET,
headers = None, allow_truncated = False):
FMT = '%a, %d %b %Y %H:%M:%S +0000'
if not headers:
headers = {}
headers = headers.copy()
headers['x-amz-date'] = datetime.datetime.utcnow().strftime(FMT)
cresource = "/%s%s" % (bucket, path)
canonheaders = {}
for key in headers:
canonheaders[key.lower()] = headers[key]
cheaders = ''
p = re.compile("^x-amz-")
chkeys = canonheaders.keys()
chkeys.sort()
for key in chkeys:
if p.match(key):
cheaders = "%s%s:%s\n" % (cheaders, key,
canonheaders[key])
ctype = canonheaders.get('content-type', '')
cmd5 = canonheaders.get('content-md5', '')
cdate = ''
cverb = _method2str(method)
stringtosign = cverb + "\n" + cmd5 + "\n" + ctype + "\n" + cdate +
"\n" + cheaders + cresource
hm = hmac.new(S3_SECRET_KEY, stringtosign, hashlib.sha1)
hmac_b64 = base64.b64encode(hm.digest())
logging.info("stringtosign: {%s} hmac {%s}" % (stringtosign,
hmac_b64))
headers["Authorization"] = "AWS %s:%s" % (S3_ACCESS_KEY, hmac_b64)

url = "http://%s.s3.amazonaws.com%s" % (bucket, path)
result = urlfetch.fetch(url, payload = payload, method = method,
headers = headers, allow_truncated = allow_truncated)
if (result.status_code >= 300) and (result.status_code != 404):
logging.error("Unexpected result from S3 (%d): {%s}" %
(result.status_code, result.content))
return HttpResponseServerError("Internal error")
logging.debug("s3 response ok, code={%d}" % result.status_code)
return result

jchris

unread,
Apr 9, 2008, 8:20:31 PM4/9/08
to Google App Engine
I have code to fetch photos form regular urls over the internet and
put them in datastore. It's pretty simple, and the source is available
here:

http://github.com/jchris/fug-this-/tree/master/fugthis.py

The photos I store are served from urls like /image/
thedatastorekeyfortheimage

The important part is getting the content-type right.

Chris

paul jobs

unread,
Apr 9, 2008, 9:14:04 PM4/9/08
to google-a...@googlegroups.com
does this code upload to s3 or to google

bjtitus

unread,
Apr 22, 2008, 2:15:21 PM4/22/08
to Google App Engine
Since he says they are stored in datastore, I believe that jchris'
images are stored in Google rather than s3. If Bryan's code does work,
however, the images should be uploaded to s3.

On Apr 9, 8:14 pm, "paul jobs" <webjog...@gmail.com> wrote:
> does this code upload to s3 or to google
>
Reply all
Reply to author
Forward
0 new messages