How-To‎ > ‎

Upload VM Image via S3:

Below is a sample script to upload a new Virtual Machine image using the S3 interface for the Clemson One Cloud. Image files must be uploaded via S3 and then registered with the EC2 server before they can be used to start an instance. 

#!/usr/bin/env python
import boto
from boto.s3.key import Key
from boto.s3.connection import OrdinaryCallingFormat
from boto.s3.connection import S3Connection
from boto.ec2.connection import EC2Connection
from boto.ec2.regioninfo import RegionInfo

# Interface variables
host="chimney.cs.clemson.edu"
ec2Port=8443
s3Port=8888

# Must use the S3 Bucket 'Repo'
bucket="Repo"

# Cloud credentials
access_id="<your access id goes here>"
access_secret="<your secret key goes here>"
canonical_id="<your canonical id goes here>"

# The name of the image after it is uploaded to S3
s3ImageName="<insert image name>"

# The local path to the image on the client's machine
imagePath="<insert path to image file>"

# Define a region for EC2 Connection
ec2Region = RegionInfo(name="opennebula", endpoint=host)

# Create S3 Connection
s3Conn = S3Connection(access_id, access_secret, host=host, port=s3Port, is_secure=False, calling_format=OrdinaryCallingFormat())

# Create EC2 Connection
ec2Conn=boto.connect_ec2(aws_access_key_id = access_id, aws_secret_access_key = access_secret, region=ec2Region, port=ec2Port, is_secure=True)

# Create a Key object for the new VM Image
k = Key(s3Conn.get_bucket(bucket))
# Path should be based on user's canonical ID and the name of the image that you want to appear in S3
k.key = "VMS/%s/%s" % (canonical_id, s3ImageName)
# Uploads the contents of the file given
k.set_contents_from_filename(imagePath)

# This will print out the keys in the bucket. Your new key should be listed
for image in s3Conn.get_bucket(bucket).get_all_keys():
  print image

# Use the key name from the previous step as the location of the image to be registered with EC2
# newid will be the EC2 Image ID for your new VM image (ie ami-00000034 or something similar)
newid = ec2Conn.register_image(image_location=k.key)

# This will print out all of the EC2 images
for image in ec2Conn.get_all_images():
  print image
Comments