Using Amazon/AWS Glacier with Python boto

16 Oct

Amazon’s Glacier is a fairly new AWS service for low-cost storage/backups. But there is little documentation on how to actually use it.

The latest version of Python’s boto library has added Glacier functionality, so it is possible to use Glacier through boto.

A quick background to Glacier storage mechanism: you can create multiple “vaults” (with human-readable names, similar to S3 buckets). Each vault can hold multiple “archives” (which have NO human-readable names, and are kind-of similar to files.)

But: note that Glacier has no filename functionality – the archives you store are all tagged with an ID. If you want to store a file and keep track of it, you will have to build an offline filename-tracking utility for Glacier.

Without much ado, here is the code for uploading a file to Glacier using boto (this assumes you’ve already created a vault through the web console.)

# Import boto's layer2
import boto.glacier.layer2

# Various variables for AWS creds, vault name, local file name
awsAccess = "AKIAxxxx"
awsSecret = "YouRSecRetKeY"
vaultName = "YourVaultName"
fileName = "LocalFileName"

# Create a Layer2 object to connect to Glacier
l = boto.glacier.layer2.Layer2(aws_access_key_id=awsAccess, aws_secret_access_key=awsSecret)

# Get a vault based on vault name (assuming you created it already)
v = l.get_vault(vaultName)

# Create an archive from a local file on the vault
archiveID = v.create_archive_from_file(fileName)

# If you ever want, you can delete the archive on the vault
# with the archive ID.
# v.delete_archive(archiveID)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: