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.Generic;
50 using System.Collections.Specialized;
51 using System.ComponentModel.Composition;
52 using System.Diagnostics;
53 using System.Diagnostics.Contracts;
57 using System.Reflection;
58 using System.Security.Cryptography;
60 using System.Threading.Tasks;
61 using Newtonsoft.Json;
62 using Pithos.Interfaces;
65 namespace Pithos.Network
67 [Export(typeof(ICloudClient))]
68 public class CloudFilesClient:ICloudClient
70 private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
72 //CloudFilesClient uses *_baseClient* internally to communicate with the server
73 //RestClient provides a REST-friendly interface over the standard WebClient.
74 private RestClient _baseClient;
77 //During authentication the client provides a UserName
78 public string UserName { get; set; }
80 //and and ApiKey to the server
81 public string ApiKey { get; set; }
83 //And receives an authentication Token. This token must be provided in ALL other operations,
84 //in the X-Auth-Token header
85 private string _token;
88 get { return _token; }
92 _baseClient.Headers["X-Auth-Token"] = value;
96 //The client also receives a StorageUrl after authentication. All subsequent operations must
98 public Uri StorageUrl { get; set; }
101 protected Uri RootAddressUri { get; set; }
103 /* private WebProxy _proxy;
104 public WebProxy Proxy
106 get { return _proxy; }
110 if (_baseClient != null)
111 _baseClient.Proxy = value;
116 /* private Uri _proxy;
119 get { return _proxy; }
123 if (_baseClient != null)
124 _baseClient.Proxy = new WebProxy(value);
128 public double DownloadPercentLimit { get; set; }
129 public double UploadPercentLimit { get; set; }
131 public string AuthenticationUrl { get; set; }
134 public string VersionPath
136 get { return UsePithos ? "v1" : "v1.0"; }
139 public bool UsePithos { get; set; }
143 public CloudFilesClient(string userName, string apiKey)
149 public CloudFilesClient(AccountInfo accountInfo)
151 if (accountInfo==null)
152 throw new ArgumentNullException("accountInfo");
153 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
154 Contract.Ensures(StorageUrl != null);
155 Contract.Ensures(_baseClient != null);
156 Contract.Ensures(RootAddressUri != null);
157 Contract.EndContractBlock();
159 _baseClient = new RestClient
161 BaseAddress = accountInfo.StorageUri.ToString(),
165 StorageUrl = accountInfo.StorageUri;
166 Token = accountInfo.Token;
167 UserName = accountInfo.UserName;
169 //Get the root address (StorageUrl without the account)
170 var storageUrl = StorageUrl.AbsoluteUri;
171 var usernameIndex = storageUrl.LastIndexOf(UserName);
172 var rootUrl = storageUrl.Substring(0, usernameIndex);
173 RootAddressUri = new Uri(rootUrl);
177 public AccountInfo Authenticate()
179 if (String.IsNullOrWhiteSpace(UserName))
180 throw new InvalidOperationException("UserName is empty");
181 if (String.IsNullOrWhiteSpace(ApiKey))
182 throw new InvalidOperationException("ApiKey is empty");
183 if (String.IsNullOrWhiteSpace(AuthenticationUrl))
184 throw new InvalidOperationException("AuthenticationUrl is empty");
185 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
186 Contract.Ensures(StorageUrl != null);
187 Contract.Ensures(_baseClient != null);
188 Contract.Ensures(RootAddressUri != null);
189 Contract.EndContractBlock();
192 Log.InfoFormat("[AUTHENTICATE] Start for {0}", UserName);
194 var groups = new List<Group>();
196 using (var authClient = new RestClient{BaseAddress=AuthenticationUrl})
198 /* if (Proxy != null)
199 authClient.Proxy = Proxy;*/
201 Contract.Assume(authClient.Headers!=null);
203 authClient.Headers.Add("X-Auth-User", UserName);
204 authClient.Headers.Add("X-Auth-Key", ApiKey);
206 authClient.DownloadStringWithRetry(VersionPath, 3);
208 authClient.AssertStatusOK("Authentication failed");
210 var storageUrl = authClient.GetHeaderValue("X-Storage-Url");
211 if (String.IsNullOrWhiteSpace(storageUrl))
212 throw new InvalidOperationException("Failed to obtain storage url");
214 _baseClient = new RestClient
216 BaseAddress = storageUrl,
222 StorageUrl = new Uri(storageUrl);
224 //Get the root address (StorageUrl without the account)
225 var usernameIndex=storageUrl.LastIndexOf(UserName);
226 var rootUrl = storageUrl.Substring(0, usernameIndex);
227 RootAddressUri = new Uri(rootUrl);
229 var token = authClient.GetHeaderValue("X-Auth-Token");
230 if (String.IsNullOrWhiteSpace(token))
231 throw new InvalidOperationException("Failed to obtain token url");
234 /* var keys = authClient.ResponseHeaders.AllKeys.AsQueryable();
235 groups = (from key in keys
236 where key.StartsWith("X-Account-Group-")
237 let name = key.Substring(16)
238 select new Group(name, authClient.ResponseHeaders[key]))
244 Log.InfoFormat("[AUTHENTICATE] End for {0}", UserName);
245 Debug.Assert(_baseClient!=null);
247 return new AccountInfo {StorageUri = StorageUrl, Token = Token, UserName = UserName,Groups=groups};
253 public IList<ContainerInfo> ListContainers(string account)
255 using (var client = new RestClient(_baseClient))
257 if (!String.IsNullOrWhiteSpace(account))
258 client.BaseAddress = GetAccountUrl(account);
260 client.Parameters.Clear();
261 client.Parameters.Add("format", "json");
262 var content = client.DownloadStringWithRetry("", 3);
263 client.AssertStatusOK("List Containers failed");
265 if (client.StatusCode == HttpStatusCode.NoContent)
266 return new List<ContainerInfo>();
267 var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(content);
269 foreach (var info in infos)
271 info.Account = account;
278 private string GetAccountUrl(string account)
280 return new Uri(RootAddressUri, new Uri(account,UriKind.Relative)).AbsoluteUri;
283 public IList<ShareAccountInfo> ListSharingAccounts(DateTime? since=null)
285 using (ThreadContext.Stacks["Share"].Push("List Accounts"))
287 if (Log.IsDebugEnabled) Log.DebugFormat("START");
289 using (var client = new RestClient(_baseClient))
291 client.Parameters.Clear();
292 client.Parameters.Add("format", "json");
293 client.IfModifiedSince = since;
295 //Extract the username from the base address
296 client.BaseAddress = RootAddressUri.AbsoluteUri;
298 var content = client.DownloadStringWithRetry(@"", 3);
300 client.AssertStatusOK("ListSharingAccounts failed");
302 //If the result is empty, return an empty list,
303 var infos = String.IsNullOrWhiteSpace(content)
304 ? new List<ShareAccountInfo>()
305 //Otherwise deserialize the account list into a list of ShareAccountInfos
306 : JsonConvert.DeserializeObject<IList<ShareAccountInfo>>(content);
308 Log.DebugFormat("END");
314 //Request listing of all objects in a container modified since a specific time.
315 //If the *since* value is missing, return all objects
316 public IList<ObjectInfo> ListSharedObjects(DateTime? since = null)
319 using (ThreadContext.Stacks["Share"].Push("List Objects"))
321 if (Log.IsDebugEnabled) Log.DebugFormat("START");
322 //'since' is not used here because we need to have ListObjects return a NoChange result
323 //for all shared accounts,containers
324 var accounts = ListSharingAccounts();
325 var items = from account in accounts
326 let containers = ListContainers(account.name)
327 from container in containers
328 select ListObjects(account.name, container.Name,since);
329 var objects=items.SelectMany(r=> r).ToList();
331 var objects = new List<ObjectInfo>();
332 foreach (var containerObjects in items)
334 objects.AddRange(containerObjects);
337 if (Log.IsDebugEnabled) Log.DebugFormat("END");
342 public void SetTags(ObjectInfo target,IDictionary<string,string> tags)
344 if (String.IsNullOrWhiteSpace(Token))
345 throw new InvalidOperationException("The Token is not set");
346 if (StorageUrl == null)
347 throw new InvalidOperationException("The StorageUrl is not set");
349 throw new ArgumentNullException("target");
350 Contract.EndContractBlock();
352 using (ThreadContext.Stacks["Share"].Push("Share Object"))
354 if (Log.IsDebugEnabled) Log.DebugFormat("START");
356 using (var client = new RestClient(_baseClient))
359 client.BaseAddress = GetAccountUrl(target.Account);
361 client.Parameters.Clear();
362 client.Parameters.Add("update", "");
364 foreach (var tag in tags)
366 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
367 client.Headers.Add(headerTag, tag.Value);
370 client.DownloadStringWithRetry(target.Container, 3);
373 client.AssertStatusOK("SetTags failed");
374 //If the status is NOT ACCEPTED we have a problem
375 if (client.StatusCode != HttpStatusCode.Accepted)
377 Log.Error("Failed to set tags");
378 throw new Exception("Failed to set tags");
381 if (Log.IsDebugEnabled) Log.DebugFormat("END");
388 public void ShareObject(string account, string container, string objectName, string shareTo, bool read, bool write)
390 if (String.IsNullOrWhiteSpace(Token))
391 throw new InvalidOperationException("The Token is not set");
392 if (StorageUrl==null)
393 throw new InvalidOperationException("The StorageUrl is not set");
394 if (String.IsNullOrWhiteSpace(container))
395 throw new ArgumentNullException("container");
396 if (String.IsNullOrWhiteSpace(objectName))
397 throw new ArgumentNullException("objectName");
398 if (String.IsNullOrWhiteSpace(account))
399 throw new ArgumentNullException("account");
400 if (String.IsNullOrWhiteSpace(shareTo))
401 throw new ArgumentNullException("shareTo");
402 Contract.EndContractBlock();
404 using (ThreadContext.Stacks["Share"].Push("Share Object"))
406 if (Log.IsDebugEnabled) Log.DebugFormat("START");
408 using (var client = new RestClient(_baseClient))
411 client.BaseAddress = GetAccountUrl(account);
413 client.Parameters.Clear();
414 client.Parameters.Add("format", "json");
416 string permission = "";
418 permission = String.Format("write={0}", shareTo);
420 permission = String.Format("read={0}", shareTo);
421 client.Headers.Add("X-Object-Sharing", permission);
423 var content = client.DownloadStringWithRetry(container, 3);
425 client.AssertStatusOK("ShareObject failed");
427 //If the result is empty, return an empty list,
428 var infos = String.IsNullOrWhiteSpace(content)
429 ? new List<ObjectInfo>()
430 //Otherwise deserialize the object list into a list of ObjectInfos
431 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
433 if (Log.IsDebugEnabled) Log.DebugFormat("END");
440 public AccountInfo GetAccountPolicies(AccountInfo accountInfo)
442 if (accountInfo==null)
443 throw new ArgumentNullException("accountInfo");
444 Contract.EndContractBlock();
446 using (ThreadContext.Stacks["Account"].Push("GetPolicies"))
448 if (Log.IsDebugEnabled) Log.DebugFormat("START");
450 using (var client = new RestClient(_baseClient))
452 if (!String.IsNullOrWhiteSpace(accountInfo.UserName))
453 client.BaseAddress = GetAccountUrl(accountInfo.UserName);
455 client.Parameters.Clear();
456 client.Parameters.Add("format", "json");
457 client.Head(String.Empty, 3);
459 var quotaValue=client.ResponseHeaders["X-Account-Policy-Quota"];
460 var bytesValue= client.ResponseHeaders["X-Account-Bytes-Used"];
463 if (long.TryParse(quotaValue, out quota))
464 accountInfo.Quota = quota;
465 if (long.TryParse(bytesValue, out bytes))
466 accountInfo.BytesUsed = bytes;
475 public void UpdateMetadata(ObjectInfo objectInfo)
477 if (objectInfo == null)
478 throw new ArgumentNullException("objectInfo");
479 Contract.EndContractBlock();
481 using (ThreadContext.Stacks["Objects"].Push("UpdateMetadata"))
483 if (Log.IsDebugEnabled) Log.DebugFormat("START");
486 using(var client=new RestClient(_baseClient))
489 client.BaseAddress = GetAccountUrl(objectInfo.Account);
491 client.Parameters.Clear();
495 foreach (var tag in objectInfo.Tags)
497 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
498 client.Headers.Add(headerTag, tag.Value);
503 var permissions=objectInfo.GetPermissionString();
504 client.SetNonEmptyHeaderValue("X-Object-Sharing",permissions);
506 client.SetNonEmptyHeaderValue("Content-Disposition",objectInfo.ContendDisposition);
507 client.SetNonEmptyHeaderValue("Content-Encoding",objectInfo.ContentEncoding);
508 client.SetNonEmptyHeaderValue("X-Object-Manifest",objectInfo.Manifest);
509 var isPublic = objectInfo.IsPublic.ToString().ToLower();
510 client.Headers.Add("X-Object-Public", isPublic);
513 var uriBuilder = client.GetAddressBuilder(objectInfo.Container, objectInfo.Name);
514 var uri = uriBuilder.Uri;
516 client.UploadValues(uri,new NameValueCollection());
519 client.AssertStatusOK("UpdateMetadata failed");
520 //If the status is NOT ACCEPTED or OK we have a problem
521 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
523 Log.Error("Failed to update metadata");
524 throw new Exception("Failed to update metadata");
527 if (Log.IsDebugEnabled) Log.DebugFormat("END");
533 public void UpdateMetadata(ContainerInfo containerInfo)
535 if (containerInfo == null)
536 throw new ArgumentNullException("containerInfo");
537 Contract.EndContractBlock();
539 using (ThreadContext.Stacks["Containers"].Push("UpdateMetadata"))
541 if (Log.IsDebugEnabled) Log.DebugFormat("START");
544 using(var client=new RestClient(_baseClient))
547 client.BaseAddress = GetAccountUrl(containerInfo.Account);
549 client.Parameters.Clear();
553 foreach (var tag in containerInfo.Tags)
555 var headerTag = String.Format("X-Container-Meta-{0}", tag.Key);
556 client.Headers.Add(headerTag, tag.Value);
561 foreach (var policy in containerInfo.Policies)
563 var headerPolicy = String.Format("X-Container-Policy-{0}", policy.Key);
564 client.Headers.Add(headerPolicy, policy.Value);
568 var uriBuilder = client.GetAddressBuilder(containerInfo.Name,"");
569 var uri = uriBuilder.Uri;
571 client.UploadValues(uri,new NameValueCollection());
574 client.AssertStatusOK("UpdateMetadata failed");
575 //If the status is NOT ACCEPTED or OK we have a problem
576 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
578 Log.Error("Failed to update metadata");
579 throw new Exception("Failed to update metadata");
582 if (Log.IsDebugEnabled) Log.DebugFormat("END");
589 public IList<ObjectInfo> ListObjects(string account, string container, DateTime? since = null)
591 if (String.IsNullOrWhiteSpace(container))
592 throw new ArgumentNullException("container");
593 Contract.EndContractBlock();
595 using (ThreadContext.Stacks["Objects"].Push("List"))
597 if (Log.IsDebugEnabled) Log.DebugFormat("START");
599 using (var client = new RestClient(_baseClient))
601 if (!String.IsNullOrWhiteSpace(account))
602 client.BaseAddress = GetAccountUrl(account);
604 client.Parameters.Clear();
605 client.Parameters.Add("format", "json");
606 client.IfModifiedSince = since;
607 var content = client.DownloadStringWithRetry(container, 3);
609 client.AssertStatusOK("ListObjects failed");
611 if (client.StatusCode==HttpStatusCode.NotModified)
612 return new[]{new NoModificationInfo(account,container)};
613 //If the result is empty, return an empty list,
614 var infos = String.IsNullOrWhiteSpace(content)
615 ? new List<ObjectInfo>()
616 //Otherwise deserialize the object list into a list of ObjectInfos
617 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
619 foreach (var info in infos)
621 info.Container = container;
622 info.Account = account;
623 info.StorageUri = this.StorageUrl;
625 if (Log.IsDebugEnabled) Log.DebugFormat("END");
631 public IList<ObjectInfo> ListObjects(string account, string container, string folder, DateTime? since = null)
633 if (String.IsNullOrWhiteSpace(container))
634 throw new ArgumentNullException("container");
636 if (String.IsNullOrWhiteSpace(folder))
637 throw new ArgumentNullException("folder");
639 Contract.EndContractBlock();
641 using (ThreadContext.Stacks["Objects"].Push("List"))
643 if (Log.IsDebugEnabled) Log.DebugFormat("START");
645 using (var client = new RestClient(_baseClient))
647 if (!String.IsNullOrWhiteSpace(account))
648 client.BaseAddress = GetAccountUrl(account);
650 client.Parameters.Clear();
651 client.Parameters.Add("format", "json");
652 client.Parameters.Add("path", folder);
653 client.IfModifiedSince = since;
654 var content = client.DownloadStringWithRetry(container, 3);
655 client.AssertStatusOK("ListObjects failed");
657 if (client.StatusCode==HttpStatusCode.NotModified)
658 return new[]{new NoModificationInfo(account,container,folder)};
660 var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
661 foreach (var info in infos)
663 info.Account = account;
664 if (info.Container == null)
665 info.Container = container;
666 info.StorageUri = this.StorageUrl;
668 if (Log.IsDebugEnabled) Log.DebugFormat("END");
675 public bool ContainerExists(string account, string container)
677 if (String.IsNullOrWhiteSpace(container))
678 throw new ArgumentNullException("container", "The container property can't be empty");
679 Contract.EndContractBlock();
681 using (ThreadContext.Stacks["Containters"].Push("Exists"))
683 if (Log.IsDebugEnabled) Log.DebugFormat("START");
685 using (var client = new RestClient(_baseClient))
687 if (!String.IsNullOrWhiteSpace(account))
688 client.BaseAddress = GetAccountUrl(account);
690 client.Parameters.Clear();
691 client.Head(container, 3);
694 switch (client.StatusCode)
696 case HttpStatusCode.OK:
697 case HttpStatusCode.NoContent:
700 case HttpStatusCode.NotFound:
704 throw CreateWebException("ContainerExists", client.StatusCode);
706 if (Log.IsDebugEnabled) Log.DebugFormat("END");
714 public bool ObjectExists(string account, string container, string objectName)
716 if (String.IsNullOrWhiteSpace(container))
717 throw new ArgumentNullException("container", "The container property can't be empty");
718 if (String.IsNullOrWhiteSpace(objectName))
719 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
720 Contract.EndContractBlock();
722 using (var client = new RestClient(_baseClient))
724 if (!String.IsNullOrWhiteSpace(account))
725 client.BaseAddress = GetAccountUrl(account);
727 client.Parameters.Clear();
728 client.Head(container + "/" + objectName, 3);
730 switch (client.StatusCode)
732 case HttpStatusCode.OK:
733 case HttpStatusCode.NoContent:
735 case HttpStatusCode.NotFound:
738 throw CreateWebException("ObjectExists", client.StatusCode);
744 public ObjectInfo GetObjectInfo(string account, string container, string objectName)
746 if (String.IsNullOrWhiteSpace(container))
747 throw new ArgumentNullException("container", "The container property can't be empty");
748 if (String.IsNullOrWhiteSpace(objectName))
749 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
750 Contract.EndContractBlock();
752 using (ThreadContext.Stacks["Objects"].Push("GetObjectInfo"))
755 using (var client = new RestClient(_baseClient))
757 if (!String.IsNullOrWhiteSpace(account))
758 client.BaseAddress = GetAccountUrl(account);
761 client.Parameters.Clear();
763 client.Head(container + "/" + objectName, 3);
766 return ObjectInfo.Empty;
768 switch (client.StatusCode)
770 case HttpStatusCode.OK:
771 case HttpStatusCode.NoContent:
772 var keys = client.ResponseHeaders.AllKeys.AsQueryable();
773 var tags = client.GetMeta("X-Object-Meta-");
774 var extensions = (from key in keys
775 where key.StartsWith("X-Object-") && !key.StartsWith("X-Object-Meta-")
776 select new {Name = key, Value = client.ResponseHeaders[key]})
777 .ToDictionary(t => t.Name, t => t.Value);
779 var permissions=client.GetHeaderValue("X-Object-Sharing", true);
782 var info = new ObjectInfo
785 Container = container,
787 Hash = client.GetHeaderValue("ETag"),
788 Content_Type = client.GetHeaderValue("Content-Type"),
789 Bytes = Convert.ToInt64(client.GetHeaderValue("Content-Length",true)),
791 Last_Modified = client.LastModified,
792 Extensions = extensions,
793 ContentEncoding=client.GetHeaderValue("Content-Encoding",true),
794 ContendDisposition = client.GetHeaderValue("Content-Disposition",true),
795 Manifest=client.GetHeaderValue("X-Object-Manifest",true),
796 PublicUrl=client.GetHeaderValue("X-Object-Public",true),
798 info.SetPermissions(permissions);
800 case HttpStatusCode.NotFound:
801 return ObjectInfo.Empty;
803 throw new WebException(
804 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
805 objectName, client.StatusCode));
809 catch (RetryException)
811 Log.WarnFormat("[RETRY FAIL] GetObjectInfo for {0} failed.",objectName);
812 return ObjectInfo.Empty;
814 catch (WebException e)
817 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
818 objectName, client.StatusCode), e);
826 public void CreateFolder(string account, string container, string folder)
828 if (String.IsNullOrWhiteSpace(container))
829 throw new ArgumentNullException("container", "The container property can't be empty");
830 if (String.IsNullOrWhiteSpace(folder))
831 throw new ArgumentNullException("folder", "The folder property can't be empty");
832 Contract.EndContractBlock();
834 var folderUrl=String.Format("{0}/{1}",container,folder);
835 using (var client = new RestClient(_baseClient))
837 if (!String.IsNullOrWhiteSpace(account))
838 client.BaseAddress = GetAccountUrl(account);
840 client.Parameters.Clear();
841 client.Headers.Add("Content-Type", @"application/directory");
842 client.Headers.Add("Content-Length", "0");
843 client.PutWithRetry(folderUrl, 3);
845 if (client.StatusCode != HttpStatusCode.Created && client.StatusCode != HttpStatusCode.Accepted)
846 throw CreateWebException("CreateFolder", client.StatusCode);
852 public ContainerInfo GetContainerInfo(string account, string container)
854 if (String.IsNullOrWhiteSpace(container))
855 throw new ArgumentNullException("container", "The container property can't be empty");
856 Contract.EndContractBlock();
858 using (var client = new RestClient(_baseClient))
860 if (!String.IsNullOrWhiteSpace(account))
861 client.BaseAddress = GetAccountUrl(account);
863 client.Head(container);
864 switch (client.StatusCode)
866 case HttpStatusCode.OK:
867 case HttpStatusCode.NoContent:
868 var tags = client.GetMeta("X-Container-Meta-");
869 var policies = client.GetMeta("X-Container-Policy-");
871 var containerInfo = new ContainerInfo
875 StorageUrl=this.StorageUrl.ToString(),
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);
1039 if (Log.IsDebugEnabled)
1040 Log.DebugFormat("Hashes:\r\n{0}", jsonHash);
1041 return uploadTask.ContinueWith(t =>
1044 var empty = (IList<string>)new List<string>();
1047 //The server will respond either with 201-created if all blocks were already on the server
1048 if (client.StatusCode == HttpStatusCode.Created)
1050 //in which case we return an empty hash list
1053 //or with a 409-conflict and return the list of missing parts
1054 //A 409 will cause an exception so we need to check t.IsFaulted to avoid propagating the exception
1057 var ex = t.Exception.InnerException;
1058 var we = ex as WebException;
1059 var response = we.Response as HttpWebResponse;
1060 if (response!=null && response.StatusCode==HttpStatusCode.Conflict)
1062 //In case of 409 the missing parts will be in the response content
1063 using (var stream = response.GetResponseStream())
1064 using(var reader=stream.GetLoggedReader(Log))
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 serializer.Error += (sender, args) => Log.ErrorFormat("Deserialization error at [{0}] [{1}]", args.ErrorContext.Error, args.ErrorContext.Member);
1074 var hashes = (List<string>)serializer.Deserialize(reader, typeof(List<string>));
1078 //Any other status code is unexpected and the exception should be rethrown
1079 Log.LogError(response);
1084 //Any other status code is unexpected but there was no exception. We can probably continue processing
1085 Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",client.StatusCode,client.StatusDescription);
1093 public Task<byte[]> GetBlock(string account, string container, Uri relativeUrl, long start, long? end)
1095 if (String.IsNullOrWhiteSpace(Token))
1096 throw new InvalidOperationException("Invalid Token");
1097 if (StorageUrl == null)
1098 throw new InvalidOperationException("Invalid Storage Url");
1099 if (String.IsNullOrWhiteSpace(container))
1100 throw new ArgumentNullException("container");
1101 if (relativeUrl== null)
1102 throw new ArgumentNullException("relativeUrl");
1103 if (end.HasValue && end<0)
1104 throw new ArgumentOutOfRangeException("end");
1106 throw new ArgumentOutOfRangeException("start");
1107 Contract.EndContractBlock();
1110 //Don't use a timeout because putting the hashmap may be a long process
1111 var client = new RestClient(_baseClient) {Timeout = 0, RangeFrom = start, RangeTo = end};
1112 if (!String.IsNullOrWhiteSpace(account))
1113 client.BaseAddress = GetAccountUrl(account);
1115 var builder = client.GetAddressBuilder(container, relativeUrl.ToString());
1116 var uri = builder.Uri;
1118 return client.DownloadDataTask(uri)
1127 public async Task PostBlock(string account, string container, byte[] block, int offset, int count)
1129 if (String.IsNullOrWhiteSpace(container))
1130 throw new ArgumentNullException("container");
1132 throw new ArgumentNullException("block");
1133 if (offset < 0 || offset >= block.Length)
1134 throw new ArgumentOutOfRangeException("offset");
1135 if (count < 0 || count > block.Length)
1136 throw new ArgumentOutOfRangeException("count");
1137 if (String.IsNullOrWhiteSpace(Token))
1138 throw new InvalidOperationException("Invalid Token");
1139 if (StorageUrl == null)
1140 throw new InvalidOperationException("Invalid Storage Url");
1141 Contract.EndContractBlock();
1147 //Don't use a timeout because putting the hashmap may be a long process
1148 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1150 if (!String.IsNullOrWhiteSpace(account))
1151 client.BaseAddress = GetAccountUrl(account);
1153 var builder = client.GetAddressBuilder(container, "");
1154 //We are doing an update
1155 builder.Query = "update";
1156 var uri = builder.Uri;
1158 client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
1160 Log.InfoFormat("[BLOCK POST] START");
1162 client.UploadProgressChanged += (sender, args) =>
1163 Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",
1164 args.ProgressPercentage, args.BytesSent,
1165 args.TotalBytesToSend);
1166 client.UploadFileCompleted += (sender, args) =>
1167 Log.InfoFormat("[BLOCK POST PROGRESS] Completed ");
1169 var buffer = new byte[count];
1170 Buffer.BlockCopy(block, offset, buffer, 0, count);
1172 await client.UploadDataTask(uri, "POST", buffer);
1173 Log.InfoFormat("[BLOCK POST] END");
1176 catch (Exception exc)
1178 Log.ErrorFormat("[BLOCK POST] FAIL with \r{0}", exc);
1184 public async Task<TreeHash> GetHashMap(string account, string container, string objectName)
1186 if (String.IsNullOrWhiteSpace(container))
1187 throw new ArgumentNullException("container");
1188 if (String.IsNullOrWhiteSpace(objectName))
1189 throw new ArgumentNullException("objectName");
1190 if (String.IsNullOrWhiteSpace(Token))
1191 throw new InvalidOperationException("Invalid Token");
1192 if (StorageUrl == null)
1193 throw new InvalidOperationException("Invalid Storage Url");
1194 Contract.EndContractBlock();
1198 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
1199 //object to avoid concurrency errors.
1201 //Download operations take a long time therefore they have no timeout.
1202 //TODO: Do they really? this is a hashmap operation, not a download
1204 //Start downloading the object asynchronously
1205 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1207 if (!String.IsNullOrWhiteSpace(account))
1208 client.BaseAddress = GetAccountUrl(account);
1210 //The container and objectName are relative names. They are joined with the client's
1211 //BaseAddress to create the object's absolute address
1212 var builder = client.GetAddressBuilder(container, objectName);
1213 builder.Query = "format=json&hashmap";
1214 var uri = builder.Uri;
1217 var json = await client.DownloadStringTaskAsync(uri);
1218 var treeHash = TreeHash.Parse(json);
1219 Log.InfoFormat("[GET HASH] END {0}", objectName);
1223 catch (Exception exc)
1225 Log.ErrorFormat("[GET HASH] END {0} with {1}", objectName, exc);
1235 /// <param name="account"></param>
1236 /// <param name="container"></param>
1237 /// <param name="objectName"></param>
1238 /// <param name="fileName"></param>
1239 /// <param name="hash">Optional hash value for the file. If no hash is provided, the method calculates a new hash</param>
1240 /// <remarks>>This method should have no timeout or a very long one</remarks>
1241 public async Task PutObject(string account, string container, string objectName, string fileName, string hash = null, string contentType = "application/octet-stream")
1243 if (String.IsNullOrWhiteSpace(container))
1244 throw new ArgumentNullException("container", "The container property can't be empty");
1245 if (String.IsNullOrWhiteSpace(objectName))
1246 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
1247 if (String.IsNullOrWhiteSpace(fileName))
1248 throw new ArgumentNullException("fileName", "The fileName property can't be empty");
1250 if (!File.Exists(fileName) && !Directory.Exists(fileName))
1251 throw new FileNotFoundException("The file or directory does not exist",fileName);
1257 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1259 if (!String.IsNullOrWhiteSpace(account))
1260 client.BaseAddress = GetAccountUrl(account);
1262 var builder = client.GetAddressBuilder(container, objectName);
1263 var uri = builder.Uri;
1265 string etag = hash ?? CalculateHash(fileName);
1267 client.Headers.Add("Content-Type", contentType);
1268 client.Headers.Add("ETag", etag);
1271 Log.InfoFormat("[PUT] START {0}", objectName);
1272 client.UploadProgressChanged += (sender, args) =>
1274 using (ThreadContext.Stacks["PUT"].Push("Progress"))
1276 Log.InfoFormat("{0} {1}% {2} of {3}", fileName,
1277 args.ProgressPercentage,
1278 args.BytesSent, args.TotalBytesToSend);
1282 client.UploadFileCompleted += (sender, args) =>
1284 using (ThreadContext.Stacks["PUT"].Push("Progress"))
1286 Log.InfoFormat("Completed {0}", fileName);
1289 if (contentType=="application/directory")
1290 await client.UploadDataTaskAsync(uri, "PUT", new byte[0]);
1292 await client.UploadFileTaskAsync(uri, "PUT", fileName);
1295 Log.InfoFormat("[PUT] END {0}", objectName);
1297 catch (Exception exc)
1299 Log.ErrorFormat("[PUT] END {0} with {1}", objectName, exc);
1306 private static string CalculateHash(string fileName)
1308 Contract.Requires(!String.IsNullOrWhiteSpace(fileName));
1309 Contract.EndContractBlock();
1312 using (var hasher = MD5.Create())
1313 using(var stream=File.OpenRead(fileName))
1315 var hashBuilder=new StringBuilder();
1316 foreach (byte b in hasher.ComputeHash(stream))
1317 hashBuilder.Append(b.ToString("x2").ToLower());
1318 hash = hashBuilder.ToString();
1323 public void MoveObject(string account, string sourceContainer, string oldObjectName, string targetContainer, string newObjectName)
1325 if (String.IsNullOrWhiteSpace(sourceContainer))
1326 throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1327 if (String.IsNullOrWhiteSpace(oldObjectName))
1328 throw new ArgumentNullException("oldObjectName", "The oldObjectName property can't be empty");
1329 if (String.IsNullOrWhiteSpace(targetContainer))
1330 throw new ArgumentNullException("targetContainer", "The container property can't be empty");
1331 if (String.IsNullOrWhiteSpace(newObjectName))
1332 throw new ArgumentNullException("newObjectName", "The newObjectName property can't be empty");
1333 Contract.EndContractBlock();
1335 var targetUrl = targetContainer + "/" + newObjectName;
1336 var sourceUrl = String.Format("/{0}/{1}", sourceContainer, oldObjectName);
1338 using (var client = new RestClient(_baseClient))
1340 if (!String.IsNullOrWhiteSpace(account))
1341 client.BaseAddress = GetAccountUrl(account);
1343 client.Headers.Add("X-Move-From", sourceUrl);
1344 client.PutWithRetry(targetUrl, 3);
1346 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
1347 if (!expectedCodes.Contains(client.StatusCode))
1348 throw CreateWebException("MoveObject", client.StatusCode);
1352 public void DeleteObject(string account, string sourceContainer, string objectName)
1354 if (String.IsNullOrWhiteSpace(sourceContainer))
1355 throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1356 if (String.IsNullOrWhiteSpace(objectName))
1357 throw new ArgumentNullException("objectName", "The oldObjectName property can't be empty");
1358 Contract.EndContractBlock();
1360 var targetUrl = FolderConstants.TrashContainer + "/" + objectName;
1361 var sourceUrl = String.Format("/{0}/{1}", sourceContainer, objectName);
1363 using (var client = new RestClient(_baseClient))
1365 if (!String.IsNullOrWhiteSpace(account))
1366 client.BaseAddress = GetAccountUrl(account);
1368 client.Headers.Add("X-Move-From", sourceUrl);
1369 client.AllowedStatusCodes.Add(HttpStatusCode.NotFound);
1370 Log.InfoFormat("[TRASH] [{0}] to [{1}]",sourceUrl,targetUrl);
1371 client.PutWithRetry(targetUrl, 3);
1373 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created,HttpStatusCode.NotFound};
1374 if (!expectedCodes.Contains(client.StatusCode))
1375 throw CreateWebException("DeleteObject", client.StatusCode);
1380 private static WebException CreateWebException(string operation, HttpStatusCode statusCode)
1382 return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode));
1387 public IEnumerable<ObjectInfo> ListDirectories(ContainerInfo container)
1389 var directories=this.ListObjects(container.Account, container.Name, "/");
1394 public class ShareAccountInfo
1396 public DateTime? last_modified { get; set; }
1397 public string name { get; set; }