2 /* -----------------------------------------------------------------------
\r
3 * <copyright file="CloudFilesClient.cs" company="GRNet">
\r
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
\r
7 * Redistribution and use in source and binary forms, with or
\r
8 * without modification, are permitted provided that the following
\r
9 * conditions are met:
\r
11 * 1. Redistributions of source code must retain the above
\r
12 * copyright notice, this list of conditions and the following
\r
15 * 2. Redistributions in binary form must reproduce the above
\r
16 * copyright notice, this list of conditions and the following
\r
17 * disclaimer in the documentation and/or other materials
\r
18 * provided with the distribution.
\r
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
\r
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
\r
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
\r
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
\r
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
\r
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\r
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
\r
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
32 * POSSIBILITY OF SUCH DAMAGE.
\r
34 * The views and conclusions contained in the software and
\r
35 * documentation are those of the authors and should not be
\r
36 * interpreted as representing official policies, either expressed
\r
37 * or implied, of GRNET S.A.
\r
39 * -----------------------------------------------------------------------
\r
43 // **CloudFilesClient** provides a simple client interface to CloudFiles and Pithos
\r
45 // The class provides methods to upload/download files, delete files, manage containers
\r
49 using System.Collections.Generic;
\r
50 using System.Collections.Specialized;
\r
51 using System.ComponentModel.Composition;
\r
52 using System.Diagnostics;
\r
53 using System.Diagnostics.Contracts;
\r
56 using System.Net.Http;
\r
57 using System.Reflection;
\r
58 using System.Threading;
\r
59 using System.Threading.Tasks;
\r
60 using Newtonsoft.Json;
\r
61 using Pithos.Interfaces;
\r
63 using System.Net.Http;
\r
65 namespace Pithos.Network
\r
67 [Export(typeof(ICloudClient))]
\r
68 public class CloudFilesClient:ICloudClient
\r
70 private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
\r
72 //CloudFilesClient uses *_baseClient* internally to communicate with the server
\r
73 //RestClient provides a REST-friendly interface over the standard WebClient.
\r
74 private RestClient _baseClient;
\r
76 private HttpClient _baseHttpClient;
\r
79 //During authentication the client provides a UserName
\r
80 public string UserName { get; set; }
\r
82 //and and ApiKey to the server
\r
83 public string ApiKey { get; set; }
\r
85 //And receives an authentication Token. This token must be provided in ALL other operations,
\r
86 //in the X-Auth-Token header
\r
87 private string _token;
\r
88 private readonly string _emptyGuid = Guid.Empty.ToString();
\r
89 private readonly Uri _emptyUri = new Uri("",UriKind.Relative);
\r
94 get { return _token; }
\r
98 _baseClient.Headers["X-Auth-Token"] = value;
\r
102 //The client also receives a StorageUrl after authentication. All subsequent operations must
\r
104 public Uri StorageUrl { get; set; }
\r
107 public Uri RootAddressUri { get; set; }
\r
109 /* private WebProxy _proxy;
\r
110 public WebProxy Proxy
\r
112 get { return _proxy; }
\r
116 if (_baseClient != null)
\r
117 _baseClient.Proxy = value;
\r
122 /* private Uri _proxy;
\r
125 get { return _proxy; }
\r
129 if (_baseClient != null)
\r
130 _baseClient.Proxy = new WebProxy(value);
\r
134 public double DownloadPercentLimit { get; set; }
\r
135 public double UploadPercentLimit { get; set; }
\r
137 public string AuthenticationUrl { get; set; }
\r
140 public string VersionPath
\r
142 get { return UsePithos ? "v1" : "v1.0"; }
\r
145 public bool UsePithos { get; set; }
\r
149 public CloudFilesClient(string userName, string apiKey)
\r
151 UserName = userName;
\r
155 public CloudFilesClient(AccountInfo accountInfo)
\r
157 if (accountInfo==null)
\r
158 throw new ArgumentNullException("accountInfo");
\r
159 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
\r
160 Contract.Ensures(StorageUrl != null);
\r
161 Contract.Ensures(_baseClient != null);
\r
162 Contract.Ensures(RootAddressUri != null);
\r
163 Contract.EndContractBlock();
\r
165 _baseClient = new RestClient
\r
167 BaseAddress = accountInfo.StorageUri.ToString(),
\r
171 StorageUrl = accountInfo.StorageUri;
\r
172 Token = accountInfo.Token;
\r
173 UserName = accountInfo.UserName;
\r
175 //Get the root address (StorageUrl without the account)
\r
176 var storageUrl = StorageUrl.AbsoluteUri;
\r
177 var usernameIndex = storageUrl.LastIndexOf(UserName);
\r
178 var rootUrl = storageUrl.Substring(0, usernameIndex);
\r
179 RootAddressUri = new Uri(rootUrl);
\r
181 var httpClientHandler = new HttpClientHandler
\r
183 AllowAutoRedirect = true,
\r
184 AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
\r
189 _baseHttpClient = new HttpClient(httpClientHandler)
\r
191 BaseAddress = StorageUrl,
\r
192 Timeout = TimeSpan.FromSeconds(30)
\r
194 _baseHttpClient.DefaultRequestHeaders.Add("X-Auth-Token", Token);
\r
200 private static void AssertStatusOK(HttpResponseMessage response, string message)
\r
202 var statusCode = response.StatusCode;
\r
203 if (statusCode >= HttpStatusCode.BadRequest)
\r
204 throw new WebException(String.Format("{0} with code {1} - {2}", message, statusCode, response.ReasonPhrase));
\r
207 public AccountInfo Authenticate()
\r
209 if (String.IsNullOrWhiteSpace(UserName))
\r
210 throw new InvalidOperationException("UserName is empty");
\r
211 if (String.IsNullOrWhiteSpace(ApiKey))
\r
212 throw new InvalidOperationException("ApiKey is empty");
\r
213 if (String.IsNullOrWhiteSpace(AuthenticationUrl))
\r
214 throw new InvalidOperationException("AuthenticationUrl is empty");
\r
215 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
\r
216 Contract.Ensures(StorageUrl != null);
\r
217 Contract.Ensures(_baseClient != null);
\r
218 Contract.Ensures(RootAddressUri != null);
\r
219 Contract.EndContractBlock();
\r
222 Log.InfoFormat("[AUTHENTICATE] Start for {0}", UserName);
\r
224 var groups = new List<Group>();
\r
226 var httpClientHandler = new HttpClientHandler
\r
228 AllowAutoRedirect = true,
\r
229 AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
\r
230 UseCookies = true,
\r
233 using (var authClient = new HttpClient(httpClientHandler){ BaseAddress = new Uri(AuthenticationUrl),Timeout=TimeSpan.FromSeconds(30) })
\r
234 //using (var authClient = new RestClient{BaseAddress=AuthenticationUrl})
\r
236 /* if (Proxy != null)
\r
237 authClient.Proxy = Proxy;*/
\r
239 //Contract.Assume(authClient.DefaultRequestHeaders != null);
\r
241 authClient.DefaultRequestHeaders.Add("X-Auth-User", UserName);
\r
242 authClient.DefaultRequestHeaders.Add("X-Auth-Key", ApiKey);
\r
244 Func<Task<HttpResponseMessage>> call = () => authClient.GetAsync(new Uri(VersionPath, UriKind.Relative));
\r
247 using (var response = call.WithRetries(3).Result) // .DownloadStringWithRetryRelative(new Uri(VersionPath, UriKind.Relative), 3);
\r
249 AssertStatusOK(response,"Authentication failed");
\r
251 storageUrl = response.Headers.GetValues("X-Storage-Url").First();
\r
252 if (String.IsNullOrWhiteSpace(storageUrl))
\r
253 throw new InvalidOperationException("Failed to obtain storage url");
\r
255 token = response.Headers.GetValues("X-Auth-Token").First();
\r
256 if (String.IsNullOrWhiteSpace(token))
\r
257 throw new InvalidOperationException("Failed to obtain token url");
\r
262 _baseClient = new RestClient
\r
264 BaseAddress = storageUrl,
\r
270 StorageUrl = new Uri(storageUrl);
\r
276 //Get the root address (StorageUrl without the account)
\r
277 var usernameIndex=storageUrl.LastIndexOf(UserName);
\r
278 var rootUrl = storageUrl.Substring(0, usernameIndex);
\r
279 RootAddressUri = new Uri(rootUrl);
\r
282 _baseHttpClient = new HttpClient(httpClientHandler)
\r
284 BaseAddress = StorageUrl,
\r
285 Timeout = TimeSpan.FromSeconds(30)
\r
287 _baseHttpClient.DefaultRequestHeaders.Add("X-Auth-Token", token);
\r
289 /* var keys = authClient.ResponseHeaders.AllKeys.AsQueryable();
\r
290 groups = (from key in keys
\r
291 where key.StartsWith("X-Account-Group-")
\r
292 let name = key.Substring(16)
\r
293 select new Group(name, authClient.ResponseHeaders[key]))
\r
299 Log.InfoFormat("[AUTHENTICATE] End for {0}", UserName);
\r
300 Debug.Assert(_baseClient!=null);
\r
302 return new AccountInfo {StorageUri = StorageUrl, Token = Token, UserName = UserName,Groups=groups};
\r
306 private static void TraceStart(string method, Uri actualAddress)
\r
308 Log.InfoFormat("[{0}] {1} {2}", method, DateTime.Now, actualAddress);
\r
311 private async Task<string> GetString(Uri targetUri, string errorMessage,DateTime? since=null)
\r
313 TraceStart("GET",targetUri);
\r
314 var request = new HttpRequestMessage(HttpMethod.Get, targetUri);
\r
315 if (since.HasValue)
\r
317 request.Headers.IfModifiedSince = since.Value;
\r
319 Func<Task<HttpResponseMessage>> call = () => _baseHttpClient.SendAsync(request);
\r
320 using (var response = await call.WithRetries(3))
\r
322 AssertStatusOK(response, errorMessage);
\r
324 if (response.StatusCode == HttpStatusCode.NoContent)
\r
325 return String.Empty;
\r
327 var content = await response.Content.ReadAsStringAsync();
\r
332 public IList<ContainerInfo> ListContainers(string account)
\r
335 /* var targetUrl = String.IsNullOrWhiteSpace(account)
\r
336 ? _baseHttpClient.BaseAddress.ToString()
\r
337 : GetAccountUrl(account);
\r
338 var targetUri=new Uri(String.Format("{0}?format=json",targetUrl));
\r
339 var result = GetString(targetUri, "List Containers failed").Result;
\r
340 if (String.IsNullOrWhiteSpace(result))
\r
341 return new List<ContainerInfo>();
\r
342 var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(result);
\r
343 foreach (var info in infos)
\r
345 info.Account = account;
\r
348 using (var client = new RestClient(_baseClient))
\r
350 if (!String.IsNullOrWhiteSpace(account))
\r
351 client.BaseAddress = GetAccountUrl(account);
\r
353 client.Parameters.Clear();
\r
354 client.Parameters.Add("format", "json");
\r
355 var content = client.DownloadStringWithRetryRelative(_emptyUri, 3);
\r
356 client.AssertStatusOK("List Containers failed");
\r
358 if (client.StatusCode == HttpStatusCode.NoContent)
\r
359 return new List<ContainerInfo>();
\r
360 var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(content);
\r
362 foreach (var info in infos)
\r
364 info.Account = account;
\r
371 private string GetAccountUrl(string account)
\r
373 return RootAddressUri.Combine(account).AbsoluteUri;
\r
376 public IList<ShareAccountInfo> ListSharingAccounts(DateTime? since=null)
\r
378 using (ThreadContext.Stacks["Share"].Push("List Accounts"))
\r
380 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
382 using (var client = new RestClient(_baseClient))
\r
384 client.Parameters.Clear();
\r
385 client.Parameters.Add("format", "json");
\r
386 client.IfModifiedSince = since;
\r
388 //Extract the username from the base address
\r
389 client.BaseAddress = RootAddressUri.AbsoluteUri;
\r
391 var content = client.DownloadStringWithRetryRelative(_emptyUri, 3);
\r
393 client.AssertStatusOK("ListSharingAccounts failed");
\r
395 //If the result is empty, return an empty list,
\r
396 var infos = String.IsNullOrWhiteSpace(content)
\r
397 ? new List<ShareAccountInfo>()
\r
398 //Otherwise deserialize the account list into a list of ShareAccountInfos
\r
399 : JsonConvert.DeserializeObject<IList<ShareAccountInfo>>(content);
\r
401 Log.DebugFormat("END");
\r
409 /// Request listing of all objects in a container modified since a specific time.
\r
410 /// If the *since* value is missing, return all objects
\r
412 /// <param name="knownContainers">Use the since variable only for the containers listed in knownContainers. Unknown containers are considered new
\r
413 /// and should be polled anyway
\r
415 /// <param name="since"></param>
\r
416 /// <returns></returns>
\r
417 public IList<ObjectInfo> ListSharedObjects(HashSet<string> knownContainers,DateTime? since = null )
\r
420 using (ThreadContext.Stacks["Share"].Push("List Objects"))
\r
422 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
423 //'since' is not used here because we need to have ListObjects return a NoChange result
\r
424 //for all shared accounts,containers
\r
426 Func<ContainerInfo, string> getKey = c => String.Format("{0}\\{1}", c.Account, c.Name);
\r
428 var accounts = ListSharingAccounts();
\r
429 var containers = (from account in accounts
\r
430 let conts = ListContainers(account.name)
\r
431 from container in conts
\r
432 select container).ToList();
\r
433 var items = from container in containers
\r
434 let actualSince=knownContainers.Contains(getKey(container))?since:null
\r
435 select ListObjects(container.Account , container.Name, actualSince);
\r
436 var objects=items.SelectMany(r=> r).ToList();
\r
439 //Check parents recursively up to (but not including) the container.
\r
440 //If parents are missing, add them to the list
\r
441 //Need function to calculate all parent URLs
\r
442 objects = AddMissingParents(objects);
\r
444 //Store any new containers
\r
445 foreach (var container in containers)
\r
447 knownContainers.Add(getKey(container));
\r
452 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
457 private List<ObjectInfo> AddMissingParents(List<ObjectInfo> objects)
\r
459 //TODO: Remove short-circuit when we decide to use Missing Parents functionality
\r
462 var existingUris = objects.ToDictionary(o => o.Uri, o => o);
\r
463 foreach (var objectInfo in objects)
\r
465 //Can be null when retrieving objects to show in selective sync
\r
466 if (objectInfo.Name == null)
\r
469 //No need to unescape here, the parts will be used to create new ObjectInfos
\r
470 var parts = objectInfo.Name.ToString().Split(new[]{'/'},StringSplitOptions.RemoveEmptyEntries);
\r
471 //If there is no parent, skip
\r
472 if (parts.Length == 1)
\r
474 var baseParts = new[]
\r
476 objectInfo.Uri.Host, objectInfo.Uri.Segments[1].TrimEnd('/'),objectInfo.Account,objectInfo.Container.ToString()
\r
478 for (var partIdx = 0; partIdx < parts.Length - 1; partIdx++)
\r
480 var nameparts = parts.Range(0, partIdx).ToArray();
\r
481 var parentName= String.Join("/", nameparts);
\r
483 var parentParts = baseParts.Concat(nameparts);
\r
484 var parentUrl = objectInfo.Uri.Scheme+ "://" + String.Join("/", parentParts);
\r
486 var parentUri = new Uri(parentUrl, UriKind.Absolute);
\r
488 ObjectInfo existingInfo;
\r
489 if (!existingUris.TryGetValue(parentUri,out existingInfo))
\r
491 var h = parentUrl.GetHashCode();
\r
492 var reverse = new string(parentUrl.Reverse().ToArray());
\r
493 var rh = reverse.GetHashCode();
\r
494 var b1 = BitConverter.GetBytes(h);
\r
495 var b2 = BitConverter.GetBytes(rh);
\r
496 var g = new Guid(0,0,0,b1.Concat(b2).ToArray());
\r
499 existingUris[parentUri] = new ObjectInfo
\r
501 Account = objectInfo.Account,
\r
502 Container = objectInfo.Container,
\r
503 Content_Type = ObjectInfo.CONTENT_TYPE_DIRECTORY,
\r
504 ETag = Signature.MERKLE_EMPTY,
\r
505 X_Object_Hash = Signature.MERKLE_EMPTY,
\r
506 Name=new Uri(parentName,UriKind.Relative),
\r
507 StorageUri=objectInfo.StorageUri,
\r
509 UUID=g.ToString(),
\r
514 return existingUris.Values.ToList();
\r
517 public void SetTags(ObjectInfo target,IDictionary<string,string> tags)
\r
519 if (String.IsNullOrWhiteSpace(Token))
\r
520 throw new InvalidOperationException("The Token is not set");
\r
521 if (StorageUrl == null)
\r
522 throw new InvalidOperationException("The StorageUrl is not set");
\r
523 if (target == null)
\r
524 throw new ArgumentNullException("target");
\r
525 Contract.EndContractBlock();
\r
527 using (ThreadContext.Stacks["Share"].Push("Share Object"))
\r
529 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
531 using (var client = new RestClient(_baseClient))
\r
534 client.BaseAddress = GetAccountUrl(target.Account);
\r
536 client.Parameters.Clear();
\r
537 client.Parameters.Add("update", "");
\r
539 foreach (var tag in tags)
\r
541 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
\r
542 client.Headers.Add(headerTag, tag.Value);
\r
545 client.DownloadStringWithRetryRelative(target.Container, 3);
\r
548 client.AssertStatusOK("SetTags failed");
\r
549 //If the status is NOT ACCEPTED we have a problem
\r
550 if (client.StatusCode != HttpStatusCode.Accepted)
\r
552 Log.Error("Failed to set tags");
\r
553 throw new Exception("Failed to set tags");
\r
556 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
563 public void ShareObject(string account, Uri container, Uri objectName, string shareTo, bool read, bool write)
\r
565 if (String.IsNullOrWhiteSpace(Token))
\r
566 throw new InvalidOperationException("The Token is not set");
\r
567 if (StorageUrl==null)
\r
568 throw new InvalidOperationException("The StorageUrl is not set");
\r
569 if (container==null)
\r
570 throw new ArgumentNullException("container");
\r
571 if (container.IsAbsoluteUri)
\r
572 throw new ArgumentException("container");
\r
573 if (objectName==null)
\r
574 throw new ArgumentNullException("objectName");
\r
575 if (objectName.IsAbsoluteUri)
\r
576 throw new ArgumentException("objectName");
\r
577 if (String.IsNullOrWhiteSpace(account))
\r
578 throw new ArgumentNullException("account");
\r
579 if (String.IsNullOrWhiteSpace(shareTo))
\r
580 throw new ArgumentNullException("shareTo");
\r
581 Contract.EndContractBlock();
\r
583 using (ThreadContext.Stacks["Share"].Push("Share Object"))
\r
585 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
587 using (var client = new RestClient(_baseClient))
\r
590 client.BaseAddress = GetAccountUrl(account);
\r
592 client.Parameters.Clear();
\r
593 client.Parameters.Add("format", "json");
\r
595 string permission = "";
\r
597 permission = String.Format("write={0}", shareTo);
\r
599 permission = String.Format("read={0}", shareTo);
\r
600 client.Headers.Add("X-Object-Sharing", permission);
\r
602 var content = client.DownloadStringWithRetryRelative(container, 3);
\r
604 client.AssertStatusOK("ShareObject failed");
\r
606 //If the result is empty, return an empty list,
\r
607 var infos = String.IsNullOrWhiteSpace(content)
\r
608 ? new List<ObjectInfo>()
\r
609 //Otherwise deserialize the object list into a list of ObjectInfos
\r
610 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
612 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
619 public AccountInfo GetAccountPolicies(AccountInfo accountInfo)
\r
621 if (accountInfo==null)
\r
622 throw new ArgumentNullException("accountInfo");
\r
623 Contract.EndContractBlock();
\r
625 using (ThreadContext.Stacks["Account"].Push("GetPolicies"))
\r
627 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
629 if (_baseClient == null)
\r
631 _baseClient = new RestClient
\r
633 BaseAddress = accountInfo.StorageUri.ToString(),
\r
639 using (var client = new RestClient(_baseClient))
\r
641 if (!String.IsNullOrWhiteSpace(accountInfo.UserName))
\r
642 client.BaseAddress = GetAccountUrl(accountInfo.UserName);
\r
644 client.Parameters.Clear();
\r
645 client.Parameters.Add("format", "json");
\r
646 client.Head(_emptyUri, 3);
\r
648 var quotaValue=client.ResponseHeaders["X-Account-Policy-Quota"];
\r
649 var bytesValue= client.ResponseHeaders["X-Account-Bytes-Used"];
\r
652 if (long.TryParse(quotaValue, out quota))
\r
653 accountInfo.Quota = quota;
\r
654 if (long.TryParse(bytesValue, out bytes))
\r
655 accountInfo.BytesUsed = bytes;
\r
657 return accountInfo;
\r
664 public void UpdateMetadata(ObjectInfo objectInfo)
\r
666 if (objectInfo == null)
\r
667 throw new ArgumentNullException("objectInfo");
\r
668 Contract.EndContractBlock();
\r
670 using (ThreadContext.Stacks["Objects"].Push("UpdateMetadata"))
\r
672 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
675 using(var client=new RestClient(_baseClient))
\r
678 client.BaseAddress = GetAccountUrl(objectInfo.Account);
\r
680 client.Parameters.Clear();
\r
684 foreach (var tag in objectInfo.Tags)
\r
686 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
\r
687 client.Headers.Add(headerTag, tag.Value);
\r
692 var permissions=objectInfo.GetPermissionString();
\r
693 client.SetNonEmptyHeaderValue("X-Object-Sharing",permissions);
\r
695 client.SetNonEmptyHeaderValue("Content-Disposition",objectInfo.ContendDisposition);
\r
696 client.SetNonEmptyHeaderValue("Content-Encoding",objectInfo.ContentEncoding);
\r
697 client.SetNonEmptyHeaderValue("X-Object-Manifest",objectInfo.Manifest);
\r
698 var isPublic = objectInfo.IsPublic.ToString().ToLower();
\r
699 client.Headers.Add("X-Object-Public", isPublic);
\r
702 /*var uriBuilder = client.GetAddressBuilder(objectInfo.Container, objectInfo.Name);
\r
703 uriBuilder.Query = "update=";
\r
704 var uri = uriBuilder.Uri.MakeRelativeUri(this.RootAddressUri);*/
\r
705 var address = String.Format("{0}/{1}?update=",objectInfo.Container, objectInfo.Name);
\r
706 client.PostWithRetry(new Uri(address,UriKind.Relative),"application/xml");
\r
708 //client.UploadValues(uri,new NameValueCollection());
\r
711 client.AssertStatusOK("UpdateMetadata failed");
\r
712 //If the status is NOT ACCEPTED or OK we have a problem
\r
713 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
\r
715 Log.Error("Failed to update metadata");
\r
716 throw new Exception("Failed to update metadata");
\r
719 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
725 public void UpdateMetadata(ContainerInfo containerInfo)
\r
727 if (containerInfo == null)
\r
728 throw new ArgumentNullException("containerInfo");
\r
729 Contract.EndContractBlock();
\r
731 using (ThreadContext.Stacks["Containers"].Push("UpdateMetadata"))
\r
733 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
736 using(var client=new RestClient(_baseClient))
\r
739 client.BaseAddress = GetAccountUrl(containerInfo.Account);
\r
741 client.Parameters.Clear();
\r
745 foreach (var tag in containerInfo.Tags)
\r
747 var headerTag = String.Format("X-Container-Meta-{0}", tag.Key);
\r
748 client.Headers.Add(headerTag, tag.Value);
\r
753 foreach (var policy in containerInfo.Policies)
\r
755 var headerPolicy = String.Format("X-Container-Policy-{0}", policy.Key);
\r
756 client.Headers.Add(headerPolicy, policy.Value);
\r
760 var uriBuilder = client.GetAddressBuilder(containerInfo.Name,_emptyUri);
\r
761 var uri = uriBuilder.Uri;
\r
763 client.UploadValues(uri,new NameValueCollection());
\r
766 client.AssertStatusOK("UpdateMetadata failed");
\r
767 //If the status is NOT ACCEPTED or OK we have a problem
\r
768 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
\r
770 Log.Error("Failed to update metadata");
\r
771 throw new Exception("Failed to update metadata");
\r
774 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
783 public IList<ObjectInfo> ListObjects(string account, Uri container, DateTime? since = null)
\r
785 if (container==null)
\r
786 throw new ArgumentNullException("container");
\r
787 if (container.IsAbsoluteUri)
\r
788 throw new ArgumentException("container");
\r
789 Contract.EndContractBlock();
\r
791 using (ThreadContext.Stacks["Objects"].Push("List"))
\r
793 /*if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
795 var targetUrl = String.IsNullOrWhiteSpace(account)
\r
796 ? _baseHttpClient.BaseAddress.ToString()
\r
797 : GetAccountUrl(account);
\r
798 var targetUri = new Uri(String.Format("{0}?format=json", targetUrl));
\r
799 var result = GetString(targetUri, "ListObjects failed",since).Result;
\r
801 if (String.IsNullOrWhiteSpace(result))
\r
802 return new[]{new NoModificationInfo(account,container)};
\r
803 //If the result is empty, return an empty list,
\r
804 var infos = String.IsNullOrWhiteSpace(result)
\r
805 ? new List<ObjectInfo>()
\r
806 //Otherwise deserialize the object list into a list of ObjectInfos
\r
807 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(result);
\r
809 foreach (var info in infos)
\r
811 info.Container = container;
\r
812 info.Account = account;
\r
813 info.StorageUri = StorageUrl;
\r
815 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
818 using (var client = new RestClient(_baseClient))
\r
820 if (!String.IsNullOrWhiteSpace(account))
\r
821 client.BaseAddress = GetAccountUrl(account);
\r
823 client.Parameters.Clear();
\r
824 client.Parameters.Add("format", "json");
\r
825 client.IfModifiedSince = since;
\r
826 var content = client.DownloadStringWithRetryRelative(container, 3);
\r
828 client.AssertStatusOK("ListObjects failed");
\r
830 if (client.StatusCode==HttpStatusCode.NotModified)
\r
831 return new[]{new NoModificationInfo(account,container)};
\r
832 //If the result is empty, return an empty list,
\r
833 var infos = String.IsNullOrWhiteSpace(content)
\r
834 ? new List<ObjectInfo>()
\r
835 //Otherwise deserialize the object list into a list of ObjectInfos
\r
836 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
838 foreach (var info in infos)
\r
840 info.Container = container;
\r
841 info.Account = account;
\r
842 info.StorageUri = StorageUrl;
\r
844 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
850 public IList<ObjectInfo> ListObjects(string account, Uri container, Uri folder, DateTime? since = null)
\r
852 if (container==null)
\r
853 throw new ArgumentNullException("container");
\r
854 if (container.IsAbsoluteUri)
\r
855 throw new ArgumentException("container");
\r
857 if (String.IsNullOrWhiteSpace(folder))
\r
858 throw new ArgumentNullException("folder");
\r
860 Contract.EndContractBlock();
\r
862 using (ThreadContext.Stacks["Objects"].Push("List"))
\r
864 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
866 using (var client = new RestClient(_baseClient))
\r
868 if (!String.IsNullOrWhiteSpace(account))
\r
869 client.BaseAddress = GetAccountUrl(account);
\r
871 client.Parameters.Clear();
\r
872 client.Parameters.Add("format", "json");
\r
873 client.Parameters.Add("path", folder.ToString());
\r
874 client.IfModifiedSince = since;
\r
875 var content = client.DownloadStringWithRetryRelative(container, 3);
\r
876 client.AssertStatusOK("ListObjects failed");
\r
878 if (client.StatusCode==HttpStatusCode.NotModified)
\r
879 return new[]{new NoModificationInfo(account,container,folder)};
\r
881 var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
882 foreach (var info in infos)
\r
884 info.Account = account;
\r
885 if (info.Container == null)
\r
886 info.Container = container;
\r
887 info.StorageUri = StorageUrl;
\r
889 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
896 public bool ContainerExists(string account, Uri container)
\r
898 if (container==null)
\r
899 throw new ArgumentNullException("container", "The container property can't be empty");
\r
900 if (container.IsAbsoluteUri)
\r
901 throw new ArgumentException( "The container must be relative","container");
\r
902 Contract.EndContractBlock();
\r
904 using (ThreadContext.Stacks["Containters"].Push("Exists"))
\r
906 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
908 using (var client = new RestClient(_baseClient))
\r
910 if (!String.IsNullOrWhiteSpace(account))
\r
911 client.BaseAddress = GetAccountUrl(account);
\r
913 client.Parameters.Clear();
\r
914 client.Head(container, 3);
\r
917 switch (client.StatusCode)
\r
919 case HttpStatusCode.OK:
\r
920 case HttpStatusCode.NoContent:
\r
923 case HttpStatusCode.NotFound:
\r
927 throw CreateWebException("ContainerExists", client.StatusCode);
\r
929 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
937 public bool ObjectExists(string account, Uri container, Uri objectName)
\r
939 if (container == null)
\r
940 throw new ArgumentNullException("container", "The container property can't be empty");
\r
941 if (container.IsAbsoluteUri)
\r
942 throw new ArgumentException("The container must be relative","container");
\r
943 if (objectName == null)
\r
944 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
945 if (objectName.IsAbsoluteUri)
\r
946 throw new ArgumentException("The objectName must be relative","objectName");
\r
947 Contract.EndContractBlock();
\r
949 using (var client = new RestClient(_baseClient))
\r
951 if (!String.IsNullOrWhiteSpace(account))
\r
952 client.BaseAddress = GetAccountUrl(account);
\r
954 client.Parameters.Clear();
\r
955 client.Head(container.Combine(objectName), 3);
\r
957 switch (client.StatusCode)
\r
959 case HttpStatusCode.OK:
\r
960 case HttpStatusCode.NoContent:
\r
962 case HttpStatusCode.NotFound:
\r
965 throw CreateWebException("ObjectExists", client.StatusCode);
\r
971 public ObjectInfo GetObjectInfo(string account, Uri container, Uri objectName)
\r
973 if (container == null)
\r
974 throw new ArgumentNullException("container", "The container property can't be empty");
\r
975 if (container.IsAbsoluteUri)
\r
976 throw new ArgumentException("The container must be relative","container");
\r
977 if (objectName == null)
\r
978 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
979 if (objectName.IsAbsoluteUri)
\r
980 throw new ArgumentException("The objectName must be relative","objectName");
\r
981 Contract.EndContractBlock();
\r
983 using (ThreadContext.Stacks["Objects"].Push("GetObjectInfo"))
\r
986 using (var client = new RestClient(_baseClient))
\r
988 if (!String.IsNullOrWhiteSpace(account))
\r
989 client.BaseAddress = GetAccountUrl(account);
\r
992 client.Parameters.Clear();
\r
994 client.Head(container.Combine(objectName), 3);
\r
996 if (client.TimedOut)
\r
997 return ObjectInfo.Empty;
\r
999 switch (client.StatusCode)
\r
1001 case HttpStatusCode.OK:
\r
1002 case HttpStatusCode.NoContent:
\r
1003 var keys = client.ResponseHeaders.AllKeys.AsQueryable();
\r
1004 var tags = client.GetMeta("X-Object-Meta-");
\r
1005 var extensions = (from key in keys
\r
1006 where key.StartsWith("X-Object-") && !key.StartsWith("X-Object-Meta-")
\r
1007 select new {Name = key, Value = client.ResponseHeaders[key]})
\r
1008 .ToDictionary(t => t.Name, t => t.Value);
\r
1010 var permissions=client.GetHeaderValue("X-Object-Sharing", true);
\r
1013 var info = new ObjectInfo
\r
1015 Account = account,
\r
1016 Container = container,
\r
1017 Name = objectName,
\r
1018 ETag = client.GetHeaderValue("ETag"),
\r
1019 UUID=client.GetHeaderValue("X-Object-UUID"),
\r
1020 X_Object_Hash = client.GetHeaderValue("X-Object-Hash"),
\r
1021 Content_Type = client.GetHeaderValue("Content-Type"),
\r
1022 Bytes = Convert.ToInt64(client.GetHeaderValue("Content-Length",true)),
\r
1024 Last_Modified = client.LastModified,
\r
1025 Extensions = extensions,
\r
1026 ContentEncoding=client.GetHeaderValue("Content-Encoding",true),
\r
1027 ContendDisposition = client.GetHeaderValue("Content-Disposition",true),
\r
1028 Manifest=client.GetHeaderValue("X-Object-Manifest",true),
\r
1029 PublicUrl=client.GetHeaderValue("X-Object-Public",true),
\r
1030 StorageUri=StorageUrl,
\r
1032 info.SetPermissions(permissions);
\r
1034 case HttpStatusCode.NotFound:
\r
1035 return ObjectInfo.Empty;
\r
1037 throw new WebException(
\r
1038 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
\r
1039 objectName, client.StatusCode));
\r
1043 catch (RetryException)
\r
1045 Log.WarnFormat("[RETRY FAIL] GetObjectInfo for {0} failed.",objectName);
\r
1046 return ObjectInfo.Empty;
\r
1048 catch (WebException e)
\r
1051 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
\r
1052 objectName, client.StatusCode), e);
\r
1060 public void CreateFolder(string account, Uri container, Uri folder)
\r
1062 if (container == null)
\r
1063 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1064 if (container.IsAbsoluteUri)
\r
1065 throw new ArgumentException("The container must be relative","container");
\r
1066 if (folder == null)
\r
1067 throw new ArgumentNullException("folder", "The objectName property can't be empty");
\r
1068 if (folder.IsAbsoluteUri)
\r
1069 throw new ArgumentException("The objectName must be relative","folder");
\r
1070 Contract.EndContractBlock();
\r
1072 var folderUri=container.Combine(folder);
\r
1073 using (var client = new RestClient(_baseClient))
\r
1075 if (!String.IsNullOrWhiteSpace(account))
\r
1076 client.BaseAddress = GetAccountUrl(account);
\r
1078 client.Parameters.Clear();
\r
1079 client.Headers.Add("Content-Type", ObjectInfo.CONTENT_TYPE_DIRECTORY);
\r
1080 client.Headers.Add("Content-Length", "0");
\r
1081 client.PutWithRetry(folderUri, 3);
\r
1083 if (client.StatusCode != HttpStatusCode.Created && client.StatusCode != HttpStatusCode.Accepted)
\r
1084 throw CreateWebException("CreateFolder", client.StatusCode);
\r
1090 public ContainerInfo GetContainerInfo(string account, Uri container)
\r
1092 if (container == null)
\r
1093 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1094 if (container.IsAbsoluteUri)
\r
1095 throw new ArgumentException("The container must be relative","container");
\r
1096 Contract.EndContractBlock();
\r
1098 using (var client = new RestClient(_baseClient))
\r
1100 if (!String.IsNullOrWhiteSpace(account))
\r
1101 client.BaseAddress = GetAccountUrl(account);
\r
1103 client.Head(container);
\r
1104 switch (client.StatusCode)
\r
1106 case HttpStatusCode.OK:
\r
1107 case HttpStatusCode.NoContent:
\r
1108 var tags = client.GetMeta("X-Container-Meta-");
\r
1109 var policies = client.GetMeta("X-Container-Policy-");
\r
1111 var containerInfo = new ContainerInfo
\r
1115 StorageUrl=StorageUrl.ToString(),
\r
1117 long.Parse(client.GetHeaderValue("X-Container-Object-Count")),
\r
1118 Bytes = long.Parse(client.GetHeaderValue("X-Container-Bytes-Used")),
\r
1119 BlockHash = client.GetHeaderValue("X-Container-Block-Hash"),
\r
1120 BlockSize=int.Parse(client.GetHeaderValue("X-Container-Block-Size")),
\r
1121 Last_Modified=client.LastModified,
\r
1127 return containerInfo;
\r
1128 case HttpStatusCode.NotFound:
\r
1129 return ContainerInfo.Empty;
\r
1131 throw CreateWebException("GetContainerInfo", client.StatusCode);
\r
1136 public void CreateContainer(string account, Uri container)
\r
1138 if (container == null)
\r
1139 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1140 if (container.IsAbsoluteUri)
\r
1141 throw new ArgumentException("The container must be relative","container");
\r
1142 Contract.EndContractBlock();
\r
1144 using (var client = new RestClient(_baseClient))
\r
1146 if (!String.IsNullOrWhiteSpace(account))
\r
1147 client.BaseAddress = GetAccountUrl(account);
\r
1149 client.PutWithRetry(container, 3);
\r
1150 var expectedCodes = new[] {HttpStatusCode.Created, HttpStatusCode.Accepted, HttpStatusCode.OK};
\r
1151 if (!expectedCodes.Contains(client.StatusCode))
\r
1152 throw CreateWebException("CreateContainer", client.StatusCode);
\r
1156 public void DeleteContainer(string account, Uri container)
\r
1158 if (container == null)
\r
1159 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1160 if (container.IsAbsoluteUri)
\r
1161 throw new ArgumentException("The container must be relative","container");
\r
1162 Contract.EndContractBlock();
\r
1164 using (var client = new RestClient(_baseClient))
\r
1166 if (!String.IsNullOrWhiteSpace(account))
\r
1167 client.BaseAddress = GetAccountUrl(account);
\r
1169 client.DeleteWithRetry(container, 3);
\r
1170 var expectedCodes = new[] {HttpStatusCode.NotFound, HttpStatusCode.NoContent};
\r
1171 if (!expectedCodes.Contains(client.StatusCode))
\r
1172 throw CreateWebException("DeleteContainer", client.StatusCode);
\r
1180 /// <param name="account"></param>
\r
1181 /// <param name="container"></param>
\r
1182 /// <param name="objectName"></param>
\r
1183 /// <param name="fileName"></param>
\r
1184 /// <param name="cancellationToken"> </param>
\r
1185 /// <returns></returns>
\r
1186 /// <remarks>This method should have no timeout or a very long one</remarks>
\r
1187 //Asynchronously download the object specified by *objectName* in a specific *container* to
\r
1189 public async Task GetObject(string account, Uri container, Uri objectName, string fileName,CancellationToken cancellationToken)
\r
1191 if (container == null)
\r
1192 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1193 if (container.IsAbsoluteUri)
\r
1194 throw new ArgumentException("The container must be relative","container");
\r
1195 if (objectName == null)
\r
1196 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1197 if (objectName.IsAbsoluteUri)
\r
1198 throw new ArgumentException("The objectName must be relative","objectName");
\r
1199 Contract.EndContractBlock();
\r
1203 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
\r
1204 //object to avoid concurrency errors.
\r
1206 //Download operations take a long time therefore they have no timeout.
\r
1207 using(var client = new RestClient(_baseClient) { Timeout = 0 })
\r
1209 if (!String.IsNullOrWhiteSpace(account))
\r
1210 client.BaseAddress = GetAccountUrl(account);
\r
1212 //The container and objectName are relative names. They are joined with the client's
\r
1213 //BaseAddress to create the object's absolute address
\r
1214 var builder = client.GetAddressBuilder(container, objectName);
\r
1215 var uri = builder.Uri;
\r
1217 //Download progress is reported to the Trace log
\r
1218 Log.InfoFormat("[GET] START {0}", objectName);
\r
1219 /*client.DownloadProgressChanged += (sender, args) =>
\r
1220 Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1221 fileName, args.ProgressPercentage,
\r
1222 args.BytesReceived,
\r
1223 args.TotalBytesToReceive);*/
\r
1224 var progress = new Progress<DownloadProgressChangedEventArgs>(args =>
\r
1226 Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1227 fileName, args.ProgressPercentage,
\r
1228 args.BytesReceived,
\r
1229 args.TotalBytesToReceive);
\r
1230 if (DownloadProgressChanged!=null)
\r
1231 DownloadProgressChanged(this, args);
\r
1234 //Start downloading the object asynchronously
\r
1235 await client.DownloadFileTaskAsync(uri, fileName, cancellationToken,progress).ConfigureAwait(false);
\r
1237 //Once the download completes
\r
1238 //Delete the local client object
\r
1240 //And report failure or completion
\r
1242 catch (Exception exc)
\r
1244 Log.ErrorFormat("[GET] FAIL {0} with {1}", objectName, exc);
\r
1248 Log.InfoFormat("[GET] END {0}", objectName);
\r
1253 public Task<IList<string>> PutHashMap(string account, Uri container, Uri objectName, TreeHash hash)
\r
1255 if (container == null)
\r
1256 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1257 if (container.IsAbsoluteUri)
\r
1258 throw new ArgumentException("The container must be relative","container");
\r
1259 if (objectName == null)
\r
1260 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1261 if (objectName.IsAbsoluteUri)
\r
1262 throw new ArgumentException("The objectName must be relative","objectName");
\r
1264 throw new ArgumentNullException("hash");
\r
1265 if (String.IsNullOrWhiteSpace(Token))
\r
1266 throw new InvalidOperationException("Invalid Token");
\r
1267 if (StorageUrl == null)
\r
1268 throw new InvalidOperationException("Invalid Storage Url");
\r
1269 Contract.EndContractBlock();
\r
1272 //Don't use a timeout because putting the hashmap may be a long process
\r
1273 var client = new RestClient(_baseClient) { Timeout = 0 };
\r
1274 if (!String.IsNullOrWhiteSpace(account))
\r
1275 client.BaseAddress = GetAccountUrl(account);
\r
1277 //The container and objectName are relative names. They are joined with the client's
\r
1278 //BaseAddress to create the object's absolute address
\r
1279 var builder = client.GetAddressBuilder(container, objectName);
\r
1280 builder.Query = "format=json&hashmap";
\r
1281 var uri = builder.Uri;
\r
1284 //Send the tree hash as Json to the server
\r
1285 client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
\r
1286 var jsonHash = hash.ToJson();
\r
1288 client.Headers.Add("ETag",hash.TopHash.ToHashString());
\r
1289 var uploadTask=client.UploadStringTask(uri, "PUT", jsonHash);
\r
1290 if (Log.IsDebugEnabled)
\r
1291 Log.DebugFormat("Hashes:\r\n{0}", jsonHash);
\r
1292 return uploadTask.ContinueWith(t =>
\r
1295 var empty = (IList<string>)new List<string>();
\r
1298 //The server will respond either with 201-created if all blocks were already on the server
\r
1299 if (client.StatusCode == HttpStatusCode.Created)
\r
1301 //in which case we return an empty hash list
\r
1304 //or with a 409-conflict and return the list of missing parts
\r
1305 //A 409 will cause an exception so we need to check t.IsFaulted to avoid propagating the exception
\r
1308 var ex = t.Exception.InnerException;
\r
1309 var we = ex as WebException;
\r
1310 var response = we.Response as HttpWebResponse;
\r
1311 if (response!=null && response.StatusCode==HttpStatusCode.Conflict)
\r
1313 //In case of 409 the missing parts will be in the response content
\r
1314 using (var stream = response.GetResponseStream())
\r
1315 using(var reader=stream.GetLoggedReader(Log))
\r
1317 //We used to have to cleanup the content before returning it because it contains
\r
1318 //error content after the list of hashes
\r
1320 //As of 30/1/2012, the result is a proper Json array so we don't need to read the content
\r
1323 var serializer = new JsonSerializer();
\r
1324 serializer.Error += (sender, args) => Log.ErrorFormat("Deserialization error at [{0}] [{1}]", args.ErrorContext.Error, args.ErrorContext.Member);
\r
1325 var hashes = (List<string>)serializer.Deserialize(reader, typeof(List<string>));
\r
1329 //Any other status code is unexpected and the exception should be rethrown
\r
1330 Log.LogError(response);
\r
1335 //Any other status code is unexpected but there was no exception. We can probably continue processing
\r
1336 Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",client.StatusCode,client.StatusDescription);
\r
1344 public async Task<byte[]> GetBlock(string account, Uri container, Uri relativeUrl, long start, long? end, CancellationToken cancellationToken)
\r
1346 if (String.IsNullOrWhiteSpace(Token))
\r
1347 throw new InvalidOperationException("Invalid Token");
\r
1348 if (StorageUrl == null)
\r
1349 throw new InvalidOperationException("Invalid Storage Url");
\r
1350 if (container == null)
\r
1351 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1352 if (container.IsAbsoluteUri)
\r
1353 throw new ArgumentException("The container must be relative","container");
\r
1354 if (relativeUrl == null)
\r
1355 throw new ArgumentNullException("relativeUrl");
\r
1356 if (end.HasValue && end < 0)
\r
1357 throw new ArgumentOutOfRangeException("end");
\r
1359 throw new ArgumentOutOfRangeException("start");
\r
1360 Contract.EndContractBlock();
\r
1362 //Don't use a timeout because putting the hashmap may be a long process
\r
1363 using (var client = new RestClient(_baseClient) {Timeout = 0, RangeFrom = start, RangeTo = end})
\r
1365 if (!String.IsNullOrWhiteSpace(account))
\r
1366 client.BaseAddress = GetAccountUrl(account);
\r
1368 var builder = client.GetAddressBuilder(container, relativeUrl);
\r
1369 var uri = builder.Uri;
\r
1371 /* client.DownloadProgressChanged += (sender, args) =>
\r
1373 Log.DebugFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1374 uri.Segments.Last(), args.ProgressPercentage,
\r
1375 args.BytesReceived,
\r
1376 args.TotalBytesToReceive);
\r
1377 DownloadProgressChanged(sender, args);
\r
1379 var progress = new Progress<DownloadProgressChangedEventArgs>(args =>
\r
1381 Log.DebugFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1382 uri.Segments.Last(), args.ProgressPercentage,
\r
1383 args.BytesReceived,
\r
1384 args.TotalBytesToReceive);
\r
1385 if (DownloadProgressChanged!=null)
\r
1386 DownloadProgressChanged(this, args);
\r
1390 var result = await client.DownloadDataTaskAsync(uri, cancellationToken,progress).ConfigureAwait(false);
\r
1395 public event UploadProgressChangedEventHandler UploadProgressChanged;
\r
1396 public event DownloadProgressChangedEventHandler DownloadProgressChanged;
\r
1398 public async Task PostBlock(string account, Uri container, byte[] block, int offset, int count,CancellationToken token)
\r
1400 if (container == null)
\r
1401 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1402 if (container.IsAbsoluteUri)
\r
1403 throw new ArgumentException("The container must be relative","container");
\r
1404 if (block == null)
\r
1405 throw new ArgumentNullException("block");
\r
1406 if (offset < 0 || offset >= block.Length)
\r
1407 throw new ArgumentOutOfRangeException("offset");
\r
1408 if (count < 0 || count > block.Length)
\r
1409 throw new ArgumentOutOfRangeException("count");
\r
1410 if (String.IsNullOrWhiteSpace(Token))
\r
1411 throw new InvalidOperationException("Invalid Token");
\r
1412 if (StorageUrl == null)
\r
1413 throw new InvalidOperationException("Invalid Storage Url");
\r
1414 Contract.EndContractBlock();
\r
1420 //Don't use a timeout because putting the hashmap may be a long process
\r
1421 using (var client = new RestClient(_baseClient) { Timeout = 0 })
\r
1423 if (!String.IsNullOrWhiteSpace(account))
\r
1424 client.BaseAddress = GetAccountUrl(account);
\r
1426 var builder = client.GetAddressBuilder(container, _emptyUri);
\r
1427 //We are doing an update
\r
1428 builder.Query = "update";
\r
1429 var uri = builder.Uri;
\r
1431 client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
\r
1433 Log.InfoFormat("[BLOCK POST] START");
\r
1436 client.UploadProgressChanged += (sender, args) =>
\r
1438 Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",
\r
1439 args.ProgressPercentage, args.BytesSent,
\r
1440 args.TotalBytesToSend);
\r
1441 UploadProgressChanged(sender, args);
\r
1444 client.UploadFileCompleted += (sender, args) =>
\r
1445 Log.InfoFormat("[BLOCK POST PROGRESS] Completed ");
\r
1447 var progress=new Progress<UploadProgressChangedEventArgs>(args=>
\r
1449 Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",
\r
1450 args.ProgressPercentage, args.BytesSent,
\r
1451 args.TotalBytesToSend);
\r
1452 if (UploadProgressChanged!=null)
\r
1453 UploadProgressChanged(this, args);
\r
1455 var buffer = new byte[count];
\r
1456 Buffer.BlockCopy(block, offset, buffer, 0, count);
\r
1458 await client.UploadDataTaskAsync(uri, "POST", buffer,token,progress).ConfigureAwait(false);
\r
1459 Log.InfoFormat("[BLOCK POST] END");
\r
1462 catch (TaskCanceledException )
\r
1464 Log.Info("Aborting block");
\r
1467 catch (Exception exc)
\r
1469 Log.ErrorFormat("[BLOCK POST] FAIL with \r{0}", exc);
\r
1475 public async Task<TreeHash> GetHashMap(string account, Uri container, Uri objectName)
\r
1477 if (container == null)
\r
1478 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1479 if (container.IsAbsoluteUri)
\r
1480 throw new ArgumentException("The container must be relative","container");
\r
1481 if (objectName == null)
\r
1482 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1483 if (objectName.IsAbsoluteUri)
\r
1484 throw new ArgumentException("The objectName must be relative","objectName");
\r
1485 if (String.IsNullOrWhiteSpace(Token))
\r
1486 throw new InvalidOperationException("Invalid Token");
\r
1487 if (StorageUrl == null)
\r
1488 throw new InvalidOperationException("Invalid Storage Url");
\r
1489 Contract.EndContractBlock();
\r
1493 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
\r
1494 //object to avoid concurrency errors.
\r
1496 //Download operations take a long time therefore they have no timeout.
\r
1497 //TODO: Do they really? this is a hashmap operation, not a download
\r
1499 //Start downloading the object asynchronously
\r
1500 using (var client = new RestClient(_baseClient) { Timeout = 0 })
\r
1502 if (!String.IsNullOrWhiteSpace(account))
\r
1503 client.BaseAddress = GetAccountUrl(account);
\r
1505 //The container and objectName are relative names. They are joined with the client's
\r
1506 //BaseAddress to create the object's absolute address
\r
1508 var builder = client.GetAddressBuilder(container, objectName);
\r
1509 builder.Query = "format=json&hashmap";
\r
1510 var uri = builder.Uri;
\r
1513 var json = await client.DownloadStringTaskAsync(uri).ConfigureAwait(false);
\r
1514 var treeHash = TreeHash.Parse(json);
\r
1515 Log.InfoFormat("[GET HASH] END {0}", objectName);
\r
1519 catch (Exception exc)
\r
1521 Log.ErrorFormat("[GET HASH] END {0} with {1}", objectName, exc);
\r
1531 /// <param name="account"></param>
\r
1532 /// <param name="container"></param>
\r
1533 /// <param name="objectName"></param>
\r
1534 /// <param name="fileName"></param>
\r
1535 /// <param name="hash">Optional hash value for the file. If no hash is provided, the method calculates a new hash</param>
\r
1536 /// <param name="contentType"> </param>
\r
1537 /// <remarks>>This method should have no timeout or a very long one</remarks>
\r
1538 public async Task PutObject(string account, Uri container, Uri objectName, string fileName, string hash = Signature.MERKLE_EMPTY, string contentType = "application/octet-stream")
\r
1540 if (container == null)
\r
1541 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1542 if (container.IsAbsoluteUri)
\r
1543 throw new ArgumentException("The container must be relative","container");
\r
1544 if (objectName == null)
\r
1545 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1546 if (objectName.IsAbsoluteUri)
\r
1547 throw new ArgumentException("The objectName must be relative","objectName");
\r
1548 if (String.IsNullOrWhiteSpace(fileName))
\r
1549 throw new ArgumentNullException("fileName", "The fileName property can't be empty");
\r
1551 if (!File.Exists(fileName) && !Directory.Exists(fileName))
\r
1552 throw new FileNotFoundException("The file or directory does not exist",fileName);
\r
1558 using (var client = new RestClient(_baseClient) { Timeout = 0 })
\r
1560 if (!String.IsNullOrWhiteSpace(account))
\r
1561 client.BaseAddress = GetAccountUrl(account);
\r
1563 var builder = client.GetAddressBuilder(container, objectName);
\r
1564 var uri = builder.Uri;
\r
1566 string etag = hash ;
\r
1568 client.Headers.Add("Content-Type", contentType);
\r
1569 if (contentType!=ObjectInfo.CONTENT_TYPE_DIRECTORY)
\r
1570 client.Headers.Add("ETag", etag);
\r
1573 Log.InfoFormat("[PUT] START {0}", objectName);
\r
1574 client.UploadProgressChanged += (sender, args) =>
\r
1576 using (ThreadContext.Stacks["PUT"].Push("Progress"))
\r
1578 Log.InfoFormat("{0} {1}% {2} of {3}", fileName,
\r
1579 args.ProgressPercentage,
\r
1580 args.BytesSent, args.TotalBytesToSend);
\r
1584 client.UploadFileCompleted += (sender, args) =>
\r
1586 using (ThreadContext.Stacks["PUT"].Push("Progress"))
\r
1588 Log.InfoFormat("Completed {0}", fileName);
\r
1591 if (contentType==ObjectInfo.CONTENT_TYPE_DIRECTORY)
\r
1592 await client.UploadDataTaskAsync(uri, "PUT", new byte[0]).ConfigureAwait(false);
\r
1594 await client.UploadFileTaskAsync(uri, "PUT", fileName).ConfigureAwait(false);
\r
1597 Log.InfoFormat("[PUT] END {0}", objectName);
\r
1599 catch (Exception exc)
\r
1601 Log.ErrorFormat("[PUT] END {0} with {1}", objectName, exc);
\r
1609 private static string CalculateHash(string fileName)
\r
1611 Contract.Requires(!String.IsNullOrWhiteSpace(fileName));
\r
1612 Contract.EndContractBlock();
\r
1615 using (var hasher = MD5.Create())
\r
1616 using(var stream=File.OpenRead(fileName))
\r
1618 var hashBuilder=new StringBuilder();
\r
1619 foreach (byte b in hasher.ComputeHash(stream))
\r
1620 hashBuilder.Append(b.ToString("x2").ToLower());
\r
1621 hash = hashBuilder.ToString();
\r
1628 public void MoveObject(string account, Uri sourceContainer, Uri oldObjectName, Uri targetContainer, Uri newObjectName)
\r
1630 if (sourceContainer == null)
\r
1631 throw new ArgumentNullException("sourceContainer", "The sourceContainer property can't be empty");
\r
1632 if (sourceContainer.IsAbsoluteUri)
\r
1633 throw new ArgumentException("The sourceContainer must be relative","sourceContainer");
\r
1634 if (oldObjectName == null)
\r
1635 throw new ArgumentNullException("oldObjectName", "The oldObjectName property can't be empty");
\r
1636 if (oldObjectName.IsAbsoluteUri)
\r
1637 throw new ArgumentException("The oldObjectName must be relative","oldObjectName");
\r
1638 if (targetContainer == null)
\r
1639 throw new ArgumentNullException("targetContainer", "The targetContainer property can't be empty");
\r
1640 if (targetContainer.IsAbsoluteUri)
\r
1641 throw new ArgumentException("The targetContainer must be relative","targetContainer");
\r
1642 if (newObjectName == null)
\r
1643 throw new ArgumentNullException("newObjectName", "The newObjectName property can't be empty");
\r
1644 if (newObjectName.IsAbsoluteUri)
\r
1645 throw new ArgumentException("The newObjectName must be relative","newObjectName");
\r
1646 Contract.EndContractBlock();
\r
1648 var targetUri =new Uri(targetContainer + "/" + newObjectName,UriKind.Relative);
\r
1649 var sourceUri =new Uri(String.Format("/{0}/{1}", sourceContainer, oldObjectName),UriKind.Relative);
\r
1651 using (var client = new RestClient(_baseClient))
\r
1653 if (!String.IsNullOrWhiteSpace(account))
\r
1654 client.BaseAddress = GetAccountUrl(account);
\r
1656 client.Headers.Add("X-Move-From", sourceUri.ToString());
\r
1657 client.PutWithRetry(targetUri, 3);
\r
1659 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
\r
1660 if (!expectedCodes.Contains(client.StatusCode))
\r
1661 throw CreateWebException("MoveObject", client.StatusCode);
\r
1665 public void DeleteObject(string account, Uri sourceContainer, Uri objectName, bool isDirectory)
\r
1667 if (sourceContainer == null)
\r
1668 throw new ArgumentNullException("sourceContainer", "The sourceContainer property can't be empty");
\r
1669 if (sourceContainer.IsAbsoluteUri)
\r
1670 throw new ArgumentException("The sourceContainer must be relative","sourceContainer");
\r
1671 if (objectName == null)
\r
1672 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1673 if (objectName.IsAbsoluteUri)
\r
1674 throw new ArgumentException("The objectName must be relative","objectName");
\r
1675 Contract.EndContractBlock();
\r
1677 var targetUrl = FolderConstants.TrashContainer + "/" + objectName;
\r
1680 targetUrl = targetUrl + "?delimiter=/";
\r
1683 var sourceUrl = String.Format("/{0}/{1}", sourceContainer, objectName);
\r
1685 using (var client = new RestClient(_baseClient))
\r
1687 if (!String.IsNullOrWhiteSpace(account))
\r
1688 client.BaseAddress = GetAccountUrl(account);
\r
1690 client.Headers.Add("X-Move-From", sourceUrl);
\r
1691 client.AllowedStatusCodes.Add(HttpStatusCode.NotFound);
\r
1692 Log.InfoFormat("[TRASH] [{0}] to [{1}]",sourceUrl,targetUrl);
\r
1693 client.PutWithRetry(new Uri(targetUrl,UriKind.Relative), 3);
\r
1695 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created,HttpStatusCode.NotFound};
\r
1696 if (!expectedCodes.Contains(client.StatusCode))
\r
1697 throw CreateWebException("DeleteObject", client.StatusCode);
\r
1702 private static WebException CreateWebException(string operation, HttpStatusCode statusCode)
\r
1704 return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode));
\r
1709 public IEnumerable<ObjectInfo> ListDirectories(ContainerInfo container)
\r
1711 var directories=this.ListObjects(container.Account, container.Name, "/");
\r
1715 public bool CanUpload(string account, ObjectInfo cloudFile)
\r
1717 Contract.Requires(!String.IsNullOrWhiteSpace(account));
\r
1718 Contract.Requires(cloudFile!=null);
\r
1720 using (var client = new RestClient(_baseClient))
\r
1722 if (!String.IsNullOrWhiteSpace(account))
\r
1723 client.BaseAddress = GetAccountUrl(account);
\r
1726 var parts = cloudFile.Name.ToString().Split('/');
\r
1727 var folder = String.Join("/", parts,0,parts.Length-1);
\r
1729 var fileName = String.Format("{0}/{1}.pithos.ignore", folder, Guid.NewGuid());
\r
1730 var fileUri=fileName.ToEscapedUri();
\r
1732 client.Parameters.Clear();
\r
1735 var relativeUri = cloudFile.Container.Combine(fileUri);
\r
1736 client.PutWithRetry(relativeUri, 3, @"application/octet-stream");
\r
1738 var expectedCodes = new[] { HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
\r
1739 var result=(expectedCodes.Contains(client.StatusCode));
\r
1740 DeleteObject(account, cloudFile.Container, fileUri, cloudFile.IsDirectory);
\r
1751 public class ShareAccountInfo
\r
1753 public DateTime? last_modified { get; set; }
\r
1754 public string name { get; set; }
\r