Skip to content

Rackspace Cloud Files Code Samples

kravi edited this page Apr 13, 2013 · 35 revisions

CloudFilesProvider Samples

Here we will provide samples for basic operation on CloudFilesProvider.

CloudFilesProvider requires we pass in CloudIdentity. We can create CloudIdentity by passing any 2 combination. Username/Password or Username/APIKey.

var cloudIdentity = new CloudIdentity() { Username = "username", Password = "password" }; or var cloudIdentity = new CloudIdentity() { APIKey = "apikey", Username = "username" };

For more information on IdentityProvider

There are 2 ways to pass in the identity credentials to CloudFilesProvider:

  1. In the constructor.

  2. Into each method individually.

Our samples below will assume the identity has been passed into the constructor.

Container

Create Container

var cloudFilesProvider = new CloudFilesProvider(cloudIdentity); ObjectStore createContainerResponse = cloudFilesProvider.CreateContainer("Container Name");

Check the createContainerResponse for status ObjectStore.ContainerCreated for container creation.

Here are the ObjectStore values.

Unknown

ContainerCreated

ContainerExists

ContainerDeleted

ContainerNotEmpty

ContainerNotFound

ObjectDeleted

ObjectCreated

ObjectPurged

Delete Container

var cloudFilesProvider = new CloudFilesProvider(cloudIdentity); ObjectStore containerDeleteResponse = cloudFilesProvider.DeleteContainer("Container Name");

Check the containerDeleteResponse for status ObjectStore.ContainerDeleted for container creation.

List Container in default region

var cloudFilesProvider = new CloudFilesProvider(cloudIdentity); IEnumerable<Container> containerList = cloudFilesProvider .ListContainers();

List Container in specific region

var cloudFilesProvider = new CloudFilesProvider(cloudIdentity); IEnumerable<Container> containerList = cloudFilesProvider.ListContainers(region:"ord");

List Objects in Container

var cloudFilesProvider = new CloudFilesProvider(cloudIdentity); IEnumerable<ContainerObject> containerObjectList = cloudFilesProvider.ListObjects("container name");

Files (Objects)

Creating Objects from Stream

var cloudFilesProvider = new CloudFilesProvider(cloudIdentity);
string fileName = "file_name.jpg";
Stream stream = System.IO.File.OpenRead("c:\filepath\file_name.jpg");
stream.Position = 0;
cloudFilesProvider.CreateObject("container name", stream, fileName);

Creating Objects from File

var cloudFilesProvider = new CloudFilesProvider(cloudIdentity);
cloudFilesProvider.CreateObjectFromFile("container name", @"c:\directory\test.jpg", "test.jpg");

Save Object to File

var cloudFilesProvider = new CloudFilesProvider(cloudIdentity);
cloudFilesProvider.GetObjectSaveToFile("container name", @"c:\save Directory\", "test.jpg");

Delete Object

var cloudFilesProvider = new CloudFilesProvider(cloudIdentity);
ObjectStore deleteObjectResponse = cloudFilesProvider.DeleteObject("container name", "test.jpg");

Copy Object from one container to another

var cloudFilesProvider = new CloudFilesProvider(cloudIdentity);
ObjectStore copyObjectResponse = cloudFilesProvider.CopyObject("source container name", "source object name", "destination container name", "destination object name");

Copy Object from one container to another using Internal URL

var cloudFilesProvider = new CloudFilesProvider(cloudIdentity);
ObjectStore copyObjectResponse = cloudFilesProvider.CopyObject("source container name", "source object name", "destination container name", "destination object name", useInternalUrl:true);

Account