Cloud 101 Webframework Tutorial

AWS Elastic Beanstalk API

Overview

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

Objectives
  • 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 api.py 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)
    key.get_contents_to_filename(filename)

    geojsondata = open(filename).read()

    return HttpResponse(json.dumps(geojsondata))

Code is here: https://github.com/cloudmaven/cloud101demo_beanstalk/blob/master/app/api.py

Key Points