This is a .NET/C# interface into the Rackspace CloudFiles service. Cloud Files is a reliable, scalable and affordable web-based storage hosting for backing up and archiving all your static content.
Cloud Files is the first and only cloud service that leverages a tier one CDN provider to create such an easy and complete storage-to-delivery solution for media content.
Please read the wiki about what information is best to help people fix your issues, then create an issue on the issues tab.
Go to the downloads page and download the latest version.
Unzip the file, unzip the bin zip, and grab the following files:
com.mosso.cloudfiles.dll log4net.dll log4Net.config
Reference them from your application. Look at the examples below once you done this. Example folder structure:
/Your_project /lib /cloudfiles com.mosso.cloudfiles.dll log4net.dll /src …
Visual Studio 2010 and .NET 4.0
Running the tests requires Ruby and Rake to be installed. We suggest you use the Ruby Installer For Windows. We currently use the 1.8.6 version. After that is installed you will need to install the rake, rubyzip(v0.9.1), and activesupport(v2.3.5) gems.
gem install rake gem install rubyzip -v 0.9.1 gem install activesupport -v 2.3.5
Follow the instructions here to install msysgit (Git for Windows users) and the instructions here on how to get your ssh keys setup for using Github.
git clone git://github.com/rackspace/csharp-cloudfiles.git
This will create the csharp-cloudfiles directory locally on your computer. Go into that folder and run:
rake
This will compile the project and give you a resulting dll in …csharp-cloudfiles/bin/debug/
Look above for an example folder structure.
Logging is done with the log4net.dll and log4net.config file that are included in the source/downloads. You just need to edit the log4net.config file to turn it on:
change: <log4net debug=“false”> to: <log4net debug=“true”> so that logging starts and you get the desired logging output and format.
Currently the log is going to log/com.mosso.cloudfiles.log, where you have the dll referenced.
<file value=“logs/com.mosso.cloudfiles.log” /> (in the log4net.config file)
Please reference the log4net documentation on how to edit that config file.
Once installed you need to create your integration tests credentials config file.
Run “rake create_credentials_config” from the project solution directory. This will create a Credentials.config file in the com.mosso.cloudfiles.integration.tests project’s bin/debug folder:
<?xml version="1.0" encoding="utf-8"?> <credentials> <username>PUT USERNAME HERE</username> <api_key>PUT API KEY HERE</api_key> </credentials>
Just replace the placeholder text. This file *is not* under source control. It is ignored by the .gitignore file.
See the class definitions for documentation on specific methods and operations.
types are explicitly used in this example. The var keyword could also be used and object/collection initializers could be used. We are being explicit for example
Connect to CloudFiles var userCredentials = new UserCredentials(“username”, “api key”); var connection = new Connection(userCredentials);
<b>Connect to SNet this will eliminate the bandwidth charges that would be associated with your requests otherwise. Please keep in mind that you will still be charged for requests and storage.<b> var userCredentials = new UserCredentials(“username”, “api key”); bool useServiceNet = true; var connection = new Connection(userCredentials, useServiceNet);
Get the account information var accountInformation = connection.GetAccountInformation();
Get the account information as JSON var jsonReturnValue = connection.GetAccountInformationJson();
Get the account information as XML var xmlReturnValue = connection.GetAccountInformationXml();
Create new container connection.CreateContainer(“container name”);
Get container information var container = connection.GetContainerInformation(“container name”);
Get container information as JSON var jsonResponse = connection.GetContainerInformationJson(“container name”);
Get container information as XML var xmlResponse = connection.GetContainerInformationXml(“container name”);
Put item in container with metadata var metadata = new Dictionary{string, string}(); metadata.Add(“key1”, “value1”); metadata.Add(“key2”, “value2”); metadata.Add(“key3”, “value3”); connection.PutStorageItem(“container name”, “C:LocalFilePathfile.txt”, metadata);
Get all the containers for the account var containers = connection.GetContainers();
Put item in container without metadata connection.PutStorageItem(“container name”, “C:LocalFilePathfile.txt”);
Put item in container from stream with metadata var metadata = new Dictionary{string, string}(); metadata.Add(“key1”, “value1”); metadata.Add(“key2”, “value2”); metadata.Add(“key3”, “value3”); var file = new FileInfo(“C:LocalFilePathfile.txt”); connection.PutStorageItem(“container name”, file.Open(FileMode.Open), “RemoteFileName.txt”);
Put item in container from stream var file = new FileInfo(“C:LocalFilePathfile.txt”); connection.PutStorageItem(“container name”, file.Open(FileMode.Open), “RemoteFileName.txt”, metadata);
Make path explicitly with auto-creation of “directory” structure connection.MakePath(“containerName”, “/subdir1/subdir2/subdir3/subdir4”);
List all the items in a container var containerItemList = connection.GetContainerItemList(“container name”);
Shortening the search results by using parameter dictionary var parameters = new Dictionary{GetItemListParameters, string}(); parameters.Add(GetItemListParameters.Limit, 2); parameters.Add(GetItemListParameters.Marker, 1); parameters.Add(GetItemListParameters.Prefix, “a”); var containerItemList = connection.GetContainerItemList(“container name”, parameters);
Get item from container var storageItem = connection.GetStorageItem(“container name”, “RemoteStorageItem.txt”);
Get item from container with request Header fields var requestHeaderFields = Dictionary{RequestHeaderFields, string}(); string dummy_etag = “5c66108b7543c6f16145e25df9849f7f”; requestHeaderFields.Add(RequestHeaderFields.IfMatch, dummy_etag); requestHeaderFields.Add(RequestHeaderFields.IfNoneMatch, dummy_etag); requestHeaderFields.Add(RequestHeaderFields.IfModifiedSince, DateTime.Now.AddDays(6).ToString()); requestHeaderFields.Add(RequestHeaderFields.IfUnmodifiedSince, DateTime.Now.AddDays(-6).ToString()); requestHeaderFields.Add(RequestHeaderFields.Range, “0-5”); var storageItem = connection.GetStorageItem(“container name”, “RemoteStorageItem.txt”, requestHeaderFields);
Get item from container and put straight into local file StorageItem storageItem = connection.GetStorageItem(“container name”, “RemoteStorageItem.txt”, “C:LocalFilePathfile.txt”);
Get item from container and put straight into local file with request Header fields Dictionary<RequestHeaderFields, string> requestHeaderFields = Dictionary<RequestHeaderFields, string>(); string dummy_etag = “5c66108b7543c6f16145e25df9849f7f”; requestHeaderFields.Add(RequestHeaderFields.IfMatch, dummy_etag); requestHeaderFields.Add(RequestHeaderFields.IfNoneMatch, dummy_etag); requestHeaderFields.Add(RequestHeaderFields.IfModifiedSince, DateTime.Now.AddDays(6).ToString()); requestHeaderFields.Add(RequestHeaderFields.IfUnmodifiedSince, DateTime.Now.AddDays(-6).ToString()); requestHeaderFields.Add(RequestHeaderFields.Range, “0-5”); StorageItem storageItem = connection.GetStorageItem(“container name”, “RemoteFileName.txt”, “C:LocalFilePathfile.txt”, requestHeaderFields);
Set meta data information for an item in a container Dictionary<string, string> metadata = new Dictionary<string, string>(); metadata.Add(“key1”, “value1”); metadata.Add(“key2”, “value2”); metadata.Add(“key3”, “value3”); connection.SetStorageItemMetaInformation(“container name”, “C:LocalFilePathfile.txt”, metadata);
Get item information StorageItem storageItem = connection.GetStorageItemInformation(“container name”, “RemoteStorageItem.txt”);
Get a list of the public containers (on the CDN) List<string> containers = connection.GetPublicContainers();
Mark a container as public (available on the CDN) Uri containerPublicUrl = connection.MarkContainerAsPublic(“container name”); Mark a container as public (available on the CDN), with time-to-live (TTL) parameters Uri containerPublicUrl = connection.MarkContainerAsPublic(“container name”, 12345);
Get public container information Container container = connection.GetPublicContainerInformation(“container name”)
Set TTL on public container connection.SetTTLOnPublicContainer(“container name”, 12345); Mark a container as private (remove it from the CDN) connection.MarkContainerAsPrivate(“container name”);
Update a CDN container’s details int ttl = 7200 bool isloggingenabled = true connection.SetDetailsOnPublicContainer(“publiccontainer”, isloggingenabled, ttl, “referreracl”“, ”useragentacl“ );
Delete item from container connection.DeleteStorageItem(“container name”, “RemoteStorageItem.txt”);
Delete container connection.DeleteContainer(“container name”); Upload/Download files asychrononously while displaying a progress bar Create a new Windows form with your progress bar control. Using the code below as an example, create upload and download methods in the code behind for the form that take whatever parameters you’re using for the GetStorageItem and PutStorageItem call.
In your main form, when initiating the download/upload, create a new instance of your form and call the appropriate method.
An example of uploading asynchronously: var p = new ProgressDialog();
p.StartFileTransfer(this, Connection, containerName, localFileName); SetSuccessfulMessageInStatusBar(); //Refresh the container RetrieveContainerItemList();
Code below is an example of what would go in your progress bar form: public void StartFileTransfer(Form owner, Connection connection, string container, string filePath)
{
totalTransferred = 0f;
Text = "Uploading File";
Cursor = Cursors.WaitCursor;
var fi = new FileInfo(filePath);
filesize = fi.Length;
totalBytesLabel.Text = filesize.ToString();
connection.AddProgressWatcher(fileTransferProgress);
connection.OperationComplete += transferComplete;
connection.PutStorageItemAsync(container, filePath);
//It's absolutely critical that ShowDialog is called over Show. ShowDialog sets the dialog to modal, blocking input to any
//other form in the application until the operation is complete. Otherwise, a deadlock occurs if you click the main form,
//hanging the entire application
ShowDialog(owner);
}
private void transferComplete()
{
if (InvokeRequired)
{
Invoke(new CloseCallback(Close), new object[]{});
}
else
{
if (!IsDisposed)
Close();
}
}
private void fileTransferProgress(int bytesTransferred)
{
if (InvokeRequired)
{
Invoke(new FileProgressCallback(fileTransferProgress), new object[] {bytesTransferred});
}
else
{
System.Console.WriteLine(totalTransferred.ToString());
totalTransferred += bytesTransferred;
bytesTransferredLabel.Text = totalTransferred.ToString();
var progress = (int) ((totalTransferred/filesize)*100.0f);
if(progress > 100)
progress = 100;
transferProgressBar.Value = progress ;
}
}
public void StartFileDownload(MainForm owner, Connection connection, long filesize, string container, string storageItemName, string localFileName)
{
Text = "Downloading File";
Cursor = Cursors.WaitCursor;
this.filesize = filesize;
totalBytesLabel.Text = filesize.ToString();
connection.AddProgressWatcher(fileTransferProgress);
connection.OperationComplete += transferComplete;
connection.GetStorageItemAsync(container, storageItemName, localFileName);
//It's absolutely critical that ShowDialog is called over Show. ShowDialog sets the dialog to modal, blocking input to any
//other form in the application until the operation is complete. Otherwise, a deadlock occurs if you click the main form,
//hanging the entire application
ShowDialog(owner);
}
See the Cloud Files Viewer demo for a more concrete example
See COPYING for license information. Copyright © 2008, 2009 2010, 2011, Rackspace US, Inc.