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
57 using System.Net.Http;
\r
58 using System.Net.Http.Headers;
\r
59 using System.Reflection;
\r
60 using System.Threading;
\r
61 using System.Threading.Tasks;
\r
62 using Newtonsoft.Json;
\r
63 using Pithos.Interfaces;
\r
64 using Pithos.Network;
\r
67 namespace Pithos.Network
\r
70 [Export(typeof(ICloudClient))]
\r
71 public class CloudFilesClient:ICloudClient,IDisposable
\r
73 private const string TOKEN_HEADER = "X-Auth-Token";
\r
74 private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
\r
76 //CloudFilesClient uses *_baseClient* internally to communicate with the server
\r
77 //RestClient provides a REST-friendly interface over the standard WebClient.
\r
78 private RestClient _baseClient;
\r
80 private HttpClient _baseHttpClient;
\r
81 private HttpClient _baseHttpClientNoTimeout;
\r
84 //During authentication the client provides a UserName
\r
85 public string UserName { get; set; }
\r
87 //and and ApiKey to the server
\r
88 public string ApiKey { get; set; }
\r
90 //And receives an authentication Token. This token must be provided in ALL other operations,
\r
91 //in the X-Auth-Token header
\r
92 private string _token;
\r
93 private readonly string _emptyGuid = Guid.Empty.ToString();
\r
94 private readonly Uri _emptyUri = new Uri("",UriKind.Relative);
\r
96 private HttpClientHandler _httpClientHandler = new HttpClientHandler
\r
98 AllowAutoRedirect = true,
\r
99 AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
\r
100 UseCookies = true,
\r
104 public string Token
\r
106 get { return _token; }
\r
110 _baseClient.Headers[TOKEN_HEADER] = value;
\r
114 //The client also receives a StorageUrl after authentication. All subsequent operations must
\r
116 public Uri StorageUrl { get; set; }
\r
119 public Uri RootAddressUri { get; set; }
\r
122 public double DownloadPercentLimit { get; set; }
\r
123 public double UploadPercentLimit { get; set; }
\r
125 public string AuthenticationUrl { get; set; }
\r
128 public string VersionPath
\r
130 get { return UsePithos ? "v1" : "v1.0"; }
\r
133 public bool UsePithos { get; set; }
\r
137 public CloudFilesClient(string userName, string apiKey)
\r
139 UserName = userName;
\r
143 public CloudFilesClient(AccountInfo accountInfo)
\r
145 if (accountInfo==null)
\r
146 throw new ArgumentNullException("accountInfo");
\r
147 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
\r
148 Contract.Ensures(StorageUrl != null);
\r
149 Contract.Ensures(_baseClient != null);
\r
150 Contract.Ensures(RootAddressUri != null);
\r
151 Contract.EndContractBlock();
\r
153 _baseClient = new RestClient
\r
155 BaseAddress = accountInfo.StorageUri.ToString(),
\r
159 StorageUrl = accountInfo.StorageUri;
\r
160 Token = accountInfo.Token;
\r
161 UserName = accountInfo.UserName;
\r
163 //Get the root address (StorageUrl without the account)
\r
164 var storageUrl = StorageUrl.AbsoluteUri;
\r
165 var usernameIndex = storageUrl.LastIndexOf(UserName);
\r
166 var rootUrl = storageUrl.Substring(0, usernameIndex);
\r
167 RootAddressUri = new Uri(rootUrl);
\r
169 var httpClientHandler = new HttpClientHandler
\r
171 AllowAutoRedirect = true,
\r
172 AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
\r
177 _baseHttpClient = new HttpClient(httpClientHandler)
\r
179 BaseAddress = StorageUrl,
\r
180 Timeout = TimeSpan.FromSeconds(30)
\r
182 _baseHttpClient.DefaultRequestHeaders.Add(TOKEN_HEADER, Token);
\r
184 _baseHttpClientNoTimeout = new HttpClient(httpClientHandler)
\r
186 BaseAddress = StorageUrl,
\r
187 Timeout = TimeSpan.FromMilliseconds(-1)
\r
189 _baseHttpClientNoTimeout.DefaultRequestHeaders.Add(TOKEN_HEADER, Token);
\r
195 private static void AssertStatusOK(HttpResponseMessage response, string message)
\r
197 var statusCode = response.StatusCode;
\r
198 if (statusCode >= HttpStatusCode.BadRequest)
\r
199 throw new WebException(String.Format("{0} with code {1} - {2}", message, statusCode, response.ReasonPhrase));
\r
202 public async Task<AccountInfo> Authenticate()
\r
204 if (String.IsNullOrWhiteSpace(UserName))
\r
205 throw new InvalidOperationException("UserName is empty");
\r
206 if (String.IsNullOrWhiteSpace(ApiKey))
\r
207 throw new InvalidOperationException("ApiKey is empty");
\r
208 if (String.IsNullOrWhiteSpace(AuthenticationUrl))
\r
209 throw new InvalidOperationException("AuthenticationUrl is empty");
\r
210 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
\r
211 Contract.Ensures(StorageUrl != null);
\r
212 Contract.Ensures(_baseClient != null);
\r
213 Contract.Ensures(RootAddressUri != null);
\r
214 Contract.EndContractBlock();
\r
217 Log.InfoFormat("[AUTHENTICATE] Start for {0}", UserName);
\r
219 var groups = new List<Group>();
\r
221 using (var authClient = new HttpClient(_httpClientHandler,false){ BaseAddress = new Uri(AuthenticationUrl),Timeout=TimeSpan.FromSeconds(30) })
\r
224 authClient.DefaultRequestHeaders.Add("X-Auth-User", UserName);
\r
225 authClient.DefaultRequestHeaders.Add("X-Auth-Key", ApiKey);
\r
230 using (var response = await authClient.GetAsyncWithRetries(new Uri(VersionPath, UriKind.Relative),3).ConfigureAwait(false)) // .DownloadStringWithRetryRelative(new Uri(VersionPath, UriKind.Relative), 3);
\r
232 AssertStatusOK(response,"Authentication failed");
\r
234 storageUrl = response.Headers.GetFirstValue("X-Storage-Url");
\r
235 if (String.IsNullOrWhiteSpace(storageUrl))
\r
236 throw new InvalidOperationException("Failed to obtain storage url");
\r
238 token = response.Headers.GetFirstValue(TOKEN_HEADER);
\r
239 if (String.IsNullOrWhiteSpace(token))
\r
240 throw new InvalidOperationException("Failed to obtain token url");
\r
245 _baseClient = new RestClient
\r
247 BaseAddress = storageUrl,
\r
252 StorageUrl = new Uri(storageUrl);
\r
258 //Get the root address (StorageUrl without the account)
\r
259 var usernameIndex=storageUrl.LastIndexOf(UserName);
\r
260 var rootUrl = storageUrl.Substring(0, usernameIndex);
\r
261 RootAddressUri = new Uri(rootUrl);
\r
264 _baseHttpClient = new HttpClient(_httpClientHandler,false)
\r
266 BaseAddress = StorageUrl,
\r
267 Timeout = TimeSpan.FromSeconds(30)
\r
269 _baseHttpClient.DefaultRequestHeaders.Add(TOKEN_HEADER, token);
\r
271 _baseHttpClientNoTimeout = new HttpClient(_httpClientHandler,false)
\r
273 BaseAddress = StorageUrl,
\r
274 Timeout = TimeSpan.FromMilliseconds(-1)
\r
276 _baseHttpClientNoTimeout.DefaultRequestHeaders.Add(TOKEN_HEADER, token);
\r
278 /* var keys = authClient.ResponseHeaders.AllKeys.AsQueryable();
\r
279 groups = (from key in keys
\r
280 where key.StartsWith("X-Account-Group-")
\r
281 let name = key.Substring(16)
\r
282 select new Group(name, authClient.ResponseHeaders[key]))
\r
288 Log.InfoFormat("[AUTHENTICATE] End for {0}", UserName);
\r
289 Debug.Assert(_baseClient!=null);
\r
291 return new AccountInfo {StorageUri = StorageUrl, Token = Token, UserName = UserName,Groups=groups};
\r
295 private static void TraceStart(string method, Uri actualAddress)
\r
297 Log.InfoFormat("[{0}] {1} {2}", method, DateTime.Now, actualAddress);
\r
300 private async Task<string> GetStringAsync(Uri targetUri, string errorMessage,DateTimeOffset? since=null)
\r
302 TraceStart("GET",targetUri);
\r
303 var request = new HttpRequestMessage(HttpMethod.Get, targetUri);
\r
304 if (since.HasValue)
\r
306 request.Headers.IfModifiedSince = since.Value;
\r
308 using (var response = await _baseHttpClient.SendAsyncWithRetries(request,3).ConfigureAwait(false))
\r
310 AssertStatusOK(response, errorMessage);
\r
312 if (response.StatusCode == HttpStatusCode.NoContent)
\r
313 return String.Empty;
\r
315 var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
\r
320 public async Task<IList<ContainerInfo>> ListContainers(string account)
\r
323 var targetUrl = GetTargetUrl(account);
\r
324 var targetUri = new Uri(String.Format("{0}?format=json", targetUrl));
\r
325 var result = await GetStringAsync(targetUri, "List Containers failed").ConfigureAwait(false);
\r
326 if (String.IsNullOrWhiteSpace(result))
\r
327 return new List<ContainerInfo>();
\r
328 var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(result);
\r
329 foreach (var info in infos)
\r
331 info.Account = account;
\r
337 private string GetAccountUrl(string account)
\r
339 return RootAddressUri.Combine(account).AbsoluteUri;
\r
342 public IList<ShareAccountInfo> ListSharingAccounts(DateTime? since=null)
\r
344 using (ThreadContext.Stacks["Share"].Push("List Accounts"))
\r
346 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
348 var targetUri = new Uri(String.Format("{0}?format=json", RootAddressUri), UriKind.Absolute);
\r
349 var content=TaskEx.Run(async ()=>await GetStringAsync(targetUri, "ListSharingAccounts failed", since).ConfigureAwait(false)).Result;
\r
351 //If the result is empty, return an empty list,
\r
352 var infos = String.IsNullOrWhiteSpace(content)
\r
353 ? new List<ShareAccountInfo>()
\r
354 //Otherwise deserialize the account list into a list of ShareAccountInfos
\r
355 : JsonConvert.DeserializeObject<IList<ShareAccountInfo>>(content);
\r
357 Log.DebugFormat("END");
\r
364 /// Request listing of all objects in a container modified since a specific time.
\r
365 /// If the *since* value is missing, return all objects
\r
367 /// <param name="knownContainers">Use the since variable only for the containers listed in knownContainers. Unknown containers are considered new
\r
368 /// and should be polled anyway
\r
370 /// <param name="since"></param>
\r
371 /// <returns></returns>
\r
372 public IList<ObjectInfo> ListSharedObjects(HashSet<string> knownContainers, DateTimeOffset? since)
\r
375 using (ThreadContext.Stacks["Share"].Push("List Objects"))
\r
377 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
378 //'since' is not used here because we need to have ListObjects return a NoChange result
\r
379 //for all shared accounts,containers
\r
381 Func<ContainerInfo, string> getKey = c => String.Format("{0}\\{1}", c.Account, c.Name);
\r
383 var containers = (from account in ListSharingAccounts()
\r
384 let conts = ListContainers(account.name).Result
\r
385 from container in conts
\r
386 select container).ToList();
\r
387 var items = from container in containers
\r
388 let actualSince=knownContainers.Contains(getKey(container))?since:null
\r
389 select ListObjects(container.Account , container.Name, actualSince);
\r
390 var objects=items.SelectMany(r=> r).ToList();
\r
393 //Check parents recursively up to (but not including) the container.
\r
394 //If parents are missing, add them to the list
\r
395 //Need function to calculate all parent URLs
\r
396 objects = AddMissingParents(objects);
\r
398 //Store any new containers
\r
399 foreach (var container in containers)
\r
401 knownContainers.Add(getKey(container));
\r
406 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
411 private List<ObjectInfo> AddMissingParents(List<ObjectInfo> objects)
\r
413 //TODO: Remove short-circuit when we decide to use Missing Parents functionality
\r
416 var existingUris = objects.ToDictionary(o => o.Uri, o => o);
\r
417 foreach (var objectInfo in objects)
\r
419 //Can be null when retrieving objects to show in selective sync
\r
420 if (objectInfo.Name == null)
\r
423 //No need to unescape here, the parts will be used to create new ObjectInfos
\r
424 var parts = objectInfo.Name.ToString().Split(new[]{'/'},StringSplitOptions.RemoveEmptyEntries);
\r
425 //If there is no parent, skip
\r
426 if (parts.Length == 1)
\r
428 var baseParts = new[]
\r
430 objectInfo.Uri.Host, objectInfo.Uri.Segments[1].TrimEnd('/'),objectInfo.Account,objectInfo.Container.ToString()
\r
432 for (var partIdx = 0; partIdx < parts.Length - 1; partIdx++)
\r
434 var nameparts = parts.Range(0, partIdx).ToArray();
\r
435 var parentName= String.Join("/", nameparts);
\r
437 var parentParts = baseParts.Concat(nameparts);
\r
438 var parentUrl = objectInfo.Uri.Scheme+ "://" + String.Join("/", parentParts);
\r
440 var parentUri = new Uri(parentUrl, UriKind.Absolute);
\r
442 ObjectInfo existingInfo;
\r
443 if (!existingUris.TryGetValue(parentUri,out existingInfo))
\r
445 var h = parentUrl.GetHashCode();
\r
446 var reverse = new string(parentUrl.Reverse().ToArray());
\r
447 var rh = reverse.GetHashCode();
\r
448 var b1 = BitConverter.GetBytes(h);
\r
449 var b2 = BitConverter.GetBytes(rh);
\r
450 var g = new Guid(0,0,0,b1.Concat(b2).ToArray());
\r
453 existingUris[parentUri] = new ObjectInfo
\r
455 Account = objectInfo.Account,
\r
456 Container = objectInfo.Container,
\r
457 Content_Type = ObjectInfo.CONTENT_TYPE_DIRECTORY,
\r
458 ETag = Signature.MERKLE_EMPTY,
\r
459 X_Object_Hash = Signature.MERKLE_EMPTY,
\r
460 Name=new Uri(parentName,UriKind.Relative),
\r
461 StorageUri=objectInfo.StorageUri,
\r
463 UUID=g.ToString(),
\r
468 return existingUris.Values.ToList();
\r
471 public void SetTags(ObjectInfo target,IDictionary<string,string> tags)
\r
473 if (String.IsNullOrWhiteSpace(Token))
\r
474 throw new InvalidOperationException("The Token is not set");
\r
475 if (StorageUrl == null)
\r
476 throw new InvalidOperationException("The StorageUrl is not set");
\r
477 if (target == null)
\r
478 throw new ArgumentNullException("target");
\r
479 Contract.EndContractBlock();
\r
481 using (ThreadContext.Stacks["Share"].Push("Share Object"))
\r
483 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
485 using (var client = new RestClient(_baseClient))
\r
488 client.BaseAddress = GetAccountUrl(target.Account);
\r
490 client.Parameters.Clear();
\r
491 client.Parameters.Add("update", "");
\r
493 foreach (var tag in tags)
\r
495 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
\r
496 client.Headers.Add(headerTag, tag.Value);
\r
499 client.DownloadStringWithRetryRelative(target.Container, 3);
\r
502 client.AssertStatusOK("SetTags failed");
\r
503 //If the status is NOT ACCEPTED we have a problem
\r
504 if (client.StatusCode != HttpStatusCode.Accepted)
\r
506 Log.Error("Failed to set tags");
\r
507 throw new Exception("Failed to set tags");
\r
510 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
517 public void ShareObject(string account, Uri container, Uri objectName, string shareTo, bool read, bool write)
\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 (container==null)
\r
524 throw new ArgumentNullException("container");
\r
525 if (container.IsAbsoluteUri)
\r
526 throw new ArgumentException("container");
\r
527 if (objectName==null)
\r
528 throw new ArgumentNullException("objectName");
\r
529 if (objectName.IsAbsoluteUri)
\r
530 throw new ArgumentException("objectName");
\r
531 if (String.IsNullOrWhiteSpace(account))
\r
532 throw new ArgumentNullException("account");
\r
533 if (String.IsNullOrWhiteSpace(shareTo))
\r
534 throw new ArgumentNullException("shareTo");
\r
535 Contract.EndContractBlock();
\r
537 using (ThreadContext.Stacks["Share"].Push("Share Object"))
\r
539 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
541 using (var client = new RestClient(_baseClient))
\r
544 client.BaseAddress = GetAccountUrl(account);
\r
546 client.Parameters.Clear();
\r
547 client.Parameters.Add("format", "json");
\r
549 string permission = "";
\r
551 permission = String.Format("write={0}", shareTo);
\r
553 permission = String.Format("read={0}", shareTo);
\r
554 client.Headers.Add("X-Object-Sharing", permission);
\r
556 var content = client.DownloadStringWithRetryRelative(container, 3);
\r
558 client.AssertStatusOK("ShareObject failed");
\r
560 //If the result is empty, return an empty list,
\r
561 var infos = String.IsNullOrWhiteSpace(content)
\r
562 ? new List<ObjectInfo>()
\r
563 //Otherwise deserialize the object list into a list of ObjectInfos
\r
564 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
566 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
573 public async Task<AccountInfo> GetAccountPolicies(AccountInfo accountInfo)
\r
575 if (accountInfo==null)
\r
576 throw new ArgumentNullException("accountInfo");
\r
577 Contract.EndContractBlock();
\r
579 using (ThreadContext.Stacks["Account"].Push("GetPolicies"))
\r
581 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
584 if (_baseClient == null)
\r
586 _baseClient = new RestClient
\r
588 BaseAddress = accountInfo.StorageUri.ToString(),
\r
595 var containerUri = GetTargetUri(accountInfo.UserName);
\r
596 var targetUri = new Uri(String.Format("{0}?format=json", containerUri), UriKind.Absolute);
\r
597 using(var response=await _baseHttpClient.HeadAsyncWithRetries(targetUri,3).ConfigureAwait(false))
\r
600 var quotaValue=response.Headers.GetFirstValue("X-Account-Policy-Quota");
\r
601 var bytesValue = response.Headers.GetFirstValue("X-Account-Bytes-Used");
\r
603 if (long.TryParse(quotaValue, out quota))
\r
604 accountInfo.Quota = quota;
\r
605 if (long.TryParse(bytesValue, out bytes))
\r
606 accountInfo.BytesUsed = bytes;
\r
608 return accountInfo;
\r
612 //using (var client = new RestClient(_baseClient))
\r
614 // if (!String.IsNullOrWhiteSpace(accountInfo.UserName))
\r
615 // client.BaseAddress = GetAccountUrl(accountInfo.UserName);
\r
617 // client.Parameters.Clear();
\r
618 // client.Parameters.Add("format", "json");
\r
619 // client.Head(_emptyUri, 3);
\r
621 // var quotaValue=client.ResponseHeaders["X-Account-Policy-Quota"];
\r
622 // var bytesValue= client.ResponseHeaders["X-Account-Bytes-Used"];
\r
624 // long quota, bytes;
\r
625 // if (long.TryParse(quotaValue, out quota))
\r
626 // accountInfo.Quota = quota;
\r
627 // if (long.TryParse(bytesValue, out bytes))
\r
628 // accountInfo.BytesUsed = bytes;
\r
630 // return accountInfo;
\r
637 public void UpdateMetadata(ObjectInfo objectInfo)
\r
639 if (objectInfo == null)
\r
640 throw new ArgumentNullException("objectInfo");
\r
641 Contract.EndContractBlock();
\r
643 using (ThreadContext.Stacks["Objects"].Push("UpdateMetadata"))
\r
645 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
648 using(var client=new RestClient(_baseClient))
\r
651 client.BaseAddress = GetAccountUrl(objectInfo.Account);
\r
653 client.Parameters.Clear();
\r
657 foreach (var tag in objectInfo.Tags)
\r
659 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
\r
660 client.Headers.Add(headerTag, tag.Value);
\r
665 var permissions=objectInfo.GetPermissionString();
\r
666 client.SetNonEmptyHeaderValue("X-Object-Sharing",permissions);
\r
668 client.SetNonEmptyHeaderValue("Content-Disposition",objectInfo.ContendDisposition);
\r
669 client.SetNonEmptyHeaderValue("Content-Encoding",objectInfo.ContentEncoding);
\r
670 client.SetNonEmptyHeaderValue("X-Object-Manifest",objectInfo.Manifest);
\r
671 var isPublic = objectInfo.IsPublic.ToString().ToLower();
\r
672 client.Headers.Add("X-Object-Public", isPublic);
\r
675 var address = String.Format("{0}/{1}?update=",objectInfo.Container, objectInfo.Name);
\r
676 client.PostWithRetry(new Uri(address,UriKind.Relative),"application/xml");
\r
678 client.AssertStatusOK("UpdateMetadata failed");
\r
679 //If the status is NOT ACCEPTED or OK we have a problem
\r
680 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
\r
682 Log.Error("Failed to update metadata");
\r
683 throw new Exception("Failed to update metadata");
\r
686 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
692 public void UpdateMetadata(ContainerInfo containerInfo)
\r
694 if (containerInfo == null)
\r
695 throw new ArgumentNullException("containerInfo");
\r
696 Contract.EndContractBlock();
\r
698 using (ThreadContext.Stacks["Containers"].Push("UpdateMetadata"))
\r
700 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
703 using(var client=new RestClient(_baseClient))
\r
706 client.BaseAddress = GetAccountUrl(containerInfo.Account);
\r
708 client.Parameters.Clear();
\r
712 foreach (var tag in containerInfo.Tags)
\r
714 var headerTag = String.Format("X-Container-Meta-{0}", tag.Key);
\r
715 client.Headers.Add(headerTag, tag.Value);
\r
720 foreach (var policy in containerInfo.Policies)
\r
722 var headerPolicy = String.Format("X-Container-Policy-{0}", policy.Key);
\r
723 client.Headers.Add(headerPolicy, policy.Value);
\r
727 var uriBuilder = client.GetAddressBuilder(containerInfo.Name,_emptyUri);
\r
728 var uri = uriBuilder.Uri;
\r
730 client.UploadValues(uri,new NameValueCollection());
\r
733 client.AssertStatusOK("UpdateMetadata failed");
\r
734 //If the status is NOT ACCEPTED or OK we have a problem
\r
735 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
\r
737 Log.Error("Failed to update metadata");
\r
738 throw new Exception("Failed to update metadata");
\r
741 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
750 public IList<ObjectInfo> ListObjects(string account, Uri container, DateTimeOffset? since = null)
\r
752 if (container==null)
\r
753 throw new ArgumentNullException("container");
\r
754 if (container.IsAbsoluteUri)
\r
755 throw new ArgumentException("container");
\r
756 Contract.EndContractBlock();
\r
758 using (ThreadContext.Stacks["Objects"].Push("List"))
\r
761 var containerUri = GetTargetUri(account).Combine(container);
\r
762 var targetUri = new Uri(String.Format("{0}?format=json", containerUri), UriKind.Absolute);
\r
764 var content =TaskEx.Run(async ()=>await GetStringAsync(targetUri, "ListObjects failed", since).ConfigureAwait(false)).Result;
\r
766 //304 will result in an empty string. Empty containers return an empty json array
\r
767 if (String.IsNullOrWhiteSpace(content))
\r
768 return new[] {new NoModificationInfo(account, container)};
\r
770 //If the result is empty, return an empty list,
\r
771 var infos = String.IsNullOrWhiteSpace(content)
\r
772 ? new List<ObjectInfo>()
\r
773 //Otherwise deserialize the object list into a list of ObjectInfos
\r
774 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
776 foreach (var info in infos)
\r
778 info.Container = container;
\r
779 info.Account = account;
\r
780 info.StorageUri = StorageUrl;
\r
782 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
787 public IList<ObjectInfo> ListObjects(string account, Uri container, Uri folder, DateTimeOffset? since = null)
\r
789 if (container==null)
\r
790 throw new ArgumentNullException("container");
\r
791 if (container.IsAbsoluteUri)
\r
792 throw new ArgumentException("container");
\r
793 Contract.EndContractBlock();
\r
795 using (ThreadContext.Stacks["Objects"].Push("List"))
\r
797 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
799 var containerUri = GetTargetUri(account).Combine(container);
\r
800 var targetUri = new Uri(String.Format("{0}?format=json&path={1}", containerUri,folder), UriKind.Absolute);
\r
801 var content = TaskEx.Run(async ()=>await GetStringAsync(targetUri, "ListObjects failed", since).ConfigureAwait(false)).Result;
\r
803 //304 will result in an empty string. Empty containers return an empty json array
\r
804 if (String.IsNullOrWhiteSpace(content))
\r
805 return new[] { new NoModificationInfo(account, container) };
\r
808 var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
809 foreach (var info in infos)
\r
811 info.Account = account;
\r
812 if (info.Container == null)
\r
813 info.Container = container;
\r
814 info.StorageUri = StorageUrl;
\r
816 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
819 using (var client = new RestClient(_baseClient))
\r
821 if (!String.IsNullOrWhiteSpace(account))
\r
822 client.BaseAddress = GetAccountUrl(account);
\r
824 client.Parameters.Clear();
\r
825 client.Parameters.Add("format", "json");
\r
826 client.Parameters.Add("path", folder.ToString());
\r
827 client.IfModifiedSince = since;
\r
828 var content = client.DownloadStringWithRetryRelative(container, 3);
\r
829 client.AssertStatusOK("ListObjects failed");
\r
831 if (client.StatusCode==HttpStatusCode.NotModified)
\r
832 return new[]{new NoModificationInfo(account,container,folder)};
\r
834 var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
835 foreach (var info in infos)
\r
837 info.Account = account;
\r
838 if (info.Container == null)
\r
839 info.Container = container;
\r
840 info.StorageUri = StorageUrl;
\r
842 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
850 public bool ContainerExists(string account, Uri container)
\r
852 if (container==null)
\r
853 throw new ArgumentNullException("container", "The container property can't be empty");
\r
854 if (container.IsAbsoluteUri)
\r
855 throw new ArgumentException( "The container must be relative","container");
\r
856 Contract.EndContractBlock();
\r
858 using (ThreadContext.Stacks["Containters"].Push("Exists"))
\r
860 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
862 var targetUri = GetTargetUri(account).Combine(container);
\r
864 using (var response = _baseHttpClient.HeadAsyncWithRetries(targetUri, 3).Result)
\r
868 switch (response.StatusCode)
\r
870 case HttpStatusCode.OK:
\r
871 case HttpStatusCode.NoContent:
\r
874 case HttpStatusCode.NotFound:
\r
878 throw CreateWebException("ContainerExists", response.StatusCode);
\r
880 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
885 using (var client = new RestClient(_baseClient))
\r
887 if (!String.IsNullOrWhiteSpace(account))
\r
888 client.BaseAddress = GetAccountUrl(account);
\r
890 client.Parameters.Clear();
\r
891 client.Head(container, 3);
\r
894 switch (client.StatusCode)
\r
896 case HttpStatusCode.OK:
\r
897 case HttpStatusCode.NoContent:
\r
900 case HttpStatusCode.NotFound:
\r
904 throw CreateWebException("ContainerExists", client.StatusCode);
\r
906 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
915 private Uri GetTargetUri(string account)
\r
917 return new Uri(GetTargetUrl(account),UriKind.Absolute);
\r
920 private string GetTargetUrl(string account)
\r
922 return String.IsNullOrWhiteSpace(account)
\r
923 ? _baseHttpClient.BaseAddress.ToString()
\r
924 : GetAccountUrl(account);
\r
927 public bool ObjectExists(string account, Uri container, Uri objectName)
\r
929 if (container == null)
\r
930 throw new ArgumentNullException("container", "The container property can't be empty");
\r
931 if (container.IsAbsoluteUri)
\r
932 throw new ArgumentException("The container must be relative","container");
\r
933 if (objectName == null)
\r
934 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
935 if (objectName.IsAbsoluteUri)
\r
936 throw new ArgumentException("The objectName must be relative","objectName");
\r
937 Contract.EndContractBlock();
\r
939 var targetUri=GetTargetUri(account).Combine(container).Combine(objectName);
\r
941 using (var response = _baseHttpClient.HeadAsyncWithRetries(targetUri, 3).Result)
\r
943 switch (response.StatusCode)
\r
945 case HttpStatusCode.OK:
\r
946 case HttpStatusCode.NoContent:
\r
948 case HttpStatusCode.NotFound:
\r
951 throw CreateWebException("ObjectExists", response.StatusCode);
\r
956 using (var client = new RestClient(_baseClient))
\r
958 if (!String.IsNullOrWhiteSpace(account))
\r
959 client.BaseAddress = GetAccountUrl(account);
\r
961 client.Parameters.Clear();
\r
962 client.Head(container.Combine(objectName), 3);
\r
964 switch (client.StatusCode)
\r
966 case HttpStatusCode.OK:
\r
967 case HttpStatusCode.NoContent:
\r
969 case HttpStatusCode.NotFound:
\r
972 throw CreateWebException("ObjectExists", client.StatusCode);
\r
979 public async Task<ObjectInfo> GetObjectInfo(string account, Uri container, Uri objectName)
\r
981 if (container == null)
\r
982 throw new ArgumentNullException("container", "The container property can't be empty");
\r
983 if (container.IsAbsoluteUri)
\r
984 throw new ArgumentException("The container must be relative", "container");
\r
985 if (objectName == null)
\r
986 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
987 if (objectName.IsAbsoluteUri)
\r
988 throw new ArgumentException("The objectName must be relative", "objectName");
\r
989 Contract.EndContractBlock();
\r
991 using (ThreadContext.Stacks["Objects"].Push("GetObjectInfo"))
\r
994 var targetUri = GetTargetUri(account).Combine(container).Combine(objectName);
\r
997 using (var response = await _baseHttpClient.HeadAsyncWithRetries(targetUri, 3))
\r
999 switch (response.StatusCode)
\r
1001 case HttpStatusCode.OK:
\r
1002 case HttpStatusCode.NoContent:
\r
1003 var tags = response.Headers.GetMeta("X-Object-Meta-");
\r
1004 var extensions = (from header in response.Headers
\r
1006 header.Key.StartsWith("X-Object-") &&
\r
1007 !header.Key.StartsWith("X-Object-Meta-")
\r
1008 select new {Name = header.Key, Value = header.Value.FirstOrDefault()})
\r
1009 .ToDictionary(t => t.Name, t => t.Value);
\r
1011 var permissions = response.Headers.GetFirstValue("X-Object-Sharing");
\r
1014 var info = new ObjectInfo
\r
1016 Account = account,
\r
1017 Container = container,
\r
1018 Name = objectName,
\r
1019 ETag = response.Headers.ETag.Tag,
\r
1020 UUID = response.Headers.GetFirstValue("X-Object-UUID"),
\r
1021 X_Object_Hash = response.Headers.GetFirstValue("X-Object-Hash"),
\r
1022 Content_Type = response.Headers.GetFirstValue("Content-Type"),
\r
1023 Bytes = Convert.ToInt64(response.Content.Headers.ContentLength),
\r
1025 Last_Modified = response.Content.Headers.LastModified,
\r
1026 Extensions = extensions,
\r
1028 response.Content.Headers.ContentEncoding.FirstOrDefault(),
\r
1029 ContendDisposition =
\r
1030 response.Content.Headers.ContentDisposition.ToString(),
\r
1031 Manifest = response.Headers.GetFirstValue("X-Object-Manifest"),
\r
1032 PublicUrl = response.Headers.GetFirstValue("X-Object-Public"),
\r
1033 StorageUri = StorageUrl,
\r
1035 info.SetPermissions(permissions);
\r
1037 case HttpStatusCode.NotFound:
\r
1038 return ObjectInfo.Empty;
\r
1040 throw new WebException(
\r
1041 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
\r
1042 objectName, response.StatusCode));
\r
1046 catch (RetryException)
\r
1048 Log.WarnFormat("[RETRY FAIL] GetObjectInfo for {0} failed.", objectName);
\r
1049 return ObjectInfo.Empty;
\r
1051 catch (WebException e)
\r
1054 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status {1}",
\r
1055 objectName, e.Status), e);
\r
1058 } /* using (var client = new RestClient(_baseClient))
\r
1060 if (!String.IsNullOrWhiteSpace(account))
\r
1061 client.BaseAddress = GetAccountUrl(account);
\r
1064 client.Parameters.Clear();
\r
1066 client.Head(container.Combine(objectName), 3);
\r
1068 if (client.TimedOut)
\r
1069 return ObjectInfo.Empty;
\r
1071 switch (client.StatusCode)
\r
1073 case HttpStatusCode.OK:
\r
1074 case HttpStatusCode.NoContent:
\r
1075 var keys = client.ResponseHeaders.AllKeys.AsQueryable();
\r
1076 var tags = client.GetMeta("X-Object-Meta-");
\r
1077 var extensions = (from key in keys
\r
1078 where key.StartsWith("X-Object-") && !key.StartsWith("X-Object-Meta-")
\r
1079 select new {Name = key, Value = client.ResponseHeaders[key]})
\r
1080 .ToDictionary(t => t.Name, t => t.Value);
\r
1082 var permissions=client.GetHeaderValue("X-Object-Sharing", true);
\r
1085 var info = new ObjectInfo
\r
1087 Account = account,
\r
1088 Container = container,
\r
1089 Name = objectName,
\r
1090 ETag = client.GetHeaderValue("ETag"),
\r
1091 UUID=client.GetHeaderValue("X-Object-UUID"),
\r
1092 X_Object_Hash = client.GetHeaderValue("X-Object-Hash"),
\r
1093 Content_Type = client.GetHeaderValue("Content-Type"),
\r
1094 Bytes = Convert.ToInt64(client.GetHeaderValue("Content-Length",true)),
\r
1096 Last_Modified = client.LastModified,
\r
1097 Extensions = extensions,
\r
1098 ContentEncoding=client.GetHeaderValue("Content-Encoding",true),
\r
1099 ContendDisposition = client.GetHeaderValue("Content-Disposition",true),
\r
1100 Manifest=client.GetHeaderValue("X-Object-Manifest",true),
\r
1101 PublicUrl=client.GetHeaderValue("X-Object-Public",true),
\r
1102 StorageUri=StorageUrl,
\r
1104 info.SetPermissions(permissions);
\r
1106 case HttpStatusCode.NotFound:
\r
1107 return ObjectInfo.Empty;
\r
1109 throw new WebException(
\r
1110 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
\r
1111 objectName, client.StatusCode));
\r
1115 catch (RetryException)
\r
1117 Log.WarnFormat("[RETRY FAIL] GetObjectInfo for {0} failed.",objectName);
\r
1118 return ObjectInfo.Empty;
\r
1120 catch (WebException e)
\r
1123 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
\r
1124 objectName, client.StatusCode), e);
\r
1134 public void CreateFolder(string account, Uri container, Uri folder)
\r
1136 if (container == null)
\r
1137 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1138 if (container.IsAbsoluteUri)
\r
1139 throw new ArgumentException("The container must be relative","container");
\r
1140 if (folder == null)
\r
1141 throw new ArgumentNullException("folder", "The objectName property can't be empty");
\r
1142 if (folder.IsAbsoluteUri)
\r
1143 throw new ArgumentException("The objectName must be relative","folder");
\r
1144 Contract.EndContractBlock();
\r
1146 var folderUri=container.Combine(folder);
\r
1147 var targetUri = GetTargetUri(account).Combine(folderUri);
\r
1148 var message = new HttpRequestMessage(HttpMethod.Put, targetUri);
\r
1150 message.Headers.Add("Content-Type", ObjectInfo.CONTENT_TYPE_DIRECTORY);
\r
1151 message.Headers.Add("Content-Length", "0");
\r
1152 using (var response = _baseHttpClient.SendAsyncWithRetries(message, 3).Result)
\r
1154 if (response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.Accepted)
\r
1155 throw CreateWebException("CreateFolder", response.StatusCode);
\r
1158 using (var client = new RestClient(_baseClient))
\r
1160 if (!String.IsNullOrWhiteSpace(account))
\r
1161 client.BaseAddress = GetAccountUrl(account);
\r
1163 client.Parameters.Clear();
\r
1164 client.Headers.Add("Content-Type", ObjectInfo.CONTENT_TYPE_DIRECTORY);
\r
1165 client.Headers.Add("Content-Length", "0");
\r
1166 client.PutWithRetry(folderUri, 3);
\r
1168 if (client.StatusCode != HttpStatusCode.Created && client.StatusCode != HttpStatusCode.Accepted)
\r
1169 throw CreateWebException("CreateFolder", client.StatusCode);
\r
1174 private Dictionary<string, string> GetMeta(HttpResponseMessage response,string metaPrefix)
\r
1176 if (String.IsNullOrWhiteSpace(metaPrefix))
\r
1177 throw new ArgumentNullException("metaPrefix");
\r
1178 Contract.EndContractBlock();
\r
1180 var dict = (from header in response.Headers
\r
1181 where header.Key.StartsWith(metaPrefix)
\r
1182 select new { Name = header.Key, Value = String.Join(",", header.Value) })
\r
1183 .ToDictionary(t => t.Name, t => t.Value);
\r
1190 public ContainerInfo GetContainerInfo(string account, Uri container)
\r
1192 if (container == null)
\r
1193 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1194 if (container.IsAbsoluteUri)
\r
1195 throw new ArgumentException("The container must be relative","container");
\r
1196 Contract.EndContractBlock();
\r
1198 var targetUri = GetTargetUri(account).Combine(container);
\r
1199 using (var response = _baseHttpClient.HeadAsyncWithRetries(targetUri, 3).Result)
\r
1201 if (Log.IsDebugEnabled)
\r
1202 Log.DebugFormat("ContainerInfo data: {0}\n{1}",response,response.Content.ReadAsStringAsync().Result);
\r
1203 switch (response.StatusCode)
\r
1205 case HttpStatusCode.OK:
\r
1206 case HttpStatusCode.NoContent:
\r
1207 var tags = GetMeta(response,"X-Container-Meta-");
\r
1208 var policies = GetMeta(response,"X-Container-Policy-");
\r
1210 var containerInfo = new ContainerInfo
\r
1212 Account = account,
\r
1214 StorageUrl = StorageUrl.ToString(),
\r
1215 Count =long.Parse(response.Headers.GetFirstValue("X-Container-Object-Count")),
\r
1216 Bytes = long.Parse(response.Headers.GetFirstValue("X-Container-Bytes-Used")),
\r
1217 BlockHash = response.Headers.GetFirstValue("X-Container-Block-Hash"),
\r
1219 int.Parse(response.Headers.GetFirstValue("X-Container-Block-Size")),
\r
1220 Last_Modified = response.Content.Headers.LastModified,
\r
1222 Policies = policies
\r
1226 return containerInfo;
\r
1227 case HttpStatusCode.NotFound:
\r
1228 return ContainerInfo.Empty;
\r
1230 throw CreateWebException("GetContainerInfo", response.StatusCode);
\r
1235 public void CreateContainer(string account, Uri container)
\r
1237 if (container == null)
\r
1238 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1239 if (container.IsAbsoluteUri)
\r
1240 throw new ArgumentException("The container must be relative","container");
\r
1241 Contract.EndContractBlock();
\r
1243 var targetUri=GetTargetUri(account).Combine(container);
\r
1244 var message = new HttpRequestMessage(HttpMethod.Put, targetUri);
\r
1245 message.Headers.Add("Content-Length", "0");
\r
1246 using (var response = _baseHttpClient.SendAsyncWithRetries(message, 3).Result)
\r
1248 var expectedCodes = new[] {HttpStatusCode.Created, HttpStatusCode.Accepted, HttpStatusCode.OK};
\r
1249 if (!expectedCodes.Contains(response.StatusCode))
\r
1250 throw CreateWebException("CreateContainer", response.StatusCode);
\r
1253 using (var client = new RestClient(_baseClient))
\r
1255 if (!String.IsNullOrWhiteSpace(account))
\r
1256 client.BaseAddress = GetAccountUrl(account);
\r
1258 client.PutWithRetry(container, 3);
\r
1259 var expectedCodes = new[] {HttpStatusCode.Created, HttpStatusCode.Accepted, HttpStatusCode.OK};
\r
1260 if (!expectedCodes.Contains(client.StatusCode))
\r
1261 throw CreateWebException("CreateContainer", client.StatusCode);
\r
1266 public async Task WipeContainer(string account, Uri container)
\r
1268 if (container == null)
\r
1269 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1270 if (container.IsAbsoluteUri)
\r
1271 throw new ArgumentException("The container must be relative", "container");
\r
1272 Contract.EndContractBlock();
\r
1274 await DeleteContainer(account, new Uri(String.Format("{0}?delimiter=/", container), UriKind.Relative)).ConfigureAwait(false);
\r
1278 public async Task DeleteContainer(string account, Uri container)
\r
1280 if (container == null)
\r
1281 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1282 if (container.IsAbsoluteUri)
\r
1283 throw new ArgumentException("The container must be relative","container");
\r
1284 Contract.EndContractBlock();
\r
1286 var targetUri = GetTargetUri(account).Combine(container);
\r
1287 var message = new HttpRequestMessage(HttpMethod.Delete, targetUri);
\r
1288 using (var response = await _baseHttpClient.SendAsyncWithRetries(message, 3).ConfigureAwait(false))
\r
1290 var expectedCodes = new[] { HttpStatusCode.NotFound, HttpStatusCode.NoContent };
\r
1291 if (!expectedCodes.Contains(response.StatusCode))
\r
1292 throw CreateWebException("DeleteContainer", response.StatusCode);
\r
1300 /// <param name="account"></param>
\r
1301 /// <param name="container"></param>
\r
1302 /// <param name="objectName"></param>
\r
1303 /// <param name="fileName"></param>
\r
1304 /// <param name="cancellationToken"> </param>
\r
1305 /// <returns></returns>
\r
1306 /// <remarks>This method should have no timeout or a very long one</remarks>
\r
1307 //Asynchronously download the object specified by *objectName* in a specific *container* to
\r
1309 public async Task GetObject(string account, Uri container, Uri objectName, string fileName,CancellationToken cancellationToken)
\r
1311 if (container == null)
\r
1312 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1313 if (container.IsAbsoluteUri)
\r
1314 throw new ArgumentException("The container must be relative","container");
\r
1315 if (objectName == null)
\r
1316 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1317 if (objectName.IsAbsoluteUri)
\r
1318 throw new ArgumentException("The objectName must be relative","objectName");
\r
1319 Contract.EndContractBlock();
\r
1324 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
\r
1325 //object to avoid concurrency errors.
\r
1327 //Download operations take a long time therefore they have no timeout.
\r
1328 using(var client = new RestClient(_baseClient) { Timeout = 0 })
\r
1330 if (!String.IsNullOrWhiteSpace(account))
\r
1331 client.BaseAddress = GetAccountUrl(account);
\r
1333 //The container and objectName are relative names. They are joined with the client's
\r
1334 //BaseAddress to create the object's absolute address
\r
1335 var builder = client.GetAddressBuilder(container, objectName);
\r
1336 var uri = builder.Uri;
\r
1338 //Download progress is reported to the Trace log
\r
1339 Log.InfoFormat("[GET] START {0}", objectName);
\r
1340 /*client.DownloadProgressChanged += (sender, args) =>
\r
1341 Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1342 fileName, args.ProgressPercentage,
\r
1343 args.BytesReceived,
\r
1344 args.TotalBytesToReceive);*/
\r
1345 var progress = new Progress<DownloadProgressChangedEventArgs>(args =>
\r
1347 Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1348 fileName, args.ProgressPercentage,
\r
1349 args.BytesReceived,
\r
1350 args.TotalBytesToReceive);
\r
1351 if (DownloadProgressChanged!=null)
\r
1352 DownloadProgressChanged(this, new DownloadArgs(args));
\r
1355 //Start downloading the object asynchronously
\r
1356 await client.DownloadFileTaskAsync(uri, fileName, cancellationToken,progress).ConfigureAwait(false);
\r
1358 //Once the download completes
\r
1359 //Delete the local client object
\r
1361 //And report failure or completion
\r
1363 catch (Exception exc)
\r
1365 Log.ErrorFormat("[GET] FAIL {0} with {1}", objectName, exc);
\r
1369 Log.InfoFormat("[GET] END {0}", objectName);
\r
1374 public async Task<IList<string>> PutHashMap(string account, Uri container, Uri objectName, TreeHash hash)
\r
1376 if (container == null)
\r
1377 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1378 if (container.IsAbsoluteUri)
\r
1379 throw new ArgumentException("The container must be relative","container");
\r
1380 if (objectName == null)
\r
1381 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1382 if (objectName.IsAbsoluteUri)
\r
1383 throw new ArgumentException("The objectName must be relative","objectName");
\r
1385 throw new ArgumentNullException("hash");
\r
1386 if (String.IsNullOrWhiteSpace(Token))
\r
1387 throw new InvalidOperationException("Invalid Token");
\r
1388 if (StorageUrl == null)
\r
1389 throw new InvalidOperationException("Invalid Storage Url");
\r
1390 Contract.EndContractBlock();
\r
1394 //The container and objectName are relative names. They are joined with the client's
\r
1395 //BaseAddress to create the object's absolute address
\r
1397 var targetUri = GetTargetUri(account).Combine(container).Combine(objectName);
\r
1400 var uri = new Uri(String.Format("{0}?format=json&hashmap",targetUri),UriKind.Absolute);
\r
1403 //Send the tree hash as Json to the server
\r
1404 var jsonHash = hash.ToJson();
\r
1405 if (Log.IsDebugEnabled)
\r
1406 Log.DebugFormat("Hashes:\r\n{0}", jsonHash);
\r
1408 var message = new HttpRequestMessage(HttpMethod.Put, uri)
\r
1410 Content = new StringContent(jsonHash)
\r
1412 message.Headers.Add("ETag",hash.TopHash.ToHashString());
\r
1414 //Don't use a timeout because putting the hashmap may be a long process
\r
1416 using (var response = await _baseHttpClientNoTimeout.SendAsyncWithRetries(message, 3).ConfigureAwait(false))
\r
1418 var empty = (IList<string>)new List<string>();
\r
1420 switch (response.StatusCode)
\r
1422 case HttpStatusCode.Created:
\r
1423 //The server will respond either with 201-created if all blocks were already on the server
\r
1425 case HttpStatusCode.Conflict:
\r
1426 //or with a 409-conflict and return the list of missing parts
\r
1427 using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
\r
1428 using(var reader=stream.GetLoggedReader(Log))
\r
1430 var serializer = new JsonSerializer();
\r
1431 serializer.Error += (sender, args) => Log.ErrorFormat("Deserialization error at [{0}] [{1}]", args.ErrorContext.Error, args.ErrorContext.Member);
\r
1432 var hashes = (List<string>)serializer.Deserialize(reader, typeof(List<string>));
\r
1436 //All other cases are unexpected
\r
1437 //Ensure that failure codes raise exceptions
\r
1438 response.EnsureSuccessStatusCode();
\r
1439 //And log any other codes as warngings, but continute processing
\r
1440 Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",response.StatusCode,response.ReasonPhrase);
\r
1448 public async Task<byte[]> GetBlock(string account, Uri container, Uri relativeUrl, long start, long? end, CancellationToken cancellationToken)
\r
1450 if (String.IsNullOrWhiteSpace(Token))
\r
1451 throw new InvalidOperationException("Invalid Token");
\r
1452 if (StorageUrl == null)
\r
1453 throw new InvalidOperationException("Invalid Storage Url");
\r
1454 if (container == null)
\r
1455 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1456 if (container.IsAbsoluteUri)
\r
1457 throw new ArgumentException("The container must be relative","container");
\r
1458 if (relativeUrl == null)
\r
1459 throw new ArgumentNullException("relativeUrl");
\r
1460 if (end.HasValue && end < 0)
\r
1461 throw new ArgumentOutOfRangeException("end");
\r
1463 throw new ArgumentOutOfRangeException("start");
\r
1464 Contract.EndContractBlock();
\r
1467 var targetUri = GetTargetUri(account).Combine(container).Combine(relativeUrl);
\r
1468 var message = new HttpRequestMessage(HttpMethod.Get, targetUri);
\r
1469 message.Headers.Range=new RangeHeaderValue(start,end);
\r
1471 //Don't use a timeout because putting the hashmap may be a long process
\r
1473 IProgress<DownloadArgs> progress = new Progress<DownloadArgs>(args =>
\r
1475 Log.DebugFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1476 targetUri.Segments.Last(), args.ProgressPercentage,
\r
1477 args.BytesReceived,
\r
1478 args.TotalBytesToReceive);
\r
1480 if (DownloadProgressChanged!=null)
\r
1481 DownloadProgressChanged(this, args);
\r
1485 using (var response = await _baseHttpClientNoTimeout.SendAsyncWithRetries(message, 3, HttpCompletionOption.ResponseHeadersRead,
\r
1486 cancellationToken).ConfigureAwait(false))
\r
1487 using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
\r
1488 using(var targetStream=new MemoryStream())
\r
1491 long totalSize = response.Content.Headers.ContentLength ?? 0;
\r
1493 var buffer = new byte[65536];
\r
1495 while ((read = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0)
\r
1498 progress.Report(new DownloadArgs(total, totalSize));
\r
1499 await targetStream.WriteAsync(buffer, 0, read).ConfigureAwait(false);
\r
1502 var result = targetStream.ToArray();
\r
1508 public event EventHandler<UploadArgs> UploadProgressChanged;
\r
1509 public event EventHandler<DownloadArgs> DownloadProgressChanged;
\r
1512 public async Task PostBlock(string account, Uri container, byte[] block, int offset, int count,string blockHash,CancellationToken token)
\r
1514 if (container == null)
\r
1515 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1516 if (container.IsAbsoluteUri)
\r
1517 throw new ArgumentException("The container must be relative","container");
\r
1518 if (block == null)
\r
1519 throw new ArgumentNullException("block");
\r
1520 if (offset < 0 || offset >= block.Length)
\r
1521 throw new ArgumentOutOfRangeException("offset");
\r
1522 if (count < 0 || count > block.Length)
\r
1523 throw new ArgumentOutOfRangeException("count");
\r
1524 if (String.IsNullOrWhiteSpace(Token))
\r
1525 throw new InvalidOperationException("Invalid Token");
\r
1526 if (StorageUrl == null)
\r
1527 throw new InvalidOperationException("Invalid Storage Url");
\r
1528 Contract.EndContractBlock();
\r
1533 var containerUri = GetTargetUri(account).Combine(container);
\r
1534 var targetUri = new Uri(String.Format("{0}?update", containerUri));
\r
1537 //Don't use a timeout because putting the hashmap may be a long process
\r
1540 Log.InfoFormat("[BLOCK POST] START");
\r
1543 var progress = new Progress<UploadArgs>(args =>
\r
1545 Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",
\r
1546 args.ProgressPercentage,
\r
1548 args.TotalBytesToSend);
\r
1549 if (UploadProgressChanged != null)
\r
1550 UploadProgressChanged(this,args);
\r
1553 var message = new HttpRequestMessage(HttpMethod.Post, targetUri)
\r
1555 Content = new ByteArrayContentWithProgress(block, offset, count,progress)
\r
1557 message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(@"application/octet-stream");
\r
1560 using (var response = await _baseHttpClientNoTimeout.SendAsyncWithRetries(message, 3,HttpCompletionOption.ResponseContentRead,token).ConfigureAwait(false))
\r
1562 Log.InfoFormat("[BLOCK POST PROGRESS] Completed ");
\r
1563 response.EnsureSuccessStatusCode();
\r
1564 var responseHash = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
\r
1565 var cleanHash = responseHash.TrimEnd();
\r
1566 Debug.Assert(blockHash==cleanHash);
\r
1567 if (responseHash.Equals(cleanHash,StringComparison.OrdinalIgnoreCase))
\r
1568 Log.ErrorFormat("Block hash mismatch posting to [{0}]:[{1}], expected [{2}] but was [{3}]",account,container,blockHash,responseHash);
\r
1570 Log.InfoFormat("[BLOCK POST] END");
\r
1572 catch (TaskCanceledException )
\r
1574 Log.Info("Aborting block");
\r
1577 catch (Exception exc)
\r
1579 Log.ErrorFormat("[BLOCK POST] FAIL with \r{0}", exc);
\r
1585 public async Task<TreeHash> GetHashMap(string account, Uri container, Uri objectName)
\r
1587 if (container == null)
\r
1588 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1589 if (container.IsAbsoluteUri)
\r
1590 throw new ArgumentException("The container must be relative","container");
\r
1591 if (objectName == null)
\r
1592 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1593 if (objectName.IsAbsoluteUri)
\r
1594 throw new ArgumentException("The objectName must be relative","objectName");
\r
1595 if (String.IsNullOrWhiteSpace(Token))
\r
1596 throw new InvalidOperationException("Invalid Token");
\r
1597 if (StorageUrl == null)
\r
1598 throw new InvalidOperationException("Invalid Storage Url");
\r
1599 Contract.EndContractBlock();
\r
1604 var objectUri = GetTargetUri(account).Combine(container).Combine(objectName);
\r
1605 var targetUri = new Uri(String.Format("{0}?format=json&hashmap", objectUri));
\r
1607 //Start downloading the object asynchronously
\r
1608 var json = await GetStringAsync(targetUri, "").ConfigureAwait(false);
\r
1609 var treeHash = TreeHash.Parse(json);
\r
1610 Log.InfoFormat("[GET HASH] END {0}", objectName);
\r
1614 catch (Exception exc)
\r
1616 Log.ErrorFormat("[GET HASH] END {0} with {1}", objectName, exc);
\r
1626 /// <param name="account"></param>
\r
1627 /// <param name="container"></param>
\r
1628 /// <param name="objectName"></param>
\r
1629 /// <param name="fileName"></param>
\r
1630 /// <param name="hash">Optional hash value for the file. If no hash is provided, the method calculates a new hash</param>
\r
1631 /// <param name="contentType"> </param>
\r
1632 /// <remarks>>This method should have no timeout or a very long one</remarks>
\r
1633 public async Task PutObject(string account, Uri container, Uri objectName, string fileName, string hash = Signature.MERKLE_EMPTY, string contentType = "application/octet-stream")
\r
1635 if (container == null)
\r
1636 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1637 if (container.IsAbsoluteUri)
\r
1638 throw new ArgumentException("The container must be relative","container");
\r
1639 if (objectName == null)
\r
1640 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1641 if (objectName.IsAbsoluteUri)
\r
1642 throw new ArgumentException("The objectName must be relative","objectName");
\r
1643 if (String.IsNullOrWhiteSpace(fileName))
\r
1644 throw new ArgumentNullException("fileName", "The fileName property can't be empty");
\r
1648 using (var client = new RestClient(_baseClient) { Timeout = 0 })
\r
1650 if (!String.IsNullOrWhiteSpace(account))
\r
1651 client.BaseAddress = GetAccountUrl(account);
\r
1653 var builder = client.GetAddressBuilder(container, objectName);
\r
1654 var uri = builder.Uri;
\r
1656 string etag = hash ;
\r
1658 client.Headers.Add("Content-Type", contentType);
\r
1659 if (contentType!=ObjectInfo.CONTENT_TYPE_DIRECTORY)
\r
1660 client.Headers.Add("ETag", etag);
\r
1663 Log.InfoFormat("[PUT] START {0}", objectName);
\r
1664 client.UploadProgressChanged += (sender, args) =>
\r
1666 using (ThreadContext.Stacks["PUT"].Push("Progress"))
\r
1668 Log.InfoFormat("{0} {1}% {2} of {3}", fileName,
\r
1669 args.ProgressPercentage,
\r
1670 args.BytesSent, args.TotalBytesToSend);
\r
1674 client.UploadFileCompleted += (sender, args) =>
\r
1676 using (ThreadContext.Stacks["PUT"].Push("Progress"))
\r
1678 Log.InfoFormat("Completed {0}", fileName);
\r
1682 if (contentType==ObjectInfo.CONTENT_TYPE_DIRECTORY)
\r
1683 await client.UploadDataTaskAsync(uri, "PUT", new byte[0]).ConfigureAwait(false);
\r
1685 await client.UploadFileTaskAsync(uri, "PUT", fileName).ConfigureAwait(false);
\r
1688 Log.InfoFormat("[PUT] END {0}", objectName);
\r
1690 catch (Exception exc)
\r
1692 Log.ErrorFormat("[PUT] END {0} with {1}", objectName, exc);
\r
1698 public void MoveObject(string account, Uri sourceContainer, Uri oldObjectName, Uri targetContainer, Uri newObjectName)
\r
1700 if (sourceContainer == null)
\r
1701 throw new ArgumentNullException("sourceContainer", "The sourceContainer property can't be empty");
\r
1702 if (sourceContainer.IsAbsoluteUri)
\r
1703 throw new ArgumentException("The sourceContainer must be relative","sourceContainer");
\r
1704 if (oldObjectName == null)
\r
1705 throw new ArgumentNullException("oldObjectName", "The oldObjectName property can't be empty");
\r
1706 if (oldObjectName.IsAbsoluteUri)
\r
1707 throw new ArgumentException("The oldObjectName must be relative","oldObjectName");
\r
1708 if (targetContainer == null)
\r
1709 throw new ArgumentNullException("targetContainer", "The targetContainer property can't be empty");
\r
1710 if (targetContainer.IsAbsoluteUri)
\r
1711 throw new ArgumentException("The targetContainer must be relative","targetContainer");
\r
1712 if (newObjectName == null)
\r
1713 throw new ArgumentNullException("newObjectName", "The newObjectName property can't be empty");
\r
1714 if (newObjectName.IsAbsoluteUri)
\r
1715 throw new ArgumentException("The newObjectName must be relative","newObjectName");
\r
1716 Contract.EndContractBlock();
\r
1718 var baseUri = GetTargetUri(account);
\r
1719 var targetUri = baseUri.Combine(targetContainer).Combine(newObjectName);
\r
1720 var sourceUri = new Uri(String.Format("/{0}/{1}", sourceContainer, oldObjectName),UriKind.Relative);
\r
1722 var message = new HttpRequestMessage(HttpMethod.Put, targetUri);
\r
1723 message.Headers.Add("X-Move-From", sourceUri.ToString());
\r
1724 using (var response = _baseHttpClient.SendAsyncWithRetries(message, 3).Result)
\r
1726 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
\r
1727 if (!expectedCodes.Contains(response.StatusCode))
\r
1728 throw CreateWebException("MoveObject", response.StatusCode);
\r
1732 public void DeleteObject(string account, Uri sourceContainer, Uri objectName, bool isDirectory)
\r
1734 if (sourceContainer == null)
\r
1735 throw new ArgumentNullException("sourceContainer", "The sourceContainer property can't be empty");
\r
1736 if (sourceContainer.IsAbsoluteUri)
\r
1737 throw new ArgumentException("The sourceContainer must be relative","sourceContainer");
\r
1738 if (objectName == null)
\r
1739 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1740 if (objectName.IsAbsoluteUri)
\r
1741 throw new ArgumentException("The objectName must be relative","objectName");
\r
1742 Contract.EndContractBlock();
\r
1744 var targetUrl = FolderConstants.TrashContainer + "/" + objectName;
\r
1747 targetUrl = targetUrl + "?delimiter=/";
\r
1750 var sourceUrl = String.Format("/{0}/{1}", sourceContainer, objectName);
\r
1752 using (var client = new RestClient(_baseClient))
\r
1754 if (!String.IsNullOrWhiteSpace(account))
\r
1755 client.BaseAddress = GetAccountUrl(account);
\r
1757 client.Headers.Add("X-Move-From", sourceUrl);
\r
1758 client.AllowedStatusCodes.Add(HttpStatusCode.NotFound);
\r
1759 Log.InfoFormat("[TRASH] [{0}] to [{1}]",sourceUrl,targetUrl);
\r
1760 client.PutWithRetry(new Uri(targetUrl,UriKind.Relative), 3);
\r
1762 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created,HttpStatusCode.NotFound};
\r
1763 if (!expectedCodes.Contains(client.StatusCode))
\r
1764 throw CreateWebException("DeleteObject", client.StatusCode);
\r
1769 private static WebException CreateWebException(string operation, HttpStatusCode statusCode)
\r
1771 return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode));
\r
1775 public bool CanUpload(string account, ObjectInfo cloudFile)
\r
1777 Contract.Requires(!String.IsNullOrWhiteSpace(account));
\r
1778 Contract.Requires(cloudFile!=null);
\r
1780 using (var client = new RestClient(_baseClient))
\r
1782 if (!String.IsNullOrWhiteSpace(account))
\r
1783 client.BaseAddress = GetAccountUrl(account);
\r
1786 var parts = cloudFile.Name.ToString().Split('/');
\r
1787 var folder = String.Join("/", parts,0,parts.Length-1);
\r
1789 var fileName = String.Format("{0}/{1}.pithos.ignore", folder, Guid.NewGuid());
\r
1790 var fileUri=fileName.ToEscapedUri();
\r
1792 client.Parameters.Clear();
\r
1795 var relativeUri = cloudFile.Container.Combine(fileUri);
\r
1796 client.PutWithRetry(relativeUri, 3, @"application/octet-stream");
\r
1798 var expectedCodes = new[] { HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
\r
1799 var result=(expectedCodes.Contains(client.StatusCode));
\r
1800 DeleteObject(account, cloudFile.Container, fileUri, cloudFile.IsDirectory);
\r
1810 ~CloudFilesClient()
\r
1815 public void Dispose()
\r
1818 GC.SuppressFinalize(this);
\r
1821 protected virtual void Dispose(bool disposing)
\r
1825 if (_httpClientHandler!=null)
\r
1826 _httpClientHandler.Dispose();
\r
1827 if (_baseClient!=null)
\r
1828 _baseClient.Dispose();
\r
1829 if(_baseHttpClient!=null)
\r
1830 _baseHttpClient.Dispose();
\r
1831 if (_baseHttpClientNoTimeout!=null)
\r
1832 _baseHttpClientNoTimeout.Dispose();
\r
1834 _httpClientHandler = null;
\r
1835 _baseClient = null;
\r
1836 _baseHttpClient = null;
\r
1837 _baseHttpClientNoTimeout = null;
\r
1841 public class ShareAccountInfo
\r
1843 public DateTime? last_modified { get; set; }
\r
1844 public string name { get; set; }
\r