NOTE: This article is written for our First-Generation Cloud Control Panel. You can access this interface from the Next-Generation Cloud Control Panel by clicking your username in the upper-right of the control panel and selecting "First-Generation Control Panel".
Contents |
In this tutorial we plan to deploy a simple web page that retrieves an image from the Cloud Files platform. Under the hood we will be using some rewrite rules to obfuscate the origin URL. For SEO purposes etc.
Now we will cover a couple of ways to get some media onto CloudFiles. The first example is very simple and covers using an application called FireUploader a Mozilla FireFox extension. The next example will describe how to upload files using the Python API.
This is a simple Mozilla FireFox Extension that allows you to upload files directly from your machine to CloudFiles in seconds.
For the purposes of this example we assume you have Python and the API installed. For more info on setting up the API for Python see Python API Installation for configuration steps.
This script is very simple and is a great place to start working with the Python API. This will upload one file and is really only useful for this example but gives you a good idea of how to upload a file from the Python API. =)
#!/usr/bin/env python
import cloudfiles
conn = cloudfiles.get_connection('UserNameGoesHere','APIKeyGoesHere')
cont = conn.create_container('media')
obj = cont.create_object('image1.jpg')
obj.load_from_filename('PutPathToImageHere')
print "File Uploaded!"
if cont.is_public == False:
cont.make_public()
Ok so the code above does a very simple task... uploads a file and enables CDN on the container named media we created. Here is a quick walk through of how to execute this code on your local machine:
python cfupload.py
Here we will create a simple page that calls an image on cloud files using the local server side path in this part of the tutorial we use mod_rewrite.
The Code:
<html> <head><title>Real Simple Webpage</title></head> <body><img src="image1.jpg" /></body> </html>
Ok well there is the super simple code.
Here we will create the rewrite rules that will redirect the image to the Cloud Files platform. We need to grab the CDN URL for the container media we created. To do this:
Once we have that; here is what the rewrite rule will look like:
RewriteEngine on
RewriteBase /
RewriteRule image1\.jpg http://c0203623401.cdn.cloudfiles.rackspacecloud.com%{REQUEST_URI}
Replace the container URL with your own and ensure the %{REQUEST_URI} is right in front of it with no spaces.
What this does is rewrite anything called image1.jpg.
The {REQUEST_URI} is a variable that holds the relative path to the file that is being requested. So it will always redirect to the correct place. if you have anymore questions about mod_rewrite check out Apache's Documentation
You might run into trouble getting mod_rewrite working if your application resides in a subdirectory of the main site. Try replacing "/" with the subdirectory of your application in the RewriteBase directive, as described in this article.
For the sake of this tutorial we will create a sample Cloud Site. To do this:
Now go ahead and navigate to the sample page we created; and like magic the image being called is actually coming from Cloud Files and if the user looks at your source code it looks like the image is being called locally =). Now this won't change the actual http request, but it does however obfuscate the URL in the source code.
If you have any questions or comments or; if this demo suddenly does not work please shoot us an email at cloudfiles@rackspacecloud.com and we would be more then happy to answer any questions you have!
© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

9 Comments
The re-write portion of this
RewriteEngine on
RewriteBase /sandbox
RewriteRule Amlactin_Coupon-180dpi\.jpg http://c3332256.r56.cf0.rackcdn.com%{REQUEST_URI}
Re: Rewrite
Re: Rewrite
CDN
Re: cname
The cloud files offers
re: Origin pull
http://feedback.rackspace.com/forums/71021-product-feedback/suggestions/1105435-add-the-origin-pull-feature-to-cloud-files
As an alternative, if you're presently a Cloud Files customer, you might look at a syncing tool like fileconveyor:
http://www.rackspace.com/knowledge_center/article/syncing-to-cloud-files-with-fileconveyor
I already rated, but the
re: Request age
Add new comment