2 /* -----------------------------------------------------------------------
3 * <copyright file="CloudFilesClient.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * The views and conclusions contained in the software and
35 * documentation are those of the authors and should not be
36 * interpreted as representing official policies, either expressed
37 * or implied, of GRNET S.A.
39 * -----------------------------------------------------------------------
43 // **CloudFilesClient** provides a simple client interface to CloudFiles and Pithos
45 // The class provides methods to upload/download files, delete files, manage containers
49 using System.Collections;
50 using System.Collections.Generic;
51 using System.Collections.Specialized;
52 using System.ComponentModel.Composition;
53 using System.Diagnostics;
54 using System.Diagnostics.Contracts;
58 using System.Reflection;
59 using System.Security.Cryptography;
61 using System.Threading.Tasks;
62 using Newtonsoft.Json;
63 using Pithos.Interfaces;
66 namespace Pithos.Network
68 [Export(typeof(ICloudClient))]
69 public class CloudFilesClient:ICloudClient
71 private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
73 //CloudFilesClient uses *_baseClient* internally to communicate with the server
74 //RestClient provides a REST-friendly interface over the standard WebClient.
75 private RestClient _baseClient;
78 //During authentication the client provides a UserName
79 public string UserName { get; set; }
81 //and and ApiKey to the server
82 public string ApiKey { get; set; }
84 //And receives an authentication Token. This token must be provided in ALL other operations,
85 //in the X-Auth-Token header
86 private string _token;
89 get { return _token; }
93 _baseClient.Headers["X-Auth-Token"] = value;
97 //The client also receives a StorageUrl after authentication. All subsequent operations must
99 public Uri StorageUrl { get; set; }
102 protected Uri RootAddressUri { get; set; }
104 /* private WebProxy _proxy;
105 public WebProxy Proxy
107 get { return _proxy; }
111 if (_baseClient != null)
112 _baseClient.Proxy = value;
117 /* private Uri _proxy;
120 get { return _proxy; }
124 if (_baseClient != null)
125 _baseClient.Proxy = new WebProxy(value);
129 public double DownloadPercentLimit { get; set; }
130 public double UploadPercentLimit { get; set; }
132 public string AuthenticationUrl { get; set; }
135 public string VersionPath
137 get { return UsePithos ? "v1" : "v1.0"; }
140 public bool UsePithos { get; set; }
144 public CloudFilesClient(string userName, string apiKey)
150 public CloudFilesClient(AccountInfo accountInfo)
152 if (accountInfo==null)
153 throw new ArgumentNullException("accountInfo");
154 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
155 Contract.Ensures(StorageUrl != null);
156 Contract.Ensures(_baseClient != null);
157 Contract.Ensures(RootAddressUri != null);
158 Contract.EndContractBlock();
160 _baseClient = new RestClient
162 BaseAddress = accountInfo.StorageUri.ToString(),
166 StorageUrl = accountInfo.StorageUri;
167 Token = accountInfo.Token;
168 UserName = accountInfo.UserName;
170 //Get the root address (StorageUrl without the account)
171 var storageUrl = StorageUrl.AbsoluteUri;
172 var usernameIndex = storageUrl.LastIndexOf(UserName);
173 var rootUrl = storageUrl.Substring(0, usernameIndex);
174 RootAddressUri = new Uri(rootUrl);
178 public AccountInfo Authenticate()
180 if (String.IsNullOrWhiteSpace(UserName))
181 throw new InvalidOperationException("UserName is empty");
182 if (String.IsNullOrWhiteSpace(ApiKey))
183 throw new InvalidOperationException("ApiKey is empty");
184 if (String.IsNullOrWhiteSpace(AuthenticationUrl))
185 throw new InvalidOperationException("AuthenticationUrl is empty");
186 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
187 Contract.Ensures(StorageUrl != null);
188 Contract.Ensures(_baseClient != null);
189 Contract.Ensures(RootAddressUri != null);
190 Contract.EndContractBlock();
193 Log.InfoFormat("[AUTHENTICATE] Start for {0}", UserName);
195 var groups = new List<Group>();
197 using (var authClient = new RestClient{BaseAddress=AuthenticationUrl})
199 /* if (Proxy != null)
200 authClient.Proxy = Proxy;*/
202 Contract.Assume(authClient.Headers!=null);
204 authClient.Headers.Add("X-Auth-User", UserName);
205 authClient.Headers.Add("X-Auth-Key", ApiKey);
207 authClient.DownloadStringWithRetry(VersionPath, 3);
209 authClient.AssertStatusOK("Authentication failed");
211 var storageUrl = authClient.GetHeaderValue("X-Storage-Url");
212 if (String.IsNullOrWhiteSpace(storageUrl))
213 throw new InvalidOperationException("Failed to obtain storage url");
215 _baseClient = new RestClient
217 BaseAddress = storageUrl,
223 StorageUrl = new Uri(storageUrl);
225 //Get the root address (StorageUrl without the account)
226 var usernameIndex=storageUrl.LastIndexOf(UserName);
227 var rootUrl = storageUrl.Substring(0, usernameIndex);
228 RootAddressUri = new Uri(rootUrl);
230 var token = authClient.GetHeaderValue("X-Auth-Token");
231 if (String.IsNullOrWhiteSpace(token))
232 throw new InvalidOperationException("Failed to obtain token url");
235 /* var keys = authClient.ResponseHeaders.AllKeys.AsQueryable();
236 groups = (from key in keys
237 where key.StartsWith("X-Account-Group-")
238 let name = key.Substring(16)
239 select new Group(name, authClient.ResponseHeaders[key]))
245 Log.InfoFormat("[AUTHENTICATE] End for {0}", UserName);
248 return new AccountInfo {StorageUri = StorageUrl, Token = Token, UserName = UserName,Groups=groups};
254 public IList<ContainerInfo> ListContainers(string account)
256 using (var client = new RestClient(_baseClient))
258 if (!String.IsNullOrWhiteSpace(account))
259 client.BaseAddress = GetAccountUrl(account);
261 client.Parameters.Clear();
262 client.Parameters.Add("format", "json");
263 var content = client.DownloadStringWithRetry("", 3);
264 client.AssertStatusOK("List Containers failed");
266 if (client.StatusCode == HttpStatusCode.NoContent)
267 return new List<ContainerInfo>();
268 var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(content);
270 foreach (var info in infos)
272 info.Account = account;
279 private string GetAccountUrl(string account)
281 return new Uri(RootAddressUri, new Uri(account,UriKind.Relative)).AbsoluteUri;
284 public IList<ShareAccountInfo> ListSharingAccounts(DateTime? since=null)
286 using (ThreadContext.Stacks["Share"].Push("List Accounts"))
288 if (Log.IsDebugEnabled) Log.DebugFormat("START");
290 using (var client = new RestClient(_baseClient))
292 client.Parameters.Clear();
293 client.Parameters.Add("format", "json");
294 client.IfModifiedSince = since;
296 //Extract the username from the base address
297 client.BaseAddress = RootAddressUri.AbsoluteUri;
299 var content = client.DownloadStringWithRetry(@"", 3);
301 client.AssertStatusOK("ListSharingAccounts failed");
303 //If the result is empty, return an empty list,
304 var infos = String.IsNullOrWhiteSpace(content)
305 ? new List<ShareAccountInfo>()
306 //Otherwise deserialize the account list into a list of ShareAccountInfos
307 : JsonConvert.DeserializeObject<IList<ShareAccountInfo>>(content);
309 Log.DebugFormat("END");
315 //Request listing of all objects in a container modified since a specific time.
316 //If the *since* value is missing, return all objects
317 public IList<ObjectInfo> ListSharedObjects(DateTime? since = null)
320 using (ThreadContext.Stacks["Share"].Push("List Objects"))
322 if (Log.IsDebugEnabled) Log.DebugFormat("START");
323 //'since' is not used here because we need to have ListObjects return a NoChange result
324 //for all shared accounts,containers
325 var accounts = ListSharingAccounts();
326 var items = from account in accounts
327 let containers = ListContainers(account.name)
328 from container in containers
329 select ListObjects(account.name, container.Name,since);
330 var objects=items.SelectMany(r=> r).ToList();
332 var objects = new List<ObjectInfo>();
333 foreach (var containerObjects in items)
335 objects.AddRange(containerObjects);
338 if (Log.IsDebugEnabled) Log.DebugFormat("END");
343 public void SetTags(ObjectInfo target,IDictionary<string,string> tags)
345 if (String.IsNullOrWhiteSpace(Token))
346 throw new InvalidOperationException("The Token is not set");
347 if (StorageUrl == null)
348 throw new InvalidOperationException("The StorageUrl is not set");
350 throw new ArgumentNullException("target");
351 Contract.EndContractBlock();
353 using (ThreadContext.Stacks["Share"].Push("Share Object"))
355 if (Log.IsDebugEnabled) Log.DebugFormat("START");
357 using (var client = new RestClient(_baseClient))
360 client.BaseAddress = GetAccountUrl(target.Account);
362 client.Parameters.Clear();
363 client.Parameters.Add("update", "");
365 foreach (var tag in tags)
367 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
368 client.Headers.Add(headerTag, tag.Value);
371 client.DownloadStringWithRetry(target.Container, 3);
374 client.AssertStatusOK("SetTags failed");
375 //If the status is NOT ACCEPTED we have a problem
376 if (client.StatusCode != HttpStatusCode.Accepted)
378 Log.Error("Failed to set tags");
379 throw new Exception("Failed to set tags");
382 if (Log.IsDebugEnabled) Log.DebugFormat("END");
389 public void ShareObject(string account, string container, string objectName, string shareTo, bool read, bool write)
391 if (String.IsNullOrWhiteSpace(Token))
392 throw new InvalidOperationException("The Token is not set");
393 if (StorageUrl==null)
394 throw new InvalidOperationException("The StorageUrl is not set");
395 if (String.IsNullOrWhiteSpace(container))
396 throw new ArgumentNullException("container");
397 if (String.IsNullOrWhiteSpace(objectName))
398 throw new ArgumentNullException("objectName");
399 if (String.IsNullOrWhiteSpace(account))
400 throw new ArgumentNullException("account");
401 if (String.IsNullOrWhiteSpace(shareTo))
402 throw new ArgumentNullException("shareTo");
403 Contract.EndContractBlock();
405 using (ThreadContext.Stacks["Share"].Push("Share Object"))
407 if (Log.IsDebugEnabled) Log.DebugFormat("START");
409 using (var client = new RestClient(_baseClient))
412 client.BaseAddress = GetAccountUrl(account);
414 client.Parameters.Clear();
415 client.Parameters.Add("format", "json");
417 string permission = "";
419 permission = String.Format("write={0}", shareTo);
421 permission = String.Format("read={0}", shareTo);
422 client.Headers.Add("X-Object-Sharing", permission);
424 var content = client.DownloadStringWithRetry(container, 3);
426 client.AssertStatusOK("ShareObject failed");
428 //If the result is empty, return an empty list,
429 var infos = String.IsNullOrWhiteSpace(content)
430 ? new List<ObjectInfo>()
431 //Otherwise deserialize the object list into a list of ObjectInfos
432 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
434 if (Log.IsDebugEnabled) Log.DebugFormat("END");
441 public AccountInfo GetAccountPolicies(AccountInfo accountInfo)
443 if (accountInfo==null)
444 throw new ArgumentNullException("accountInfo");
445 Contract.EndContractBlock();
447 using (ThreadContext.Stacks["Account"].Push("GetPolicies"))
449 if (Log.IsDebugEnabled) Log.DebugFormat("START");
451 using (var client = new RestClient(_baseClient))
453 if (!String.IsNullOrWhiteSpace(accountInfo.UserName))
454 client.BaseAddress = GetAccountUrl(accountInfo.UserName);
456 client.Parameters.Clear();
457 client.Parameters.Add("format", "json");
458 client.Head(String.Empty, 3);
460 var quotaValue=client.ResponseHeaders["X-Account-Policy-Quota"];
461 var bytesValue= client.ResponseHeaders["X-Account-Bytes-Used"];
464 if (long.TryParse(quotaValue, out quota))
465 accountInfo.Quota = quota;
466 if (long.TryParse(bytesValue, out bytes))
467 accountInfo.BytesUsed = bytes;
476 public void UpdateMetadata(ObjectInfo objectInfo)
478 if (objectInfo == null)
479 throw new ArgumentNullException("objectInfo");
480 Contract.EndContractBlock();
482 using (ThreadContext.Stacks["Objects"].Push("UpdateMetadata"))
484 if (Log.IsDebugEnabled) Log.DebugFormat("START");
487 using(var client=new RestClient(_baseClient))
490 client.BaseAddress = GetAccountUrl(objectInfo.Account);
492 client.Parameters.Clear();
496 foreach (var tag in objectInfo.Tags)
498 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
499 client.Headers.Add(headerTag, tag.Value);
504 var permissions=objectInfo.GetPermissionString();
505 client.SetNonEmptyHeaderValue("X-Object-Sharing",permissions);
507 client.SetNonEmptyHeaderValue("Content-Disposition",objectInfo.ContendDisposition);
508 client.SetNonEmptyHeaderValue("Content-Encoding",objectInfo.ContentEncoding);
509 client.SetNonEmptyHeaderValue("X-Object-Manifest",objectInfo.Manifest);
510 var isPublic = objectInfo.IsPublic.ToString().ToLower();
511 client.Headers.Add("X-Object-Public", isPublic);
514 var uriBuilder = client.GetAddressBuilder(objectInfo.Container, objectInfo.Name);
515 var uri = uriBuilder.Uri;
517 client.UploadValues(uri,new NameValueCollection());
520 client.AssertStatusOK("UpdateMetadata failed");
521 //If the status is NOT ACCEPTED or OK we have a problem
522 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
524 Log.Error("Failed to update metadata");
525 throw new Exception("Failed to update metadata");
528 if (Log.IsDebugEnabled) Log.DebugFormat("END");
534 public void UpdateMetadata(ContainerInfo containerInfo)
536 if (containerInfo == null)
537 throw new ArgumentNullException("containerInfo");
538 Contract.EndContractBlock();
540 using (ThreadContext.Stacks["Containers"].Push("UpdateMetadata"))
542 if (Log.IsDebugEnabled) Log.DebugFormat("START");
545 using(var client=new RestClient(_baseClient))
548 client.BaseAddress = GetAccountUrl(containerInfo.Account);
550 client.Parameters.Clear();
554 foreach (var tag in containerInfo.Tags)
556 var headerTag = String.Format("X-Container-Meta-{0}", tag.Key);
557 client.Headers.Add(headerTag, tag.Value);
562 foreach (var policy in containerInfo.Policies)
564 var headerPolicy = String.Format("X-Container-Policy-{0}", policy.Key);
565 client.Headers.Add(headerPolicy, policy.Value);
569 var uriBuilder = client.GetAddressBuilder(containerInfo.Name,"");
570 var uri = uriBuilder.Uri;
572 client.UploadValues(uri,new NameValueCollection());
575 client.AssertStatusOK("UpdateMetadata failed");
576 //If the status is NOT ACCEPTED or OK we have a problem
577 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
579 Log.Error("Failed to update metadata");
580 throw new Exception("Failed to update metadata");
583 if (Log.IsDebugEnabled) Log.DebugFormat("END");
590 public IList<ObjectInfo> ListObjects(string account, string container, DateTime? since = null)
592 if (String.IsNullOrWhiteSpace(container))
593 throw new ArgumentNullException("container");
594 Contract.EndContractBlock();
596 using (ThreadContext.Stacks["Objects"].Push("List"))
598 if (Log.IsDebugEnabled) Log.DebugFormat("START");
600 using (var client = new RestClient(_baseClient))
602 if (!String.IsNullOrWhiteSpace(account))
603 client.BaseAddress = GetAccountUrl(account);
605 client.Parameters.Clear();
606 client.Parameters.Add("format", "json");
607 client.IfModifiedSince = since;
608 var content = client.DownloadStringWithRetry(container, 3);
610 client.AssertStatusOK("ListObjects failed");
612 if (client.StatusCode==HttpStatusCode.NotModified)
613 return new[]{new NoModificationInfo(account,container)};
614 //If the result is empty, return an empty list,
615 var infos = String.IsNullOrWhiteSpace(content)
616 ? new List<ObjectInfo>()
617 //Otherwise deserialize the object list into a list of ObjectInfos
618 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
620 foreach (var info in infos)
622 info.Container = container;
623 info.Account = account;
624 info.StorageUri = this.StorageUrl;
626 if (Log.IsDebugEnabled) Log.DebugFormat("END");
632 public IList<ObjectInfo> ListObjects(string account, string container, string folder, DateTime? since = null)
634 if (String.IsNullOrWhiteSpace(container))
635 throw new ArgumentNullException("container");
637 if (String.IsNullOrWhiteSpace(folder))
638 throw new ArgumentNullException("folder");
640 Contract.EndContractBlock();
642 using (ThreadContext.Stacks["Objects"].Push("List"))
644 if (Log.IsDebugEnabled) Log.DebugFormat("START");
646 using (var client = new RestClient(_baseClient))
648 if (!String.IsNullOrWhiteSpace(account))
649 client.BaseAddress = GetAccountUrl(account);
651 client.Parameters.Clear();
652 client.Parameters.Add("format", "json");
653 client.Parameters.Add("path", folder);
654 client.IfModifiedSince = since;
655 var content = client.DownloadStringWithRetry(container, 3);
656 client.AssertStatusOK("ListObjects failed");
658 if (client.StatusCode==HttpStatusCode.NotModified)
659 return new[]{new NoModificationInfo(account,container,folder)};
661 var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
662 foreach (var info in infos)
664 info.Account = account;
665 if (info.Container == null)
666 info.Container = container;
667 info.StorageUri = this.StorageUrl;
669 if (Log.IsDebugEnabled) Log.DebugFormat("END");
676 public bool ContainerExists(string account, string container)
678 if (String.IsNullOrWhiteSpace(container))
679 throw new ArgumentNullException("container", "The container property can't be empty");
680 Contract.EndContractBlock();
682 using (ThreadContext.Stacks["Containters"].Push("Exists"))
684 if (Log.IsDebugEnabled) Log.DebugFormat("START");
686 using (var client = new RestClient(_baseClient))
688 if (!String.IsNullOrWhiteSpace(account))
689 client.BaseAddress = GetAccountUrl(account);
691 client.Parameters.Clear();
692 client.Head(container, 3);
695 switch (client.StatusCode)
697 case HttpStatusCode.OK:
698 case HttpStatusCode.NoContent:
701 case HttpStatusCode.NotFound:
705 throw CreateWebException("ContainerExists", client.StatusCode);
707 if (Log.IsDebugEnabled) Log.DebugFormat("END");
715 public bool ObjectExists(string account, string container, string objectName)
717 if (String.IsNullOrWhiteSpace(container))
718 throw new ArgumentNullException("container", "The container property can't be empty");
719 if (String.IsNullOrWhiteSpace(objectName))
720 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
721 Contract.EndContractBlock();
723 using (var client = new RestClient(_baseClient))
725 if (!String.IsNullOrWhiteSpace(account))
726 client.BaseAddress = GetAccountUrl(account);
728 client.Parameters.Clear();
729 client.Head(container + "/" + objectName, 3);
731 switch (client.StatusCode)
733 case HttpStatusCode.OK:
734 case HttpStatusCode.NoContent:
736 case HttpStatusCode.NotFound:
739 throw CreateWebException("ObjectExists", client.StatusCode);
745 public ObjectInfo GetObjectInfo(string account, string container, string objectName)
747 if (String.IsNullOrWhiteSpace(container))
748 throw new ArgumentNullException("container", "The container property can't be empty");
749 if (String.IsNullOrWhiteSpace(objectName))
750 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
751 Contract.EndContractBlock();
753 using (ThreadContext.Stacks["Objects"].Push("GetObjectInfo"))
756 using (var client = new RestClient(_baseClient))
758 if (!String.IsNullOrWhiteSpace(account))
759 client.BaseAddress = GetAccountUrl(account);
762 client.Parameters.Clear();
764 client.Head(container + "/" + objectName, 3);
767 return ObjectInfo.Empty;
769 switch (client.StatusCode)
771 case HttpStatusCode.OK:
772 case HttpStatusCode.NoContent:
773 var keys = client.ResponseHeaders.AllKeys.AsQueryable();
774 var tags = client.GetMeta("X-Object-Meta-");
775 var extensions = (from key in keys
776 where key.StartsWith("X-Object-") && !key.StartsWith("X-Object-Meta-")
777 select new {Name = key, Value = client.ResponseHeaders[key]})
778 .ToDictionary(t => t.Name, t => t.Value);
780 var permissions=client.GetHeaderValue("X-Object-Sharing", true);
783 var info = new ObjectInfo
786 Container = container,
788 Hash = client.GetHeaderValue("ETag"),
789 Content_Type = client.GetHeaderValue("Content-Type"),
790 Bytes = Convert.ToInt64(client.GetHeaderValue("Content-Length",true)),
792 Last_Modified = client.LastModified,
793 Extensions = extensions,
794 ContentEncoding=client.GetHeaderValue("Content-Encoding",true),
795 ContendDisposition = client.GetHeaderValue("Content-Disposition",true),
796 Manifest=client.GetHeaderValue("X-Object-Manifest",true),
797 PublicUrl=client.GetHeaderValue("X-Object-Public",true),
799 info.SetPermissions(permissions);
801 case HttpStatusCode.NotFound:
802 return ObjectInfo.Empty;
804 throw new WebException(
805 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
806 objectName, client.StatusCode));
810 catch (RetryException)
812 Log.WarnFormat("[RETRY FAIL] GetObjectInfo for {0} failed.",objectName);
813 return ObjectInfo.Empty;
815 catch (WebException e)
818 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
819 objectName, client.StatusCode), e);
827 public void CreateFolder(string account, string container, string folder)
829 if (String.IsNullOrWhiteSpace(container))
830 throw new ArgumentNullException("container", "The container property can't be empty");
831 if (String.IsNullOrWhiteSpace(folder))
832 throw new ArgumentNullException("folder", "The folder property can't be empty");
833 Contract.EndContractBlock();
835 var folderUrl=String.Format("{0}/{1}",container,folder);
836 using (var client = new RestClient(_baseClient))
838 if (!String.IsNullOrWhiteSpace(account))
839 client.BaseAddress = GetAccountUrl(account);
841 client.Parameters.Clear();
842 client.Headers.Add("Content-Type", @"application/directory");
843 client.Headers.Add("Content-Length", "0");
844 client.PutWithRetry(folderUrl, 3);
846 if (client.StatusCode != HttpStatusCode.Created && client.StatusCode != HttpStatusCode.Accepted)
847 throw CreateWebException("CreateFolder", client.StatusCode);
853 public ContainerInfo GetContainerInfo(string account, string container)
855 if (String.IsNullOrWhiteSpace(container))
856 throw new ArgumentNullException("container", "The container property can't be empty");
857 Contract.EndContractBlock();
859 using (var client = new RestClient(_baseClient))
861 if (!String.IsNullOrWhiteSpace(account))
862 client.BaseAddress = GetAccountUrl(account);
864 client.Head(container);
865 switch (client.StatusCode)
867 case HttpStatusCode.OK:
868 case HttpStatusCode.NoContent:
869 var tags = client.GetMeta("X-Container-Meta-");
870 var policies = client.GetMeta("X-Container-Policy-");
872 var containerInfo = new ContainerInfo
877 long.Parse(client.GetHeaderValue("X-Container-Object-Count")),
878 Bytes = long.Parse(client.GetHeaderValue("X-Container-Bytes-Used")),
879 BlockHash = client.GetHeaderValue("X-Container-Block-Hash"),
880 BlockSize=int.Parse(client.GetHeaderValue("X-Container-Block-Size")),
881 Last_Modified=client.LastModified,
887 return containerInfo;
888 case HttpStatusCode.NotFound:
889 return ContainerInfo.Empty;
891 throw CreateWebException("GetContainerInfo", client.StatusCode);
896 public void CreateContainer(string account, string container)
898 if (String.IsNullOrWhiteSpace(account))
899 throw new ArgumentNullException("account");
900 if (String.IsNullOrWhiteSpace(container))
901 throw new ArgumentNullException("container");
902 Contract.EndContractBlock();
904 using (var client = new RestClient(_baseClient))
906 if (!String.IsNullOrWhiteSpace(account))
907 client.BaseAddress = GetAccountUrl(account);
909 client.PutWithRetry(container, 3);
910 var expectedCodes = new[] {HttpStatusCode.Created, HttpStatusCode.Accepted, HttpStatusCode.OK};
911 if (!expectedCodes.Contains(client.StatusCode))
912 throw CreateWebException("CreateContainer", client.StatusCode);
916 public void DeleteContainer(string account, string container)
918 if (String.IsNullOrWhiteSpace(container))
919 throw new ArgumentNullException("container", "The container property can't be empty");
920 Contract.EndContractBlock();
922 using (var client = new RestClient(_baseClient))
924 if (!String.IsNullOrWhiteSpace(account))
925 client.BaseAddress = GetAccountUrl(account);
927 client.DeleteWithRetry(container, 3);
928 var expectedCodes = new[] {HttpStatusCode.NotFound, HttpStatusCode.NoContent};
929 if (!expectedCodes.Contains(client.StatusCode))
930 throw CreateWebException("DeleteContainer", client.StatusCode);
938 /// <param name="account"></param>
939 /// <param name="container"></param>
940 /// <param name="objectName"></param>
941 /// <param name="fileName"></param>
942 /// <returns></returns>
943 /// <remarks>This method should have no timeout or a very long one</remarks>
944 //Asynchronously download the object specified by *objectName* in a specific *container* to
946 public Task GetObject(string account, string container, string objectName, string fileName)
948 if (String.IsNullOrWhiteSpace(container))
949 throw new ArgumentNullException("container", "The container property can't be empty");
950 if (String.IsNullOrWhiteSpace(objectName))
951 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
952 Contract.EndContractBlock();
956 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
957 //object to avoid concurrency errors.
959 //Download operations take a long time therefore they have no timeout.
960 var client = new RestClient(_baseClient) { Timeout = 0 };
961 if (!String.IsNullOrWhiteSpace(account))
962 client.BaseAddress = GetAccountUrl(account);
964 //The container and objectName are relative names. They are joined with the client's
965 //BaseAddress to create the object's absolute address
966 var builder = client.GetAddressBuilder(container, objectName);
967 var uri = builder.Uri;
969 //Download progress is reported to the Trace log
970 Log.InfoFormat("[GET] START {0}", objectName);
971 client.DownloadProgressChanged += (sender, args) =>
972 Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
973 fileName, args.ProgressPercentage,
975 args.TotalBytesToReceive);
978 //Start downloading the object asynchronously
979 var downloadTask = client.DownloadFileTask(uri, fileName);
981 //Once the download completes
982 return downloadTask.ContinueWith(download =>
984 //Delete the local client object
986 //And report failure or completion
987 if (download.IsFaulted)
989 Log.ErrorFormat("[GET] FAIL for {0} with \r{1}", objectName,
994 Log.InfoFormat("[GET] END {0}", objectName);
998 catch (Exception exc)
1000 Log.ErrorFormat("[GET] END {0} with {1}", objectName, exc);
1008 public Task<IList<string>> PutHashMap(string account, string container, string objectName, TreeHash hash)
1010 if (String.IsNullOrWhiteSpace(container))
1011 throw new ArgumentNullException("container");
1012 if (String.IsNullOrWhiteSpace(objectName))
1013 throw new ArgumentNullException("objectName");
1015 throw new ArgumentNullException("hash");
1016 if (String.IsNullOrWhiteSpace(Token))
1017 throw new InvalidOperationException("Invalid Token");
1018 if (StorageUrl == null)
1019 throw new InvalidOperationException("Invalid Storage Url");
1020 Contract.EndContractBlock();
1023 //Don't use a timeout because putting the hashmap may be a long process
1024 var client = new RestClient(_baseClient) { Timeout = 0 };
1025 if (!String.IsNullOrWhiteSpace(account))
1026 client.BaseAddress = GetAccountUrl(account);
1028 //The container and objectName are relative names. They are joined with the client's
1029 //BaseAddress to create the object's absolute address
1030 var builder = client.GetAddressBuilder(container, objectName);
1031 builder.Query = "format=json&hashmap";
1032 var uri = builder.Uri;
1035 //Send the tree hash as Json to the server
1036 client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
1037 var jsonHash = hash.ToJson();
1038 var uploadTask=client.UploadStringTask(uri, "PUT", jsonHash);
1040 return uploadTask.ContinueWith(t =>
1043 var empty = (IList<string>)new List<string>();
1046 //The server will respond either with 201-created if all blocks were already on the server
1047 if (client.StatusCode == HttpStatusCode.Created)
1049 //in which case we return an empty hash list
1052 //or with a 409-conflict and return the list of missing parts
1053 //A 409 will cause an exception so we need to check t.IsFaulted to avoid propagating the exception
1056 var ex = t.Exception.InnerException;
1057 var we = ex as WebException;
1058 var response = we.Response as HttpWebResponse;
1059 if (response!=null && response.StatusCode==HttpStatusCode.Conflict)
1061 //In case of 409 the missing parts will be in the response content
1062 using (var stream = response.GetResponseStream())
1063 using(var reader=new StreamReader(stream))
1065 Debug.Assert(stream.Position == 0);
1066 //We used to have to cleanup the content before returning it because it contains
1067 //error content after the list of hashes
1069 //As of 30/1/2012, the result is a proper Json array so we don't need to read the content
1072 var serializer = new JsonSerializer();
1073 var hashes=(List<string>)serializer.Deserialize(reader, typeof (List<string>));
1078 //Any other status code is unexpected and the exception should be rethrown
1082 //Any other status code is unexpected but there was no exception. We can probably continue processing
1083 Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",client.StatusCode,client.StatusDescription);
1090 public Task<byte[]> GetBlock(string account, string container, Uri relativeUrl, long start, long? end)
1092 if (String.IsNullOrWhiteSpace(Token))
1093 throw new InvalidOperationException("Invalid Token");
1094 if (StorageUrl == null)
1095 throw new InvalidOperationException("Invalid Storage Url");
1096 if (String.IsNullOrWhiteSpace(container))
1097 throw new ArgumentNullException("container");
1098 if (relativeUrl== null)
1099 throw new ArgumentNullException("relativeUrl");
1100 if (end.HasValue && end<0)
1101 throw new ArgumentOutOfRangeException("end");
1103 throw new ArgumentOutOfRangeException("start");
1104 Contract.EndContractBlock();
1107 //Don't use a timeout because putting the hashmap may be a long process
1108 var client = new RestClient(_baseClient) {Timeout = 0, RangeFrom = start, RangeTo = end};
1109 if (!String.IsNullOrWhiteSpace(account))
1110 client.BaseAddress = GetAccountUrl(account);
1112 var builder = client.GetAddressBuilder(container, relativeUrl.ToString());
1113 var uri = builder.Uri;
1115 return client.DownloadDataTask(uri)
1124 public async Task PostBlock(string account, string container, byte[] block, int offset, int count)
1126 if (String.IsNullOrWhiteSpace(container))
1127 throw new ArgumentNullException("container");
1129 throw new ArgumentNullException("block");
1130 if (offset < 0 || offset >= block.Length)
1131 throw new ArgumentOutOfRangeException("offset");
1132 if (count < 0 || count > block.Length)
1133 throw new ArgumentOutOfRangeException("count");
1134 if (String.IsNullOrWhiteSpace(Token))
1135 throw new InvalidOperationException("Invalid Token");
1136 if (StorageUrl == null)
1137 throw new InvalidOperationException("Invalid Storage Url");
1138 Contract.EndContractBlock();
1144 //Don't use a timeout because putting the hashmap may be a long process
1145 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1147 if (!String.IsNullOrWhiteSpace(account))
1148 client.BaseAddress = GetAccountUrl(account);
1150 var builder = client.GetAddressBuilder(container, "");
1151 //We are doing an update
1152 builder.Query = "update";
1153 var uri = builder.Uri;
1155 client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
1157 Log.InfoFormat("[BLOCK POST] START");
1159 client.UploadProgressChanged += (sender, args) =>
1160 Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",
1161 args.ProgressPercentage, args.BytesSent,
1162 args.TotalBytesToSend);
1163 client.UploadFileCompleted += (sender, args) =>
1164 Log.InfoFormat("[BLOCK POST PROGRESS] Completed ");
1166 var buffer = new byte[count];
1167 Buffer.BlockCopy(block, offset, buffer, 0, count);
1169 await client.UploadDataTask(uri, "POST", buffer);
1170 Log.InfoFormat("[BLOCK POST] END");
1173 catch (Exception exc)
1175 Log.ErrorFormat("[BLOCK POST] FAIL with \r{0}", exc);
1181 public async Task<TreeHash> GetHashMap(string account, string container, string objectName)
1183 if (String.IsNullOrWhiteSpace(container))
1184 throw new ArgumentNullException("container");
1185 if (String.IsNullOrWhiteSpace(objectName))
1186 throw new ArgumentNullException("objectName");
1187 if (String.IsNullOrWhiteSpace(Token))
1188 throw new InvalidOperationException("Invalid Token");
1189 if (StorageUrl == null)
1190 throw new InvalidOperationException("Invalid Storage Url");
1191 Contract.EndContractBlock();
1195 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
1196 //object to avoid concurrency errors.
1198 //Download operations take a long time therefore they have no timeout.
1199 //TODO: Do they really? this is a hashmap operation, not a download
1201 //Start downloading the object asynchronously
1202 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1204 if (!String.IsNullOrWhiteSpace(account))
1205 client.BaseAddress = GetAccountUrl(account);
1207 //The container and objectName are relative names. They are joined with the client's
1208 //BaseAddress to create the object's absolute address
1209 var builder = client.GetAddressBuilder(container, objectName);
1210 builder.Query = "format=json&hashmap";
1211 var uri = builder.Uri;
1214 var json = await client.DownloadStringTaskAsync(uri);
1215 var treeHash = TreeHash.Parse(json);
1216 Log.InfoFormat("[GET HASH] END {0}", objectName);
1220 catch (Exception exc)
1222 Log.ErrorFormat("[GET HASH] END {0} with {1}", objectName, exc);
1232 /// <param name="account"></param>
1233 /// <param name="container"></param>
1234 /// <param name="objectName"></param>
1235 /// <param name="fileName"></param>
1236 /// <param name="hash">Optional hash value for the file. If no hash is provided, the method calculates a new hash</param>
1237 /// <remarks>>This method should have no timeout or a very long one</remarks>
1238 public async Task PutObject(string account, string container, string objectName, string fileName, string hash = null, string contentType = "application/octet-stream")
1240 if (String.IsNullOrWhiteSpace(container))
1241 throw new ArgumentNullException("container", "The container property can't be empty");
1242 if (String.IsNullOrWhiteSpace(objectName))
1243 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
1244 if (String.IsNullOrWhiteSpace(fileName))
1245 throw new ArgumentNullException("fileName", "The fileName property can't be empty");
1247 if (!File.Exists(fileName) && !Directory.Exists(fileName))
1248 throw new FileNotFoundException("The file or directory does not exist",fileName);
1250 Contract.EndContractBlock();
1255 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1257 if (!String.IsNullOrWhiteSpace(account))
1258 client.BaseAddress = GetAccountUrl(account);
1260 var builder = client.GetAddressBuilder(container, objectName);
1261 var uri = builder.Uri;
1263 string etag = hash ?? CalculateHash(fileName);
1265 client.Headers.Add("Content-Type", contentType);
1266 client.Headers.Add("ETag", etag);
1269 Log.InfoFormat("[PUT] START {0}", objectName);
1270 client.UploadProgressChanged += (sender, args) =>
1272 using (ThreadContext.Stacks["PUT"].Push("Progress"))
1274 Log.InfoFormat("{0} {1}% {2} of {3}", fileName,
1275 args.ProgressPercentage,
1276 args.BytesSent, args.TotalBytesToSend);
1280 client.UploadFileCompleted += (sender, args) =>
1282 using (ThreadContext.Stacks["PUT"].Push("Progress"))
1284 Log.InfoFormat("Completed {0}", fileName);
1287 if (contentType=="application/directory")
1288 await client.UploadDataTaskAsync(uri, "PUT", new byte[0]);
1290 await client.UploadFileTaskAsync(uri, "PUT", fileName);
1293 Log.InfoFormat("[PUT] END {0}", objectName);
1295 catch (Exception exc)
1297 Log.ErrorFormat("[PUT] END {0} with {1}", objectName, exc);
1304 private static string CalculateHash(string fileName)
1306 Contract.Requires(!String.IsNullOrWhiteSpace(fileName));
1307 Contract.EndContractBlock();
1310 using (var hasher = MD5.Create())
1311 using(var stream=File.OpenRead(fileName))
1313 var hashBuilder=new StringBuilder();
1314 foreach (byte b in hasher.ComputeHash(stream))
1315 hashBuilder.Append(b.ToString("x2").ToLower());
1316 hash = hashBuilder.ToString();
1321 public void MoveObject(string account, string sourceContainer, string oldObjectName, string targetContainer, string newObjectName)
1323 if (String.IsNullOrWhiteSpace(sourceContainer))
1324 throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1325 if (String.IsNullOrWhiteSpace(oldObjectName))
1326 throw new ArgumentNullException("oldObjectName", "The oldObjectName property can't be empty");
1327 if (String.IsNullOrWhiteSpace(targetContainer))
1328 throw new ArgumentNullException("targetContainer", "The container property can't be empty");
1329 if (String.IsNullOrWhiteSpace(newObjectName))
1330 throw new ArgumentNullException("newObjectName", "The newObjectName property can't be empty");
1331 Contract.EndContractBlock();
1333 var targetUrl = targetContainer + "/" + newObjectName;
1334 var sourceUrl = String.Format("/{0}/{1}", sourceContainer, oldObjectName);
1336 using (var client = new RestClient(_baseClient))
1338 if (!String.IsNullOrWhiteSpace(account))
1339 client.BaseAddress = GetAccountUrl(account);
1341 client.Headers.Add("X-Move-From", sourceUrl);
1342 client.PutWithRetry(targetUrl, 3);
1344 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
1345 if (!expectedCodes.Contains(client.StatusCode))
1346 throw CreateWebException("MoveObject", client.StatusCode);
1350 public void DeleteObject(string account, string sourceContainer, string objectName)
1352 if (String.IsNullOrWhiteSpace(sourceContainer))
1353 throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1354 if (String.IsNullOrWhiteSpace(objectName))
1355 throw new ArgumentNullException("objectName", "The oldObjectName property can't be empty");
1356 Contract.EndContractBlock();
1358 var targetUrl = FolderConstants.TrashContainer + "/" + objectName;
1359 var sourceUrl = String.Format("/{0}/{1}", sourceContainer, objectName);
1361 using (var client = new RestClient(_baseClient))
1363 if (!String.IsNullOrWhiteSpace(account))
1364 client.BaseAddress = GetAccountUrl(account);
1366 client.Headers.Add("X-Move-From", sourceUrl);
1367 client.AllowedStatusCodes.Add(HttpStatusCode.NotFound);
1368 client.PutWithRetry(targetUrl, 3);
1370 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created,HttpStatusCode.NotFound};
1371 if (!expectedCodes.Contains(client.StatusCode))
1372 throw CreateWebException("DeleteObject", client.StatusCode);
1377 private static WebException CreateWebException(string operation, HttpStatusCode statusCode)
1379 return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode));
1384 public IEnumerable<ObjectInfo> ListDirectories(ContainerInfo container)
1386 var directories=this.ListObjects(container.Account, container.Name, "/");
1391 public class ShareAccountInfo
1393 public DateTime? last_modified { get; set; }
1394 public string name { get; set; }