Cloud 101 Webframework Tutorial

AWS Elastic Beanstalk API


Teaching: 0 min
Exercises: 10 min
  • How do I connect to my s3 bucket from my webapp

  • Build a simple API to display contents of an s3 bucket to a map

…Continued from Part 1

Build your API

We will create a new file in the app folder called and stick this code in.

Here we are creating the s3 connection using boto. When the month (e.g. Apr) and the variable (e.g. ET) are specified, this API will request the necessary file (‘Mean_’ + month + ‘_’ + var + ‘.geojson’) from your s3 bucket and dump the contents into “./test.geojson”

#### Django modules
from django.http import  HttpResponse
from django.conf import settings

# #### External modules
import json
import boto
import boto.s3.connection

def simple_chart(request):

    month = request.GET.get('month', None)
    var = request.GET.get('var', None)

    filename = "./test.geojson"
    infile = 'Mean_' + month + '_' + var + '.geojson'

    # connect to the bucket
    conn = boto.connect_s3(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
    bucket = conn.get_bucket(settings.BUCKET_NAME)

    key = bucket.get_key(infile)

    geojsondata = open(filename).read()

    return HttpResponse(json.dumps(geojsondata))

Code is here:

Key Points