1 // -----------------------------------------------------------------------
2 // <copyright file="CloudFilesClient.cs" company="GRNET">
3 // Copyright 2011 GRNET S.A. All rights reserved.
5 // Redistribution and use in source and binary forms, with or
6 // without modification, are permitted provided that the following
9 // 1. Redistributions of source code must retain the above
10 // copyright notice, this list of conditions and the following
13 // 2. Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials
16 // provided with the distribution.
18 // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
31 // The views and conclusions contained in the software and
32 // documentation are those of the authors and should not be
33 // interpreted as representing official policies, either expressed
34 // or implied, of GRNET S.A.
36 // -----------------------------------------------------------------------
39 // **CloudFilesClient** provides a simple client interface to CloudFiles and Pithos
41 // The class provides methods to upload/download files, delete files, manage containers
45 using System.Collections.Generic;
46 using System.Collections.Specialized;
47 using System.ComponentModel.Composition;
48 using System.Diagnostics;
49 using System.Diagnostics.Contracts;
53 using System.Security.Cryptography;
55 using System.Threading.Tasks;
56 using Newtonsoft.Json;
57 using Pithos.Interfaces;
60 namespace Pithos.Network
62 [Export(typeof(ICloudClient))]
63 public class CloudFilesClient:ICloudClient
65 //CloudFilesClient uses *_baseClient* internally to communicate with the server
66 //RestClient provides a REST-friendly interface over the standard WebClient.
67 private RestClient _baseClient;
70 //During authentication the client provides a UserName
71 public string UserName { get; set; }
73 //and and ApiKey to the server
74 public string ApiKey { get; set; }
76 //And receives an authentication Token. This token must be provided in ALL other operations,
77 //in the X-Auth-Token header
78 private string _token;
81 get { return _token; }
85 _baseClient.Headers["X-Auth-Token"] = value;
89 //The client also receives a StorageUrl after authentication. All subsequent operations must
91 public Uri StorageUrl { get; set; }
94 protected Uri RootAddressUri { get; set; }
96 private WebProxy _proxy;
99 get { return _proxy; }
103 if (_baseClient != null)
104 _baseClient.Proxy = value;
109 /* private Uri _proxy;
112 get { return _proxy; }
116 if (_baseClient != null)
117 _baseClient.Proxy = new WebProxy(value);
121 public double DownloadPercentLimit { get; set; }
122 public double UploadPercentLimit { get; set; }
124 public string AuthenticationUrl { get; set; }
127 public string VersionPath
129 get { return UsePithos ? "v1" : "v1.0"; }
132 public bool UsePithos { get; set; }
135 private static readonly ILog Log = LogManager.GetLogger("CloudFilesClient");
137 public CloudFilesClient(string userName, string apiKey)
143 public CloudFilesClient(AccountInfo accountInfo)
145 if (accountInfo==null)
146 throw new ArgumentNullException("accountInfo");
147 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
148 Contract.Ensures(StorageUrl != null);
149 Contract.Ensures(_baseClient != null);
150 Contract.Ensures(RootAddressUri != null);
151 Contract.EndContractBlock();
153 _baseClient = new RestClient
155 BaseAddress = accountInfo.StorageUri.ToString(),
160 StorageUrl = accountInfo.StorageUri;
161 Token = accountInfo.Token;
162 UserName = accountInfo.UserName;
164 //Get the root address (StorageUrl without the account)
165 var storageUrl = StorageUrl.AbsoluteUri;
166 var usernameIndex = storageUrl.LastIndexOf(UserName);
167 var rootUrl = storageUrl.Substring(0, usernameIndex);
168 RootAddressUri = new Uri(rootUrl);
172 public AccountInfo Authenticate()
174 if (String.IsNullOrWhiteSpace(UserName))
175 throw new InvalidOperationException("UserName is empty");
176 if (String.IsNullOrWhiteSpace(ApiKey))
177 throw new InvalidOperationException("ApiKey is empty");
178 if (String.IsNullOrWhiteSpace(AuthenticationUrl))
179 throw new InvalidOperationException("AuthenticationUrl is empty");
180 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
181 Contract.Ensures(StorageUrl != null);
182 Contract.Ensures(_baseClient != null);
183 Contract.Ensures(RootAddressUri != null);
184 Contract.EndContractBlock();
187 Log.InfoFormat("[AUTHENTICATE] Start for {0}", UserName);
189 var groups = new List<Group>();
191 using (var authClient = new RestClient{BaseAddress=AuthenticationUrl})
194 authClient.Proxy = Proxy;
196 Contract.Assume(authClient.Headers!=null);
198 authClient.Headers.Add("X-Auth-User", UserName);
199 authClient.Headers.Add("X-Auth-Key", ApiKey);
201 authClient.DownloadStringWithRetry(VersionPath, 3);
203 authClient.AssertStatusOK("Authentication failed");
205 var storageUrl = authClient.GetHeaderValue("X-Storage-Url");
206 if (String.IsNullOrWhiteSpace(storageUrl))
207 throw new InvalidOperationException("Failed to obtain storage url");
209 _baseClient = new RestClient
211 BaseAddress = storageUrl,
217 StorageUrl = new Uri(storageUrl);
219 //Get the root address (StorageUrl without the account)
220 var usernameIndex=storageUrl.LastIndexOf(UserName);
221 var rootUrl = storageUrl.Substring(0, usernameIndex);
222 RootAddressUri = new Uri(rootUrl);
224 var token = authClient.GetHeaderValue("X-Auth-Token");
225 if (String.IsNullOrWhiteSpace(token))
226 throw new InvalidOperationException("Failed to obtain token url");
229 /* var keys = authClient.ResponseHeaders.AllKeys.AsQueryable();
230 groups = (from key in keys
231 where key.StartsWith("X-Account-Group-")
232 let name = key.Substring(16)
233 select new Group(name, authClient.ResponseHeaders[key]))
239 Log.InfoFormat("[AUTHENTICATE] End for {0}", UserName);
242 return new AccountInfo {StorageUri = StorageUrl, Token = Token, UserName = UserName,Groups=groups};
248 public IList<ContainerInfo> ListContainers(string account)
250 using (var client = new RestClient(_baseClient))
252 if (!String.IsNullOrWhiteSpace(account))
253 client.BaseAddress = GetAccountUrl(account);
255 client.Parameters.Clear();
256 client.Parameters.Add("format", "json");
257 var content = client.DownloadStringWithRetry("", 3);
258 client.AssertStatusOK("List Containers failed");
260 if (client.StatusCode == HttpStatusCode.NoContent)
261 return new List<ContainerInfo>();
262 var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(content);
264 foreach (var info in infos)
266 info.Account = account;
273 private string GetAccountUrl(string account)
275 return new Uri(RootAddressUri, new Uri(account,UriKind.Relative)).AbsoluteUri;
278 public IList<ShareAccountInfo> ListSharingAccounts(DateTime? since=null)
280 using (ThreadContext.Stacks["Share"].Push("List Accounts"))
282 if (Log.IsDebugEnabled) Log.DebugFormat("START");
284 using (var client = new RestClient(_baseClient))
286 client.Parameters.Clear();
287 client.Parameters.Add("format", "json");
288 client.IfModifiedSince = since;
290 //Extract the username from the base address
291 client.BaseAddress = RootAddressUri.AbsoluteUri;
293 var content = client.DownloadStringWithRetry(@"", 3);
295 client.AssertStatusOK("ListSharingAccounts failed");
297 //If the result is empty, return an empty list,
298 var infos = String.IsNullOrWhiteSpace(content)
299 ? new List<ShareAccountInfo>()
300 //Otherwise deserialize the account list into a list of ShareAccountInfos
301 : JsonConvert.DeserializeObject<IList<ShareAccountInfo>>(content);
303 Log.DebugFormat("END");
309 //Request listing of all objects in a container modified since a specific time.
310 //If the *since* value is missing, return all objects
311 public IList<ObjectInfo> ListSharedObjects(DateTime? since = null)
314 using (ThreadContext.Stacks["Share"].Push("List Objects"))
316 if (Log.IsDebugEnabled) Log.DebugFormat("START");
318 var objects = new List<ObjectInfo>();
319 var accounts = ListSharingAccounts(since);
320 foreach (var account in accounts)
322 var containers = ListContainers(account.name);
323 foreach (var container in containers)
325 var containerObjects = ListObjects(account.name, container.Name);
326 objects.AddRange(containerObjects);
329 if (Log.IsDebugEnabled) Log.DebugFormat("END");
334 public void SetTags(ObjectInfo target,IDictionary<string,string> tags)
336 if (String.IsNullOrWhiteSpace(Token))
337 throw new InvalidOperationException("The Token is not set");
338 if (StorageUrl == null)
339 throw new InvalidOperationException("The StorageUrl is not set");
341 throw new ArgumentNullException("target");
342 Contract.EndContractBlock();
344 using (ThreadContext.Stacks["Share"].Push("Share Object"))
346 if (Log.IsDebugEnabled) Log.DebugFormat("START");
348 using (var client = new RestClient(_baseClient))
351 client.BaseAddress = GetAccountUrl(target.Account);
353 client.Parameters.Clear();
354 client.Parameters.Add("update", "");
356 foreach (var tag in tags)
358 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
359 client.Headers.Add(headerTag, tag.Value);
362 client.DownloadStringWithRetry(target.Container, 3);
365 client.AssertStatusOK("SetTags failed");
366 //If the status is NOT ACCEPTED we have a problem
367 if (client.StatusCode != HttpStatusCode.Accepted)
369 Log.Error("Failed to set tags");
370 throw new Exception("Failed to set tags");
373 if (Log.IsDebugEnabled) Log.DebugFormat("END");
380 public void ShareObject(string account, string container, string objectName, string shareTo, bool read, bool write)
382 if (String.IsNullOrWhiteSpace(Token))
383 throw new InvalidOperationException("The Token is not set");
384 if (StorageUrl==null)
385 throw new InvalidOperationException("The StorageUrl is not set");
386 if (String.IsNullOrWhiteSpace(container))
387 throw new ArgumentNullException("container");
388 if (String.IsNullOrWhiteSpace(objectName))
389 throw new ArgumentNullException("objectName");
390 if (String.IsNullOrWhiteSpace(account))
391 throw new ArgumentNullException("account");
392 if (String.IsNullOrWhiteSpace(shareTo))
393 throw new ArgumentNullException("shareTo");
394 Contract.EndContractBlock();
396 using (ThreadContext.Stacks["Share"].Push("Share Object"))
398 if (Log.IsDebugEnabled) Log.DebugFormat("START");
400 using (var client = new RestClient(_baseClient))
403 client.BaseAddress = GetAccountUrl(account);
405 client.Parameters.Clear();
406 client.Parameters.Add("format", "json");
408 string permission = "";
410 permission = String.Format("write={0}", shareTo);
412 permission = String.Format("read={0}", shareTo);
413 client.Headers.Add("X-Object-Sharing", permission);
415 var content = client.DownloadStringWithRetry(container, 3);
417 client.AssertStatusOK("ShareObject failed");
419 //If the result is empty, return an empty list,
420 var infos = String.IsNullOrWhiteSpace(content)
421 ? new List<ObjectInfo>()
422 //Otherwise deserialize the object list into a list of ObjectInfos
423 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
425 if (Log.IsDebugEnabled) Log.DebugFormat("END");
432 public AccountInfo GetAccountPolicies(AccountInfo accountInfo)
434 if (accountInfo==null)
435 throw new ArgumentNullException("accountInfo");
436 Contract.EndContractBlock();
438 using (ThreadContext.Stacks["Account"].Push("GetPolicies"))
440 if (Log.IsDebugEnabled) Log.DebugFormat("START");
442 using (var client = new RestClient(_baseClient))
444 if (!String.IsNullOrWhiteSpace(accountInfo.UserName))
445 client.BaseAddress = GetAccountUrl(accountInfo.UserName);
447 client.Parameters.Clear();
448 client.Parameters.Add("format", "json");
449 client.Head(String.Empty, 3);
451 var quotaValue=client.ResponseHeaders["X-Account-Policy-Quota"];
452 var bytesValue= client.ResponseHeaders["X-Account-Bytes-Used"];
455 if (long.TryParse(quotaValue, out quota))
456 accountInfo.Quota = quota;
457 if (long.TryParse(bytesValue, out bytes))
458 accountInfo.BytesUsed = bytes;
467 public void UpdateMetadata(ObjectInfo objectInfo)
469 if (objectInfo == null)
470 throw new ArgumentNullException("objectInfo");
471 Contract.EndContractBlock();
473 using (ThreadContext.Stacks["Objects"].Push("UpdateMetadata"))
475 if (Log.IsDebugEnabled) Log.DebugFormat("START");
478 using(var client=new RestClient(_baseClient))
481 client.BaseAddress = GetAccountUrl(objectInfo.Account);
483 client.Parameters.Clear();
487 foreach (var tag in objectInfo.Tags)
489 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
490 client.Headers.Add(headerTag, tag.Value);
495 var permissions=objectInfo.GetPermissionString();
496 client.SetNonEmptyHeaderValue("X-Object-Sharing",permissions);
498 client.SetNonEmptyHeaderValue("Content-Disposition",objectInfo.ContendDisposition);
499 client.SetNonEmptyHeaderValue("Content-Encoding",objectInfo.ContentEncoding);
500 client.SetNonEmptyHeaderValue("X-Object-Manifest",objectInfo.Manifest);
501 var isPublic = objectInfo.IsPublic.ToString().ToLower();
502 client.Headers.Add("X-Object-Public", isPublic);
505 var uriBuilder = client.GetAddressBuilder(objectInfo.Container, objectInfo.Name);
506 var uri = uriBuilder.Uri;
508 client.UploadValues(uri,new NameValueCollection());
511 client.AssertStatusOK("UpdateMetadata failed");
512 //If the status is NOT ACCEPTED or OK we have a problem
513 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
515 Log.Error("Failed to update metadata");
516 throw new Exception("Failed to update metadata");
519 if (Log.IsDebugEnabled) Log.DebugFormat("END");
525 public void UpdateMetadata(ContainerInfo containerInfo)
527 if (containerInfo == null)
528 throw new ArgumentNullException("containerInfo");
529 Contract.EndContractBlock();
531 using (ThreadContext.Stacks["Containers"].Push("UpdateMetadata"))
533 if (Log.IsDebugEnabled) Log.DebugFormat("START");
536 using(var client=new RestClient(_baseClient))
539 client.BaseAddress = GetAccountUrl(containerInfo.Account);
541 client.Parameters.Clear();
545 foreach (var tag in containerInfo.Tags)
547 var headerTag = String.Format("X-Container-Meta-{0}", tag.Key);
548 client.Headers.Add(headerTag, tag.Value);
553 foreach (var policy in containerInfo.Policies)
555 var headerPolicy = String.Format("X-Container-Policy-{0}", policy.Key);
556 client.Headers.Add(headerPolicy, policy.Value);
560 var uriBuilder = client.GetAddressBuilder(containerInfo.Name,"");
561 var uri = uriBuilder.Uri;
563 client.UploadValues(uri,new NameValueCollection());
566 client.AssertStatusOK("UpdateMetadata failed");
567 //If the status is NOT ACCEPTED or OK we have a problem
568 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
570 Log.Error("Failed to update metadata");
571 throw new Exception("Failed to update metadata");
574 if (Log.IsDebugEnabled) Log.DebugFormat("END");
581 public IList<ObjectInfo> ListObjects(string account, string container, DateTime? since = null)
583 if (String.IsNullOrWhiteSpace(container))
584 throw new ArgumentNullException("container");
585 Contract.EndContractBlock();
587 using (ThreadContext.Stacks["Objects"].Push("List"))
589 if (Log.IsDebugEnabled) Log.DebugFormat("START");
591 using (var client = new RestClient(_baseClient))
593 if (!String.IsNullOrWhiteSpace(account))
594 client.BaseAddress = GetAccountUrl(account);
596 client.Parameters.Clear();
597 client.Parameters.Add("format", "json");
598 client.IfModifiedSince = since;
599 var content = client.DownloadStringWithRetry(container, 3);
601 client.AssertStatusOK("ListObjects failed");
603 //If the result is empty, return an empty list,
604 var infos = String.IsNullOrWhiteSpace(content)
605 ? new List<ObjectInfo>()
606 //Otherwise deserialize the object list into a list of ObjectInfos
607 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
609 foreach (var info in infos)
611 info.Container = container;
612 info.Account = account;
614 if (Log.IsDebugEnabled) Log.DebugFormat("START");
622 public IList<ObjectInfo> ListObjects(string account, string container, string folder, DateTime? since = null)
624 if (String.IsNullOrWhiteSpace(container))
625 throw new ArgumentNullException("container");
626 if (String.IsNullOrWhiteSpace(folder))
627 throw new ArgumentNullException("folder");
628 Contract.EndContractBlock();
630 using (ThreadContext.Stacks["Objects"].Push("List"))
632 if (Log.IsDebugEnabled) Log.DebugFormat("START");
634 using (var client = new RestClient(_baseClient))
636 if (!String.IsNullOrWhiteSpace(account))
637 client.BaseAddress = GetAccountUrl(account);
639 client.Parameters.Clear();
640 client.Parameters.Add("format", "json");
641 client.Parameters.Add("path", folder);
642 client.IfModifiedSince = since;
643 var content = client.DownloadStringWithRetry(container, 3);
644 client.AssertStatusOK("ListObjects failed");
646 var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
648 if (Log.IsDebugEnabled) Log.DebugFormat("END");
655 public bool ContainerExists(string account, string container)
657 if (String.IsNullOrWhiteSpace(container))
658 throw new ArgumentNullException("container", "The container property can't be empty");
659 Contract.EndContractBlock();
661 using (ThreadContext.Stacks["Containters"].Push("Exists"))
663 if (Log.IsDebugEnabled) Log.DebugFormat("START");
665 using (var client = new RestClient(_baseClient))
667 if (!String.IsNullOrWhiteSpace(account))
668 client.BaseAddress = GetAccountUrl(account);
670 client.Parameters.Clear();
671 client.Head(container, 3);
674 switch (client.StatusCode)
676 case HttpStatusCode.OK:
677 case HttpStatusCode.NoContent:
680 case HttpStatusCode.NotFound:
684 throw CreateWebException("ContainerExists", client.StatusCode);
686 if (Log.IsDebugEnabled) Log.DebugFormat("END");
694 public bool ObjectExists(string account, string container, string objectName)
696 if (String.IsNullOrWhiteSpace(container))
697 throw new ArgumentNullException("container", "The container property can't be empty");
698 if (String.IsNullOrWhiteSpace(objectName))
699 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
700 Contract.EndContractBlock();
702 using (var client = new RestClient(_baseClient))
704 if (!String.IsNullOrWhiteSpace(account))
705 client.BaseAddress = GetAccountUrl(account);
707 client.Parameters.Clear();
708 client.Head(container + "/" + objectName, 3);
710 switch (client.StatusCode)
712 case HttpStatusCode.OK:
713 case HttpStatusCode.NoContent:
715 case HttpStatusCode.NotFound:
718 throw CreateWebException("ObjectExists", client.StatusCode);
724 public ObjectInfo GetObjectInfo(string account, string container, string objectName)
726 if (String.IsNullOrWhiteSpace(container))
727 throw new ArgumentNullException("container", "The container property can't be empty");
728 if (String.IsNullOrWhiteSpace(objectName))
729 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
730 Contract.EndContractBlock();
732 using (ThreadContext.Stacks["Objects"].Push("GetObjectInfo"))
735 using (var client = new RestClient(_baseClient))
737 if (!String.IsNullOrWhiteSpace(account))
738 client.BaseAddress = GetAccountUrl(account);
741 client.Parameters.Clear();
743 client.Head(container + "/" + objectName, 3);
746 return ObjectInfo.Empty;
748 switch (client.StatusCode)
750 case HttpStatusCode.OK:
751 case HttpStatusCode.NoContent:
752 var keys = client.ResponseHeaders.AllKeys.AsQueryable();
753 var tags = client.GetMeta("X-Object-Meta-");
754 var extensions = (from key in keys
755 where key.StartsWith("X-Object-") && !key.StartsWith("X-Object-Meta-")
756 select new {Name = key, Value = client.ResponseHeaders[key]})
757 .ToDictionary(t => t.Name, t => t.Value);
759 var permissions=client.GetHeaderValue("X-Object-Sharing", true);
762 var info = new ObjectInfo
765 Container = container,
767 Hash = client.GetHeaderValue("ETag"),
768 Content_Type = client.GetHeaderValue("Content-Type"),
769 Bytes = Convert.ToInt64(client.GetHeaderValue("Content-Length",true)),
771 Last_Modified = client.LastModified,
772 Extensions = extensions,
773 ContentEncoding=client.GetHeaderValue("Content-Encoding",true),
774 ContendDisposition = client.GetHeaderValue("Content-Disposition",true),
775 Manifest=client.GetHeaderValue("X-Object-Manifest",true),
776 PublicUrl=client.GetHeaderValue("X-Object-Public",true),
778 info.SetPermissions(permissions);
780 case HttpStatusCode.NotFound:
781 return ObjectInfo.Empty;
783 throw new WebException(
784 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
785 objectName, client.StatusCode));
789 catch (RetryException)
791 Log.WarnFormat("[RETRY FAIL] GetObjectInfo for {0} failed.",objectName);
792 return ObjectInfo.Empty;
794 catch (WebException e)
797 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
798 objectName, client.StatusCode), e);
806 public void CreateFolder(string account, string container, string folder)
808 if (String.IsNullOrWhiteSpace(container))
809 throw new ArgumentNullException("container", "The container property can't be empty");
810 if (String.IsNullOrWhiteSpace(folder))
811 throw new ArgumentNullException("folder", "The folder property can't be empty");
812 Contract.EndContractBlock();
814 var folderUrl=String.Format("{0}/{1}",container,folder);
815 using (var client = new RestClient(_baseClient))
817 if (!String.IsNullOrWhiteSpace(account))
818 client.BaseAddress = GetAccountUrl(account);
820 client.Parameters.Clear();
821 client.Headers.Add("Content-Type", @"application/directory");
822 client.Headers.Add("Content-Length", "0");
823 client.PutWithRetry(folderUrl, 3);
825 if (client.StatusCode != HttpStatusCode.Created && client.StatusCode != HttpStatusCode.Accepted)
826 throw CreateWebException("CreateFolder", client.StatusCode);
832 public ContainerInfo GetContainerInfo(string account, string container)
834 if (String.IsNullOrWhiteSpace(container))
835 throw new ArgumentNullException("container", "The container property can't be empty");
836 Contract.EndContractBlock();
838 using (var client = new RestClient(_baseClient))
840 if (!String.IsNullOrWhiteSpace(account))
841 client.BaseAddress = GetAccountUrl(account);
843 client.Head(container);
844 switch (client.StatusCode)
846 case HttpStatusCode.OK:
847 case HttpStatusCode.NoContent:
848 var tags = client.GetMeta("X-Container-Meta-");
849 var policies = client.GetMeta("X-Container-Policy-");
851 var containerInfo = new ContainerInfo
856 long.Parse(client.GetHeaderValue("X-Container-Object-Count")),
857 Bytes = long.Parse(client.GetHeaderValue("X-Container-Bytes-Used")),
858 BlockHash = client.GetHeaderValue("X-Container-Block-Hash"),
859 BlockSize=int.Parse(client.GetHeaderValue("X-Container-Block-Size")),
860 Last_Modified=client.LastModified,
866 return containerInfo;
867 case HttpStatusCode.NotFound:
868 return ContainerInfo.Empty;
870 throw CreateWebException("GetContainerInfo", client.StatusCode);
875 public void CreateContainer(string account, string container)
877 if (String.IsNullOrWhiteSpace(account))
878 throw new ArgumentNullException("account");
879 if (String.IsNullOrWhiteSpace(container))
880 throw new ArgumentNullException("container");
881 Contract.EndContractBlock();
883 using (var client = new RestClient(_baseClient))
885 if (!String.IsNullOrWhiteSpace(account))
886 client.BaseAddress = GetAccountUrl(account);
888 client.PutWithRetry(container, 3);
889 var expectedCodes = new[] {HttpStatusCode.Created, HttpStatusCode.Accepted, HttpStatusCode.OK};
890 if (!expectedCodes.Contains(client.StatusCode))
891 throw CreateWebException("CreateContainer", client.StatusCode);
895 public void DeleteContainer(string account, string container)
897 if (String.IsNullOrWhiteSpace(container))
898 throw new ArgumentNullException("container", "The container property can't be empty");
899 Contract.EndContractBlock();
901 using (var client = new RestClient(_baseClient))
903 if (!String.IsNullOrWhiteSpace(account))
904 client.BaseAddress = GetAccountUrl(account);
906 client.DeleteWithRetry(container, 3);
907 var expectedCodes = new[] {HttpStatusCode.NotFound, HttpStatusCode.NoContent};
908 if (!expectedCodes.Contains(client.StatusCode))
909 throw CreateWebException("DeleteContainer", client.StatusCode);
917 /// <param name="account"></param>
918 /// <param name="container"></param>
919 /// <param name="objectName"></param>
920 /// <param name="fileName"></param>
921 /// <returns></returns>
922 /// <remarks>This method should have no timeout or a very long one</remarks>
923 //Asynchronously download the object specified by *objectName* in a specific *container* to
925 public Task GetObject(string account, string container, string objectName, string fileName)
927 if (String.IsNullOrWhiteSpace(container))
928 throw new ArgumentNullException("container", "The container property can't be empty");
929 if (String.IsNullOrWhiteSpace(objectName))
930 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
931 Contract.EndContractBlock();
935 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
936 //object to avoid concurrency errors.
938 //Download operations take a long time therefore they have no timeout.
939 var client = new RestClient(_baseClient) { Timeout = 0 };
940 if (!String.IsNullOrWhiteSpace(account))
941 client.BaseAddress = GetAccountUrl(account);
943 //The container and objectName are relative names. They are joined with the client's
944 //BaseAddress to create the object's absolute address
945 var builder = client.GetAddressBuilder(container, objectName);
946 var uri = builder.Uri;
948 //Download progress is reported to the Trace log
949 Log.InfoFormat("[GET] START {0}", objectName);
950 client.DownloadProgressChanged += (sender, args) =>
951 Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
952 fileName, args.ProgressPercentage,
954 args.TotalBytesToReceive);
957 //Start downloading the object asynchronously
958 var downloadTask = client.DownloadFileTask(uri, fileName);
960 //Once the download completes
961 return downloadTask.ContinueWith(download =>
963 //Delete the local client object
965 //And report failure or completion
966 if (download.IsFaulted)
968 Log.ErrorFormat("[GET] FAIL for {0} with \r{1}", objectName,
973 Log.InfoFormat("[GET] END {0}", objectName);
977 catch (Exception exc)
979 Log.ErrorFormat("[GET] END {0} with {1}", objectName, exc);
987 public Task<IList<string>> PutHashMap(string account, string container, string objectName, TreeHash hash)
989 if (String.IsNullOrWhiteSpace(container))
990 throw new ArgumentNullException("container");
991 if (String.IsNullOrWhiteSpace(objectName))
992 throw new ArgumentNullException("objectName");
994 throw new ArgumentNullException("hash");
995 if (String.IsNullOrWhiteSpace(Token))
996 throw new InvalidOperationException("Invalid Token");
997 if (StorageUrl == null)
998 throw new InvalidOperationException("Invalid Storage Url");
999 Contract.EndContractBlock();
1002 //Don't use a timeout because putting the hashmap may be a long process
1003 var client = new RestClient(_baseClient) { Timeout = 0 };
1004 if (!String.IsNullOrWhiteSpace(account))
1005 client.BaseAddress = GetAccountUrl(account);
1007 //The container and objectName are relative names. They are joined with the client's
1008 //BaseAddress to create the object's absolute address
1009 var builder = client.GetAddressBuilder(container, objectName);
1010 builder.Query = "format=json&hashmap";
1011 var uri = builder.Uri;
1014 //Send the tree hash as Json to the server
1015 client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
1016 var jsonHash = hash.ToJson();
1017 var uploadTask=client.UploadStringTask(uri, "PUT", jsonHash);
1019 return uploadTask.ContinueWith(t =>
1022 var empty = (IList<string>)new List<string>();
1025 //The server will respond either with 201-created if all blocks were already on the server
1026 if (client.StatusCode == HttpStatusCode.Created)
1028 //in which case we return an empty hash list
1031 //or with a 409-conflict and return the list of missing parts
1032 //A 409 will cause an exception so we need to check t.IsFaulted to avoid propagating the exception
1035 var ex = t.Exception.InnerException;
1036 var we = ex as WebException;
1037 var response = we.Response as HttpWebResponse;
1038 if (response!=null && response.StatusCode==HttpStatusCode.Conflict)
1040 //In case of 409 the missing parts will be in the response content
1041 using (var stream = response.GetResponseStream())
1042 using(var reader=new StreamReader(stream))
1044 Debug.Assert(stream.Position == 0);
1045 //We need to cleanup the content before returning it because it contains
1046 //error content after the list of hashes
1047 var hashes = new List<string>();
1049 //All lines up to the first empty line are hashes
1050 while(!String.IsNullOrWhiteSpace(line=reader.ReadLine()))
1058 //Any other status code is unexpected and the exception should be rethrown
1062 //Any other status code is unexpected but there was no exception. We can probably continue processing
1063 Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",client.StatusCode,client.StatusDescription);
1070 public Task<byte[]> GetBlock(string account, string container, Uri relativeUrl, long start, long? end)
1072 if (String.IsNullOrWhiteSpace(Token))
1073 throw new InvalidOperationException("Invalid Token");
1074 if (StorageUrl == null)
1075 throw new InvalidOperationException("Invalid Storage Url");
1076 if (String.IsNullOrWhiteSpace(container))
1077 throw new ArgumentNullException("container");
1078 if (relativeUrl== null)
1079 throw new ArgumentNullException("relativeUrl");
1080 if (end.HasValue && end<0)
1081 throw new ArgumentOutOfRangeException("end");
1083 throw new ArgumentOutOfRangeException("start");
1084 Contract.EndContractBlock();
1087 //Don't use a timeout because putting the hashmap may be a long process
1088 var client = new RestClient(_baseClient) {Timeout = 0, RangeFrom = start, RangeTo = end};
1089 if (!String.IsNullOrWhiteSpace(account))
1090 client.BaseAddress = GetAccountUrl(account);
1092 var builder = client.GetAddressBuilder(container, relativeUrl.ToString());
1093 var uri = builder.Uri;
1095 return client.DownloadDataTask(uri)
1104 public async Task PostBlock(string account, string container, byte[] block, int offset, int count)
1106 if (String.IsNullOrWhiteSpace(container))
1107 throw new ArgumentNullException("container");
1109 throw new ArgumentNullException("block");
1110 if (offset < 0 || offset >= block.Length)
1111 throw new ArgumentOutOfRangeException("offset");
1112 if (count < 0 || count > block.Length)
1113 throw new ArgumentOutOfRangeException("count");
1114 if (String.IsNullOrWhiteSpace(Token))
1115 throw new InvalidOperationException("Invalid Token");
1116 if (StorageUrl == null)
1117 throw new InvalidOperationException("Invalid Storage Url");
1118 Contract.EndContractBlock();
1124 //Don't use a timeout because putting the hashmap may be a long process
1125 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1127 if (!String.IsNullOrWhiteSpace(account))
1128 client.BaseAddress = GetAccountUrl(account);
1130 var builder = client.GetAddressBuilder(container, "");
1131 //We are doing an update
1132 builder.Query = "update";
1133 var uri = builder.Uri;
1135 client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
1137 Log.InfoFormat("[BLOCK POST] START");
1139 client.UploadProgressChanged += (sender, args) =>
1140 Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",
1141 args.ProgressPercentage, args.BytesSent,
1142 args.TotalBytesToSend);
1143 client.UploadFileCompleted += (sender, args) =>
1144 Log.InfoFormat("[BLOCK POST PROGRESS] Completed ");
1146 var buffer = new byte[count];
1147 Buffer.BlockCopy(block, offset, buffer, 0, count);
1149 await client.UploadDataTask(uri, "POST", buffer);
1150 Log.InfoFormat("[BLOCK POST] END");
1153 catch (Exception exc)
1155 Log.ErrorFormat("[BLOCK POST] FAIL with \r{0}", exc);
1161 public async Task<TreeHash> GetHashMap(string account, string container, string objectName)
1163 if (String.IsNullOrWhiteSpace(container))
1164 throw new ArgumentNullException("container");
1165 if (String.IsNullOrWhiteSpace(objectName))
1166 throw new ArgumentNullException("objectName");
1167 if (String.IsNullOrWhiteSpace(Token))
1168 throw new InvalidOperationException("Invalid Token");
1169 if (StorageUrl == null)
1170 throw new InvalidOperationException("Invalid Storage Url");
1171 Contract.EndContractBlock();
1175 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
1176 //object to avoid concurrency errors.
1178 //Download operations take a long time therefore they have no timeout.
1179 //TODO: Do they really? this is a hashmap operation, not a download
1181 //Start downloading the object asynchronously
1182 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1184 if (!String.IsNullOrWhiteSpace(account))
1185 client.BaseAddress = GetAccountUrl(account);
1187 //The container and objectName are relative names. They are joined with the client's
1188 //BaseAddress to create the object's absolute address
1189 var builder = client.GetAddressBuilder(container, objectName);
1190 builder.Query = "format=json&hashmap";
1191 var uri = builder.Uri;
1194 var json = await client.DownloadStringTaskAsync(uri);
1195 var treeHash = TreeHash.Parse(json);
1196 Log.InfoFormat("[GET HASH] END {0}", objectName);
1200 catch (Exception exc)
1202 Log.ErrorFormat("[GET HASH] END {0} with {1}", objectName, exc);
1212 /// <param name="account"></param>
1213 /// <param name="container"></param>
1214 /// <param name="objectName"></param>
1215 /// <param name="fileName"></param>
1216 /// <param name="hash">Optional hash value for the file. If no hash is provided, the method calculates a new hash</param>
1217 /// <remarks>>This method should have no timeout or a very long one</remarks>
1218 public async Task PutObject(string account, string container, string objectName, string fileName, string hash = null, string contentType = "application/octet-stream")
1220 if (String.IsNullOrWhiteSpace(container))
1221 throw new ArgumentNullException("container", "The container property can't be empty");
1222 if (String.IsNullOrWhiteSpace(objectName))
1223 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
1224 if (String.IsNullOrWhiteSpace(fileName))
1225 throw new ArgumentNullException("fileName", "The fileName property can't be empty");
1227 if (!File.Exists(fileName) && !Directory.Exists(fileName))
1228 throw new FileNotFoundException("The file or directory does not exist",fileName);
1230 Contract.EndContractBlock();
1235 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1237 if (!String.IsNullOrWhiteSpace(account))
1238 client.BaseAddress = GetAccountUrl(account);
1240 var builder = client.GetAddressBuilder(container, objectName);
1241 var uri = builder.Uri;
1243 string etag = hash ?? CalculateHash(fileName);
1245 client.Headers.Add("Content-Type", contentType);
1246 client.Headers.Add("ETag", etag);
1249 Log.InfoFormat("[PUT] START {0}", objectName);
1250 client.UploadProgressChanged += (sender, args) =>
1252 using (ThreadContext.Stacks["PUT"].Push("Progress"))
1254 Log.InfoFormat("{0} {1}% {2} of {3}", fileName,
1255 args.ProgressPercentage,
1256 args.BytesSent, args.TotalBytesToSend);
1260 client.UploadFileCompleted += (sender, args) =>
1262 using (ThreadContext.Stacks["PUT"].Push("Progress"))
1264 Log.InfoFormat("Completed {0}", fileName);
1267 if (contentType=="application/directory")
1268 await client.UploadDataTaskAsync(uri, "PUT", new byte[0]);
1270 await client.UploadFileTaskAsync(uri, "PUT", fileName);
1273 Log.InfoFormat("[PUT] END {0}", objectName);
1275 catch (Exception exc)
1277 Log.ErrorFormat("[PUT] END {0} with {1}", objectName, exc);
1284 private static string CalculateHash(string fileName)
1286 Contract.Requires(!String.IsNullOrWhiteSpace(fileName));
1287 Contract.EndContractBlock();
1290 using (var hasher = MD5.Create())
1291 using(var stream=File.OpenRead(fileName))
1293 var hashBuilder=new StringBuilder();
1294 foreach (byte b in hasher.ComputeHash(stream))
1295 hashBuilder.Append(b.ToString("x2").ToLower());
1296 hash = hashBuilder.ToString();
1301 public void MoveObject(string account, string sourceContainer, string oldObjectName, string targetContainer, string newObjectName)
1303 if (String.IsNullOrWhiteSpace(sourceContainer))
1304 throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1305 if (String.IsNullOrWhiteSpace(oldObjectName))
1306 throw new ArgumentNullException("oldObjectName", "The oldObjectName property can't be empty");
1307 if (String.IsNullOrWhiteSpace(targetContainer))
1308 throw new ArgumentNullException("targetContainer", "The container property can't be empty");
1309 if (String.IsNullOrWhiteSpace(newObjectName))
1310 throw new ArgumentNullException("newObjectName", "The newObjectName property can't be empty");
1311 Contract.EndContractBlock();
1313 var targetUrl = targetContainer + "/" + newObjectName;
1314 var sourceUrl = String.Format("/{0}/{1}", sourceContainer, oldObjectName);
1316 using (var client = new RestClient(_baseClient))
1318 if (!String.IsNullOrWhiteSpace(account))
1319 client.BaseAddress = GetAccountUrl(account);
1321 client.Headers.Add("X-Move-From", sourceUrl);
1322 client.PutWithRetry(targetUrl, 3);
1324 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
1325 if (!expectedCodes.Contains(client.StatusCode))
1326 throw CreateWebException("MoveObject", client.StatusCode);
1330 public void DeleteObject(string account, string sourceContainer, string objectName)
1332 if (String.IsNullOrWhiteSpace(sourceContainer))
1333 throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1334 if (String.IsNullOrWhiteSpace(objectName))
1335 throw new ArgumentNullException("objectName", "The oldObjectName property can't be empty");
1336 Contract.EndContractBlock();
1338 var targetUrl = FolderConstants.TrashContainer + "/" + objectName;
1339 var sourceUrl = String.Format("/{0}/{1}", sourceContainer, objectName);
1341 using (var client = new RestClient(_baseClient))
1343 if (!String.IsNullOrWhiteSpace(account))
1344 client.BaseAddress = GetAccountUrl(account);
1346 client.Headers.Add("X-Move-From", sourceUrl);
1347 client.AllowedStatusCodes.Add(HttpStatusCode.NotFound);
1348 client.PutWithRetry(targetUrl, 3);
1350 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created,HttpStatusCode.NotFound};
1351 if (!expectedCodes.Contains(client.StatusCode))
1352 throw CreateWebException("DeleteObject", client.StatusCode);
1357 private static WebException CreateWebException(string operation, HttpStatusCode statusCode)
1359 return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode));
1365 public class ShareAccountInfo
1367 public DateTime? last_modified { get; set; }
1368 public string name { get; set; }