Save File to Rackspace Cloud Files with Go Language and Martini

Rackspace Cloud Files is awesome, it’s competitively priced and is powered by Akamai, one of the leaders in CDN technology.

It’s relatively easy to get up and running, first you need to have the following imports into the file you are using to fire your controller:


Since we are using this with Martini, make sure you are using the Martini binding middleware, that link actually has a basic example on how to store the object to a variable, but we expand by grabbing the file name and uploading that to Rackspace Cloud Files.

Below is the code, anything in ALL_CAPS should be replaced with your information

func saveController(postInfo PostInfo) {

    if postInfo.DefaultImage != nil {
        file, err := postInfo.DefaultImage.Open()

        if err == nil {
            c := swift.Connection{
                UserName: "YOUR_USERNAME",
                ApiKey:   "YOUR_API_KEY",
                AuthUrl:  "",

            err := c.Authenticate()

            if err != nil {

            body, err := ioutil.ReadAll(file)

            if err == nil {
                err := c.ObjectPutBytes("YOUR_PUBLIC_CONTAINER", postInfo.DefaultImage.Filename, body, "")

                if err == nil {
                    postInfo.DefaultImagePath = postInfo.DefaultImage.Filename
    // This is where you would save the postInfo variable to your database, with the DefaultImagePath variable now set, and continue onto do what else you need to do for the user.

To start, postInfo is being set by the PostInfo struct which was sent where you set up the route, the route would look something like the following:

m.Post('/info/save', binding.Bind(PostInfo{}), saveController)

to start, we are checking to see if the DefaultImage was set, DefaultImage should be set to your <input type="file" name="default_image" /> field. Reference the binding link provided above to learn how to set up your struct to read form values, or check out the Guide to creating an app in Martini with Gorm to learn how to do this.

Then we are opening up the file, and assinging it to the file variable. If there were no errors, we are going to go on to create the swift connection object. To be honest, panic(err) could probably and should be handled better, to your discretion.

Once we authenticate, we use the ioutil library to load the body as a byte object, then the next few lines will put the object in your container using the uploaded file name. postInfo.DefaultImage.Filename is how we get the filename of what was uploaded, you may want to change this.

That’s it! If you look in your container on Rackspace, you should see the file has been uploaded.

** Tip: lets say you name the file in the ObjectPutBytes function as “directory/file.png”, when you view the structure in the Rackspace control panel, the files will be organized into directories. **

Last Updated: 2015-01-07 09:37:31 +0000 UTC

What are your thoughts on this?


RSS feed

Follow Doug On Social