if ( hash != info.Hash)
{
this.StatusKeeper.SetFileOverlayStatus(path, FileOverlayStatus.Synch);
- using (var stream = File.OpenRead(path))
- {
- CloudClient.PutObject("PITHOS", fileName, stream, fileSize);
- }
+
+ CloudClient.PutObject("PITHOS", fileName, path, fileSize);
+
}
this.StatusKeeper.SetFileStatus(path,FileStatus.Unchanged);
this.StatusKeeper.SetFileOverlayStatus(path,FileOverlayStatus.Normal);
{
string ApiKey { get; set; }
string UserName { get; set; }
- string StorageUrl { get; set; }
+ Uri StorageUrl { get; set; }
string Token { get; set; }
bool UsePithos { get; set; }
void Authenticate(string userName,string apiKey);
void DeleteContainer(string container);
Stream GetObject(string container, string objectName);
- void PutObject(string container, string objectName, Stream file,long fileSize);
+ void PutObject(string container, string objectName, string fileName,long fileSize);
void DeleteObject(string container, string objectName);
void MoveObject(string container, string oldObjectName, string newObjectName);
bool ObjectExists(string container,string objectName);
{
public string ApiKey { get; set; }
public string UserName { get; set; }
- public string StorageUrl { get; set; }
+ public Uri StorageUrl { get; set; }
public string Token { get; set; }
public bool UsePithos { get; set; }
Contract.Ensures(apiKey==ApiKey);
Contract.Ensures(userName==UserName);
- Contract.Ensures(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Ensures(StorageUrl!=null);
Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
}
public IList<ContainerInfo> ListContainers()
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl!=null);
return default(IList<ContainerInfo>);
}
public IList<ObjectInfo> ListObjects(string container)
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl != null);
Contract.Requires(!String.IsNullOrWhiteSpace(container));
return default(IList<ObjectInfo>);
public bool ContainerExists(string container)
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl!=null);
Contract.Requires(!String.IsNullOrWhiteSpace(container));
return default(bool);
public ContainerInfo GetContainerInfo(string container)
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl!=null);
Contract.Requires(!String.IsNullOrWhiteSpace(container));
return default(ContainerInfo);
public void CreateContainer(string container)
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl!=null);
Contract.Requires(!String.IsNullOrWhiteSpace(container));
}
public void DeleteContainer(string container)
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl!=null);
Contract.Requires(!String.IsNullOrWhiteSpace(container));
}
public Stream GetObject(string container, string objectName)
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl!=null);
Contract.Requires(!String.IsNullOrWhiteSpace(container));
Contract.Requires(!String.IsNullOrWhiteSpace(objectName));
return default(Stream);
}
- public void PutObject(string container, string objectName, Stream file,long fileSize)
+ public void PutObject(string container, string objectName, string fileName,long fileSize)
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl!=null);
Contract.Requires(!String.IsNullOrWhiteSpace(container));
- Contract.Requires(file!=null);
- Contract.Requires(file.CanRead);
+ Contract.Requires(!String.IsNullOrWhiteSpace(fileName));
Contract.Requires(fileSize>=0);
Contract.Requires(!String.IsNullOrWhiteSpace(objectName));
}
public void DeleteObject(string container, string objectName)
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl!=null);
Contract.Requires(!String.IsNullOrWhiteSpace(container));
Contract.Requires(!String.IsNullOrWhiteSpace(objectName));
}
public void MoveObject(string container, string oldObjectName, string newObjectName)
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl!=null);
Contract.Requires(!String.IsNullOrWhiteSpace(container));
Contract.Requires(!String.IsNullOrWhiteSpace(oldObjectName));
Contract.Requires(!String.IsNullOrWhiteSpace(newObjectName));
public bool ObjectExists(string container,string objectName)
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl!=null);
Contract.Requires(!String.IsNullOrWhiteSpace(container));
Contract.Requires(!String.IsNullOrWhiteSpace(objectName));
public ObjectInfo GetObjectInfo(string container,string objectName)
{
Contract.Requires(!String.IsNullOrWhiteSpace(Token));
- Contract.Requires(!String.IsNullOrWhiteSpace(StorageUrl));
+ Contract.Requires(StorageUrl!=null);
Contract.Requires(!String.IsNullOrWhiteSpace(container));
Contract.Requires(!String.IsNullOrWhiteSpace(objectName));
private string _apiKey = "9d3cb7b231e96f72ebe96af1c6cd5112";
private string _userName = "pkanavos";
- private bool _usePithos = false;
+ private bool _usePithos = true;
private ICloudClient client;
[SetUp]
var filePath = "devguide.pdf";
var info=new FileInfo(filePath);
- using (var file = File.OpenRead(filePath))
- {
- var hash = CalculateHash(file);
- file.Seek(0, 0);
- client.PutObject("Shares", info.Name, file, info.Length);
+
+ var hash = CalculateHash(filePath);
+
+ client.PutObject("Shares", info.Name, filePath, info.Length);
var meta = client.GetObjectInfo("Shares", "DeveloperGuide.pdf");
Assert.AreEqual(hash,meta.Hash,String.Format("The hashes don't match, expected {0} but got {1}",hash,meta.Hash));
- }
+
});
}
- private static string CalculateHash(FileStream file)
+ private static string CalculateHash(string fileName)
{
string hash;
using (var hasher = MD5.Create())
+ using(var stream=File.OpenRead(fileName))
{
var hashBuilder = new StringBuilder();
- foreach (byte b in hasher.ComputeHash(file))
+ foreach (byte b in hasher.ComputeHash(stream))
hashBuilder.Append(b.ToString("x2").ToLower());
hash = hashBuilder.ToString();
}
{
private string _apiKey = "9d3cb7b231e96f72ebe96af1c6cd5112";
private string _userName = "pkanavos";
- private bool _usePithos = false;
+
[Test]
- public void TestAuthenticate()
+ public void TestAuthenticate([Values(true,false)]bool usePithos)
{
- ICloudClient client = new CloudFilesClient{UsePithos=_usePithos};
+ ICloudClient client = new CloudFilesClient{UsePithos=usePithos};
client.Authenticate(_userName, _apiKey);
- string storageUrl=client.StorageUrl;
+ Uri storageUrl=client.StorageUrl;
string token = client.Token;
- Assert.IsNotEmpty(storageUrl, "Storage Url was empty");
+ Assert.IsNotNull(storageUrl, "Storage Url was empty");
Assert.IsNotEmpty(token, "Token was empty");
}
[Test]
- public void TestListContainers()
+ public void TestListContainers([Values(true, false)]bool usePithos)
{
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
client.Authenticate(_userName, _apiKey);
IList<ContainerInfo> containers=client.ListContainers();
}
[Test]
- public void TestListObjects()
+ public void TestListObjects([Values(true, false)]bool usePithos)
{
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
client.Authenticate(_userName, _apiKey);
IList<ObjectInfo> objects=client.ListObjects("PITHOS");
}
[Test]
- public void TestContainerExists()
+ public void TestContainerExists([Values(true, false)]bool usePithos)
{
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
client.Authenticate(_userName, _apiKey);
-
- bool dnzExists=client.ContainerExists("DotNetZone");
- bool pithosExists = client.ContainerExists("PITHOS");
+ client.CreateContainer("TestContainer");
+ bool dnzExists=client.ContainerExists("TestContainer");
+
bool mooExists = client.ContainerExists("Moo");
Assert.IsTrue(dnzExists);
- Assert.IsTrue(pithosExists);
+
Assert.IsFalse(mooExists);
}
[Test]
- public void TestGetContainerInfo()
+ public void TestGetContainerInfo([Values(true, false)]bool usePithos)
{
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
client.Authenticate(_userName, _apiKey);
var dnzInfo =client.GetContainerInfo("DotNetZone");
}
[Test]
- public void TestCreateContainer()
+ public void TestCreateContainer([Values(true, false)]bool usePithos)
{
Assert.DoesNotThrow(() =>
{
- ICloudClient client = new CloudFilesClient{UsePithos=_usePithos};
+ ICloudClient client = new CloudFilesClient{UsePithos=usePithos};
client.Authenticate(_userName, _apiKey);
client.CreateContainer("Shares2");
}
[Test]
- public void TestGetObject()
+ public void TestGetObject([Values(true, false)]bool usePithos)
{
Assert.DoesNotThrow(() =>
{
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
client.Authenticate(_userName, _apiKey);
client.CreateContainer("Shares");
}
[Test]
- public void TestPutObject()
+ public void TestPutObject([Values(true, false)]bool usePithos)
{
Assert.DoesNotThrow(() =>
- {
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
- client.Authenticate(_userName, _apiKey);
+ {
+ ICloudClient client = new CloudFilesClient {UsePithos = usePithos};
+ client.Authenticate(_userName, _apiKey);
- client.CreateContainer("Shares");
- Assert.IsTrue(client.ContainerExists("Shares"));
+ client.CreateContainer("Shares");
+ Assert.IsTrue(client.ContainerExists("Shares"));
- var filePath = "devguide.pdf";
- FileInfo info=new FileInfo(filePath);
-
- using (var file = File.OpenRead(filePath))
- {
- client.PutObject("Shares",info.Name, file,info.Length );
- }
+ var filePath = "devguide.pdf";
+ FileInfo info = new FileInfo(filePath);
- });
+
+ client.PutObject("Shares", info.Name, filePath, info.Length);
+
+
+ });
}
[Test]
- public void TestGetObjectMetadata()
+ public void TestGetObjectMetadata([Values(true, false)]bool usePithos)
{
Assert.DoesNotThrow(() =>
{
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
client.Authenticate(_userName, _apiKey);
client.CreateContainer("Shares");
var filePath = "devguide.pdf";
FileInfo info = new FileInfo(filePath);
- using (var file = File.OpenRead(filePath))
- {
- client.PutObject("Shares", info.Name, file, info.Length);
- }
+
+ client.PutObject("Shares", info.Name, filePath, info.Length);
+
}
[Test]
- public void TestDeleteObject()
+ public void TestDeleteObject([Values(true, false)]bool usePithos)
{
Assert.DoesNotThrow(() =>
{
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
client.Authenticate(_userName, _apiKey);
client.CreateContainer("Shares");
var filePath = "devguide.pdf";
FileInfo info=new FileInfo(filePath);
- using (var file = File.OpenRead(filePath))
- {
- client.PutObject("Shares",info.Name, file,info.Length );
- }
+
+ client.PutObject("Shares",info.Name, filePath,info.Length );
+
Assert.IsTrue(client.ObjectExists("Shares",info.Name),"File Created");
- client.DeleteObject("Shares",info.Name);
- Assert.IsFalse(client.ObjectExists("Shares", info.Name),"File Deleted");
+ client.DeleteObject("Shares/devguide.pdf",info.Name);
+ });
+ }
+
+ [Test]
+ public void TestDeleteContainer([Values(true, false)]bool usePithos)
+ {
+ Assert.DoesNotThrow(() =>
+ {
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
+ client.Authenticate(_userName, _apiKey);
+ client.CreateContainer("Shares");
+ Assert.IsTrue(client.ContainerExists("Shares"),"Container Exists");
+ var filePath = "devguide.pdf";
+ FileInfo info=new FileInfo(filePath);
+
+
+ client.PutObject("Shares",info.Name, filePath,info.Length );
+
+
+ Assert.IsTrue(client.ObjectExists("Shares",info.Name),"File Created");
+
+ client.DeleteObject("Shares",info.Name);
+ Assert.IsFalse(client.ObjectExists("Shares", info.Name),"Container Deleted");
+
+ client.DeleteObject("Moo",info.Name);
+ Assert.IsFalse(client.ObjectExists("Moo", info.Name),"Container Deleted");
+
});
}
- [Test]
- public void TestFilesWithSpaces()
+ /* [Test]
+ public void TestFilesWithSpaces([Values(true, false)]bool usePithos)
{
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
client.Authenticate(_userName, _apiKey);
var testName = "Name with spaces.txt";
using(var stream=new MemoryStream())
- using (var writer = new StreamWriter(stream))
+ using (var writer = new File.(stream))
{
writer.WriteLine("This is a test line");
{
client.DeleteObject("PITHOS", testName);
});
- }
+ }*/
[Test]
- public void TestMoveObject()
+ public void TestMoveObject([Values(true, false)]bool usePithos)
{
Assert.DoesNotThrow(() =>
{
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
client.Authenticate(_userName, _apiKey);
client.CreateContainer("Shares");
var filePath = "devguide.pdf";
FileInfo info=new FileInfo(filePath);
- using (var file = File.OpenRead(filePath))
- {
- client.PutObject("Shares",info.Name, file,info.Length );
- }
+
+ client.PutObject("Shares",info.Name, filePath,info.Length );
+
Assert.IsTrue(client.ObjectExists("Shares",info.Name),"File Created");
[Test]
- public void TestAuthenticateMissingArguments()
+ public void TestAuthenticateMissingArguments([Values(true, false)]bool usePithos)
{
Assert.Catch<ArgumentNullException>(() =>
{
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
client.Authenticate("someUser",null);
});
Assert.Catch<ArgumentNullException>(() =>
{
- ICloudClient client = new CloudFilesClient { UsePithos = _usePithos };
+ ICloudClient client = new CloudFilesClient { UsePithos = usePithos };
client.Authenticate(null,"someKey");
});
public class CloudFilesClient:ICloudClient
{
string _rackSpaceAuthUrl = "https://auth.api.rackspacecloud.com/v1.0";
- private string _pithosAuthUrl = "http://pithos.dev.grnet.gr/v1/";
+ private string _pithosAuthUrl = "http://pithos.dev.grnet.gr/v1/Pithos";
private RestClient _client;
private readonly TimeSpan _timeout = TimeSpan.FromSeconds(10);
private readonly int _retries = 5;
public string ApiKey { get; set; }
public string UserName { get; set; }
- public string StorageUrl { get; set; }
+ public Uri StorageUrl { get; set; }
public string Token { get; set; }
public string AuthUrl
if (String.IsNullOrWhiteSpace(storageUrl))
throw new InvalidOperationException("Failed to obtain storage url");
- StorageUrl = storageUrl;
+ StorageUrl = new Uri(storageUrl);
-
- var token = GetHeaderValue("X-Auth-Token",response,keys);
- if (String.IsNullOrWhiteSpace(token))
- throw new InvalidOperationException("Failed to obtain token url");
- Token = token;
+ if (!UsePithos)
+ {
+ var token = GetHeaderValue("X-Auth-Token", response, keys);
+ if (String.IsNullOrWhiteSpace(token))
+ throw new InvalidOperationException("Failed to obtain token url");
+ Token = token;
+ }
+ else
+ Token = "0000";
- _client = new RestClient { Authority = StorageUrl, RetryPolicy = new RetryPolicy { RetryCount = _retries }, Timeout = _timeout };
+ _client = new RestClient { Authority = StorageUrl.AbsoluteUri, RetryPolicy = new RetryPolicy { RetryCount = _retries }, Timeout = _timeout };
_client.RetryPolicy.RetryConditions.Add(new TimeoutRetryCondition());
_client.AddHeader("X-Auth-Token", Token);
if (UsePithos)
throw new ArgumentNullException("container", "The container property can't be empty");
var request = new RestRequest { Path = container, Method = WebMethod.Put };
- var response = _client.Request(request);
+ //var request=(HttpWebRequest)WebRequest.Create(new Uri(StorageUrl, container));
+ //request.Method = "PUT";
+ //request.ContentLength = 0;
+ /* foreach(var header in _client.GetAllHeaders())
+ request.Headers.Add(header.Name,header.Value);*/
+
+ //var response = (HttpWebResponse) request.GetResponse();
+ var response = _client.Request(request);
+
if (response.StatusCode!=HttpStatusCode.Created && response.StatusCode!=HttpStatusCode.Accepted )
throw new WebException(String.Format("ContainerExists failed with unexpected status code {0}", response.StatusCode));
}
throw new WebException(String.Format("GetObject failed with unexpected status code {0}", response.StatusCode));
}
- public void PutObject(string container, string objectName, Stream file,long fileSize)
+ public void PutObject(string container, string objectName, string fileName,long fileSize)
{
if (String.IsNullOrWhiteSpace(container))
throw new ArgumentNullException("container", "The container property can't be empty");
if (String.IsNullOrWhiteSpace(objectName))
throw new ArgumentNullException("objectName", "The objectName property can't be empty");
- if (file==null)
- throw new ArgumentNullException("file", "The file property can't be empty");
+ if (String.IsNullOrWhiteSpace(fileName))
+ throw new ArgumentNullException("fileName", "The fileName property can't be empty");
- string url = StorageUrl + "/" + container + "/" + objectName;
+ string url = StorageUrl + "/" + container + "/" + objectName;
- WebRequest request = WebRequest.Create(url);
- request.Headers["X-Auth-Token"]=Token;
+ //var request = new RestRequest {Path=container + "/" + objectName,Method=WebMethod.Put};
+
+/*
+ var request = (HttpWebRequest)WebRequest.Create(url);
+ //request.Headers["X-Auth-Token"]=Token;
request.Method = "PUT";
- //request.Headers.Add("Content-Length",fileSize.ToString());
- //request.Headers.Add("Content-Type","application/octet-stream");
+ request.ContentLength = fileSize;
+ request.ContentType="application/octet-stream"; */
+
+ string hash = CalculateHash(fileName);
- string hash = CalculateHash(file);
+ //request.Headers["ETag"] = hash;
+ /*request.AddHeader("ETag", hash);
+ request.AddFile(objectName,objectName,file,"application/octet-stream");
+ */
+ /* foreach(var header in _client.GetAllHeaders())
+ request.Headers.Add(header.Name,header.Value);
- request.Headers["ETag"] = hash;
using (var stream = request.GetRequestStream())
{
file.Seek(0, SeekOrigin.Begin);
file.CopyTo(stream);
}
+ request.ContentType = "application/octet-stream";
+*/
+ WebClient c = new WebClient();
+ foreach (var header in _client.GetAllHeaders())
+ c.Headers.Add(header.Name, header.Value);
+ c.Headers["ETag"] = hash;
+
+
+ c.UploadFile(new Uri(url), "PUT",fileName);
+ //var response=_client.Request(request);
+ //var response=request.GetResponse() as HttpWebResponse;
- var response=request.GetResponse() as HttpWebResponse;
-
- if (response.StatusCode == HttpStatusCode.Created)
+ /* if (response.StatusCode == HttpStatusCode.Created)
return;
if (response.StatusCode == HttpStatusCode.LengthRequired)
throw new InvalidOperationException();
else
- throw new WebException(String.Format("GetObject failed with unexpected status code {0}", response.StatusCode));
+ throw new WebException(String.Format("GetObject failed with unexpected status code {0}", response.StatusCode));*/
}
- private static string CalculateHash(Stream file)
+ private static string CalculateHash(string fileName)
{
string hash;
using (var hasher = MD5.Create())
+ using(var stream=File.OpenRead(fileName))
{
var hashBuilder=new StringBuilder();
- foreach (byte b in hasher.ComputeHash(file))
+ foreach (byte b in hasher.ComputeHash(stream))
hashBuilder.Append(b.ToString("x2").ToLower());
hash = hashBuilder.ToString();
}