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
121 /* private WebProxy _proxy;
\r
122 public WebProxy Proxy
\r
124 get { return _proxy; }
\r
128 if (_baseClient != null)
\r
129 _baseClient.Proxy = value;
\r
134 /* private Uri _proxy;
\r
137 get { return _proxy; }
\r
141 if (_baseClient != null)
\r
142 _baseClient.Proxy = new WebProxy(value);
\r
146 public double DownloadPercentLimit { get; set; }
\r
147 public double UploadPercentLimit { get; set; }
\r
149 public string AuthenticationUrl { get; set; }
\r
152 public string VersionPath
\r
154 get { return UsePithos ? "v1" : "v1.0"; }
\r
157 public bool UsePithos { get; set; }
\r
161 public CloudFilesClient(string userName, string apiKey)
\r
163 UserName = userName;
\r
167 public CloudFilesClient(AccountInfo accountInfo)
\r
169 if (accountInfo==null)
\r
170 throw new ArgumentNullException("accountInfo");
\r
171 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
\r
172 Contract.Ensures(StorageUrl != null);
\r
173 Contract.Ensures(_baseClient != null);
\r
174 Contract.Ensures(RootAddressUri != null);
\r
175 Contract.EndContractBlock();
\r
177 _baseClient = new RestClient
\r
179 BaseAddress = accountInfo.StorageUri.ToString(),
\r
183 StorageUrl = accountInfo.StorageUri;
\r
184 Token = accountInfo.Token;
\r
185 UserName = accountInfo.UserName;
\r
187 //Get the root address (StorageUrl without the account)
\r
188 var storageUrl = StorageUrl.AbsoluteUri;
\r
189 var usernameIndex = storageUrl.LastIndexOf(UserName);
\r
190 var rootUrl = storageUrl.Substring(0, usernameIndex);
\r
191 RootAddressUri = new Uri(rootUrl);
\r
193 var httpClientHandler = new HttpClientHandler
\r
195 AllowAutoRedirect = true,
\r
196 AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
\r
201 _baseHttpClient = new HttpClient(httpClientHandler)
\r
203 BaseAddress = StorageUrl,
\r
204 Timeout = TimeSpan.FromSeconds(30)
\r
206 _baseHttpClient.DefaultRequestHeaders.Add(TOKEN_HEADER, Token);
\r
208 _baseHttpClientNoTimeout = new HttpClient(httpClientHandler)
\r
210 BaseAddress = StorageUrl,
\r
211 Timeout = TimeSpan.FromMilliseconds(-1)
\r
213 _baseHttpClientNoTimeout.DefaultRequestHeaders.Add(TOKEN_HEADER, Token);
\r
219 private static void AssertStatusOK(HttpResponseMessage response, string message)
\r
221 var statusCode = response.StatusCode;
\r
222 if (statusCode >= HttpStatusCode.BadRequest)
\r
223 throw new WebException(String.Format("{0} with code {1} - {2}", message, statusCode, response.ReasonPhrase));
\r
226 public async Task<AccountInfo> Authenticate()
\r
228 if (String.IsNullOrWhiteSpace(UserName))
\r
229 throw new InvalidOperationException("UserName is empty");
\r
230 if (String.IsNullOrWhiteSpace(ApiKey))
\r
231 throw new InvalidOperationException("ApiKey is empty");
\r
232 if (String.IsNullOrWhiteSpace(AuthenticationUrl))
\r
233 throw new InvalidOperationException("AuthenticationUrl is empty");
\r
234 Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
\r
235 Contract.Ensures(StorageUrl != null);
\r
236 Contract.Ensures(_baseClient != null);
\r
237 Contract.Ensures(RootAddressUri != null);
\r
238 Contract.EndContractBlock();
\r
241 Log.InfoFormat("[AUTHENTICATE] Start for {0}", UserName);
\r
243 var groups = new List<Group>();
\r
245 using (var authClient = new HttpClient(_httpClientHandler,false){ BaseAddress = new Uri(AuthenticationUrl),Timeout=TimeSpan.FromSeconds(30) })
\r
246 //using (var authClient = new RestClient{BaseAddress=AuthenticationUrl})
\r
248 /* if (Proxy != null)
\r
249 authClient.Proxy = Proxy;*/
\r
251 //Contract.Assume(authClient.DefaultRequestHeaders != null);
\r
253 authClient.DefaultRequestHeaders.Add("X-Auth-User", UserName);
\r
254 authClient.DefaultRequestHeaders.Add("X-Auth-Key", ApiKey);
\r
256 //Func<Task<HttpResponseMessage>> call = () => ;
\r
260 using (var response = await authClient.GetAsyncWithRetries(new Uri(VersionPath, UriKind.Relative),3).ConfigureAwait(false)) // .DownloadStringWithRetryRelative(new Uri(VersionPath, UriKind.Relative), 3);
\r
262 AssertStatusOK(response,"Authentication failed");
\r
264 storageUrl = response.Headers.GetValues("X-Storage-Url").First();
\r
265 if (String.IsNullOrWhiteSpace(storageUrl))
\r
266 throw new InvalidOperationException("Failed to obtain storage url");
\r
268 token = response.Headers.GetValues(TOKEN_HEADER).First();
\r
269 if (String.IsNullOrWhiteSpace(token))
\r
270 throw new InvalidOperationException("Failed to obtain token url");
\r
275 _baseClient = new RestClient
\r
277 BaseAddress = storageUrl,
\r
283 StorageUrl = new Uri(storageUrl);
\r
289 //Get the root address (StorageUrl without the account)
\r
290 var usernameIndex=storageUrl.LastIndexOf(UserName);
\r
291 var rootUrl = storageUrl.Substring(0, usernameIndex);
\r
292 RootAddressUri = new Uri(rootUrl);
\r
295 _baseHttpClient = new HttpClient(_httpClientHandler,false)
\r
297 BaseAddress = StorageUrl,
\r
298 Timeout = TimeSpan.FromSeconds(30)
\r
300 _baseHttpClient.DefaultRequestHeaders.Add(TOKEN_HEADER, token);
\r
302 _baseHttpClientNoTimeout = new HttpClient(_httpClientHandler,false)
\r
304 BaseAddress = StorageUrl,
\r
305 Timeout = TimeSpan.FromMilliseconds(-1)
\r
307 _baseHttpClientNoTimeout.DefaultRequestHeaders.Add(TOKEN_HEADER, token);
\r
309 /* var keys = authClient.ResponseHeaders.AllKeys.AsQueryable();
\r
310 groups = (from key in keys
\r
311 where key.StartsWith("X-Account-Group-")
\r
312 let name = key.Substring(16)
\r
313 select new Group(name, authClient.ResponseHeaders[key]))
\r
319 Log.InfoFormat("[AUTHENTICATE] End for {0}", UserName);
\r
320 Debug.Assert(_baseClient!=null);
\r
322 return new AccountInfo {StorageUri = StorageUrl, Token = Token, UserName = UserName,Groups=groups};
\r
326 private static void TraceStart(string method, Uri actualAddress)
\r
328 Log.InfoFormat("[{0}] {1} {2}", method, DateTime.Now, actualAddress);
\r
331 private async Task<string> GetStringAsync(Uri targetUri, string errorMessage,DateTime? since=null)
\r
333 TraceStart("GET",targetUri);
\r
334 var request = new HttpRequestMessage(HttpMethod.Get, targetUri);
\r
335 if (since.HasValue)
\r
337 request.Headers.IfModifiedSince = since.Value;
\r
339 //Func<Task<HttpResponseMessage>> call = () => _baseHttpClient.SendAsync(request);
\r
340 using (var response = await _baseHttpClient.SendAsyncWithRetries(request,3).ConfigureAwait(false))
\r
342 AssertStatusOK(response, errorMessage);
\r
344 if (response.StatusCode == HttpStatusCode.NoContent)
\r
345 return String.Empty;
\r
347 var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
\r
352 public async Task<IList<ContainerInfo>> ListContainers(string account)
\r
355 var targetUrl = GetTargetUrl(account);
\r
356 var targetUri = new Uri(String.Format("{0}?format=json", targetUrl));
\r
357 var result = await GetStringAsync(targetUri, "List Containers failed").ConfigureAwait(false);
\r
358 if (String.IsNullOrWhiteSpace(result))
\r
359 return new List<ContainerInfo>();
\r
360 var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(result);
\r
361 foreach (var info in infos)
\r
363 info.Account = account;
\r
368 //public IList<ContainerInfo> ListContainers(string account)
\r
371 // var targetUrl = GetTargetUrl(account);
\r
372 // var targetUri=new Uri(String.Format("{0}?format=json",targetUrl));
\r
373 // var result = GetStringAsync(targetUri, "List Containers failed").Result;
\r
374 // if (String.IsNullOrWhiteSpace(result))
\r
375 // return new List<ContainerInfo>();
\r
376 // var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(result);
\r
377 // foreach (var info in infos)
\r
379 // info.Account = account;
\r
382 // /* using (var client = new RestClient(_baseClient))
\r
384 // if (!String.IsNullOrWhiteSpace(account))
\r
385 // client.BaseAddress = GetAccountUrl(account);
\r
387 // client.Parameters.Clear();
\r
388 // client.Parameters.Add("format", "json");
\r
389 // var content = client.DownloadStringWithRetryRelative(_emptyUri, 3);
\r
390 // client.AssertStatusOK("List Containers failed");
\r
392 // if (client.StatusCode == HttpStatusCode.NoContent)
\r
393 // return new List<ContainerInfo>();
\r
394 // var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(content);
\r
396 // foreach (var info in infos)
\r
398 // info.Account = account;
\r
405 private string GetAccountUrl(string account)
\r
407 return RootAddressUri.Combine(account).AbsoluteUri;
\r
410 public IList<ShareAccountInfo> ListSharingAccounts(DateTime? since=null)
\r
412 using (ThreadContext.Stacks["Share"].Push("List Accounts"))
\r
414 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
416 var targetUri = new Uri(String.Format("{0}?format=json", RootAddressUri), UriKind.Absolute);
\r
417 var content=TaskEx.Run(async ()=>await GetStringAsync(targetUri, "ListSharingAccounts failed", since).ConfigureAwait(false)).Result;
\r
419 //If the result is empty, return an empty list,
\r
420 var infos = String.IsNullOrWhiteSpace(content)
\r
421 ? new List<ShareAccountInfo>()
\r
422 //Otherwise deserialize the account list into a list of ShareAccountInfos
\r
423 : JsonConvert.DeserializeObject<IList<ShareAccountInfo>>(content);
\r
425 Log.DebugFormat("END");
\r
428 using (var client = new RestClient(_baseClient))
\r
430 client.Parameters.Clear();
\r
431 client.Parameters.Add("format", "json");
\r
432 client.IfModifiedSince = since;
\r
434 //Extract the username from the base address
\r
435 client.BaseAddress = RootAddressUri.AbsoluteUri;
\r
437 var content = client.DownloadStringWithRetryRelative(_emptyUri, 3);
\r
439 client.AssertStatusOK("ListSharingAccounts failed");
\r
441 //If the result is empty, return an empty list,
\r
442 var infos = String.IsNullOrWhiteSpace(content)
\r
443 ? new List<ShareAccountInfo>()
\r
444 //Otherwise deserialize the account list into a list of ShareAccountInfos
\r
445 : JsonConvert.DeserializeObject<IList<ShareAccountInfo>>(content);
\r
447 Log.DebugFormat("END");
\r
456 /// Request listing of all objects in a container modified since a specific time.
\r
457 /// If the *since* value is missing, return all objects
\r
459 /// <param name="knownContainers">Use the since variable only for the containers listed in knownContainers. Unknown containers are considered new
\r
460 /// and should be polled anyway
\r
462 /// <param name="since"></param>
\r
463 /// <returns></returns>
\r
464 public IList<ObjectInfo> ListSharedObjects(HashSet<string> knownContainers,DateTime? since = null )
\r
467 using (ThreadContext.Stacks["Share"].Push("List Objects"))
\r
469 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
470 //'since' is not used here because we need to have ListObjects return a NoChange result
\r
471 //for all shared accounts,containers
\r
473 Func<ContainerInfo, string> getKey = c => String.Format("{0}\\{1}", c.Account, c.Name);
\r
475 var containers = (from account in ListSharingAccounts()
\r
476 let conts = ListContainers(account.name).Result
\r
477 from container in conts
\r
478 select container).ToList();
\r
479 var items = from container in containers
\r
480 let actualSince=knownContainers.Contains(getKey(container))?since:null
\r
481 select ListObjects(container.Account , container.Name, actualSince);
\r
482 var objects=items.SelectMany(r=> r).ToList();
\r
485 //Check parents recursively up to (but not including) the container.
\r
486 //If parents are missing, add them to the list
\r
487 //Need function to calculate all parent URLs
\r
488 objects = AddMissingParents(objects);
\r
490 //Store any new containers
\r
491 foreach (var container in containers)
\r
493 knownContainers.Add(getKey(container));
\r
498 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
503 private List<ObjectInfo> AddMissingParents(List<ObjectInfo> objects)
\r
505 //TODO: Remove short-circuit when we decide to use Missing Parents functionality
\r
508 var existingUris = objects.ToDictionary(o => o.Uri, o => o);
\r
509 foreach (var objectInfo in objects)
\r
511 //Can be null when retrieving objects to show in selective sync
\r
512 if (objectInfo.Name == null)
\r
515 //No need to unescape here, the parts will be used to create new ObjectInfos
\r
516 var parts = objectInfo.Name.ToString().Split(new[]{'/'},StringSplitOptions.RemoveEmptyEntries);
\r
517 //If there is no parent, skip
\r
518 if (parts.Length == 1)
\r
520 var baseParts = new[]
\r
522 objectInfo.Uri.Host, objectInfo.Uri.Segments[1].TrimEnd('/'),objectInfo.Account,objectInfo.Container.ToString()
\r
524 for (var partIdx = 0; partIdx < parts.Length - 1; partIdx++)
\r
526 var nameparts = parts.Range(0, partIdx).ToArray();
\r
527 var parentName= String.Join("/", nameparts);
\r
529 var parentParts = baseParts.Concat(nameparts);
\r
530 var parentUrl = objectInfo.Uri.Scheme+ "://" + String.Join("/", parentParts);
\r
532 var parentUri = new Uri(parentUrl, UriKind.Absolute);
\r
534 ObjectInfo existingInfo;
\r
535 if (!existingUris.TryGetValue(parentUri,out existingInfo))
\r
537 var h = parentUrl.GetHashCode();
\r
538 var reverse = new string(parentUrl.Reverse().ToArray());
\r
539 var rh = reverse.GetHashCode();
\r
540 var b1 = BitConverter.GetBytes(h);
\r
541 var b2 = BitConverter.GetBytes(rh);
\r
542 var g = new Guid(0,0,0,b1.Concat(b2).ToArray());
\r
545 existingUris[parentUri] = new ObjectInfo
\r
547 Account = objectInfo.Account,
\r
548 Container = objectInfo.Container,
\r
549 Content_Type = ObjectInfo.CONTENT_TYPE_DIRECTORY,
\r
550 ETag = Signature.MERKLE_EMPTY,
\r
551 X_Object_Hash = Signature.MERKLE_EMPTY,
\r
552 Name=new Uri(parentName,UriKind.Relative),
\r
553 StorageUri=objectInfo.StorageUri,
\r
555 UUID=g.ToString(),
\r
560 return existingUris.Values.ToList();
\r
563 public void SetTags(ObjectInfo target,IDictionary<string,string> tags)
\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 (target == null)
\r
570 throw new ArgumentNullException("target");
\r
571 Contract.EndContractBlock();
\r
573 using (ThreadContext.Stacks["Share"].Push("Share Object"))
\r
575 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
577 using (var client = new RestClient(_baseClient))
\r
580 client.BaseAddress = GetAccountUrl(target.Account);
\r
582 client.Parameters.Clear();
\r
583 client.Parameters.Add("update", "");
\r
585 foreach (var tag in tags)
\r
587 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
\r
588 client.Headers.Add(headerTag, tag.Value);
\r
591 client.DownloadStringWithRetryRelative(target.Container, 3);
\r
594 client.AssertStatusOK("SetTags failed");
\r
595 //If the status is NOT ACCEPTED we have a problem
\r
596 if (client.StatusCode != HttpStatusCode.Accepted)
\r
598 Log.Error("Failed to set tags");
\r
599 throw new Exception("Failed to set tags");
\r
602 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
609 public void ShareObject(string account, Uri container, Uri objectName, string shareTo, bool read, bool write)
\r
611 if (String.IsNullOrWhiteSpace(Token))
\r
612 throw new InvalidOperationException("The Token is not set");
\r
613 if (StorageUrl==null)
\r
614 throw new InvalidOperationException("The StorageUrl is not set");
\r
615 if (container==null)
\r
616 throw new ArgumentNullException("container");
\r
617 if (container.IsAbsoluteUri)
\r
618 throw new ArgumentException("container");
\r
619 if (objectName==null)
\r
620 throw new ArgumentNullException("objectName");
\r
621 if (objectName.IsAbsoluteUri)
\r
622 throw new ArgumentException("objectName");
\r
623 if (String.IsNullOrWhiteSpace(account))
\r
624 throw new ArgumentNullException("account");
\r
625 if (String.IsNullOrWhiteSpace(shareTo))
\r
626 throw new ArgumentNullException("shareTo");
\r
627 Contract.EndContractBlock();
\r
629 using (ThreadContext.Stacks["Share"].Push("Share Object"))
\r
631 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
633 using (var client = new RestClient(_baseClient))
\r
636 client.BaseAddress = GetAccountUrl(account);
\r
638 client.Parameters.Clear();
\r
639 client.Parameters.Add("format", "json");
\r
641 string permission = "";
\r
643 permission = String.Format("write={0}", shareTo);
\r
645 permission = String.Format("read={0}", shareTo);
\r
646 client.Headers.Add("X-Object-Sharing", permission);
\r
648 var content = client.DownloadStringWithRetryRelative(container, 3);
\r
650 client.AssertStatusOK("ShareObject failed");
\r
652 //If the result is empty, return an empty list,
\r
653 var infos = String.IsNullOrWhiteSpace(content)
\r
654 ? new List<ObjectInfo>()
\r
655 //Otherwise deserialize the object list into a list of ObjectInfos
\r
656 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
658 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
665 public AccountInfo GetAccountPolicies(AccountInfo accountInfo)
\r
667 if (accountInfo==null)
\r
668 throw new ArgumentNullException("accountInfo");
\r
669 Contract.EndContractBlock();
\r
671 using (ThreadContext.Stacks["Account"].Push("GetPolicies"))
\r
673 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
675 if (_baseClient == null)
\r
677 _baseClient = new RestClient
\r
679 BaseAddress = accountInfo.StorageUri.ToString(),
\r
685 using (var client = new RestClient(_baseClient))
\r
687 if (!String.IsNullOrWhiteSpace(accountInfo.UserName))
\r
688 client.BaseAddress = GetAccountUrl(accountInfo.UserName);
\r
690 client.Parameters.Clear();
\r
691 client.Parameters.Add("format", "json");
\r
692 client.Head(_emptyUri, 3);
\r
694 var quotaValue=client.ResponseHeaders["X-Account-Policy-Quota"];
\r
695 var bytesValue= client.ResponseHeaders["X-Account-Bytes-Used"];
\r
698 if (long.TryParse(quotaValue, out quota))
\r
699 accountInfo.Quota = quota;
\r
700 if (long.TryParse(bytesValue, out bytes))
\r
701 accountInfo.BytesUsed = bytes;
\r
703 return accountInfo;
\r
710 public void UpdateMetadata(ObjectInfo objectInfo)
\r
712 if (objectInfo == null)
\r
713 throw new ArgumentNullException("objectInfo");
\r
714 Contract.EndContractBlock();
\r
716 using (ThreadContext.Stacks["Objects"].Push("UpdateMetadata"))
\r
718 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
721 using(var client=new RestClient(_baseClient))
\r
724 client.BaseAddress = GetAccountUrl(objectInfo.Account);
\r
726 client.Parameters.Clear();
\r
730 foreach (var tag in objectInfo.Tags)
\r
732 var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
\r
733 client.Headers.Add(headerTag, tag.Value);
\r
738 var permissions=objectInfo.GetPermissionString();
\r
739 client.SetNonEmptyHeaderValue("X-Object-Sharing",permissions);
\r
741 client.SetNonEmptyHeaderValue("Content-Disposition",objectInfo.ContendDisposition);
\r
742 client.SetNonEmptyHeaderValue("Content-Encoding",objectInfo.ContentEncoding);
\r
743 client.SetNonEmptyHeaderValue("X-Object-Manifest",objectInfo.Manifest);
\r
744 var isPublic = objectInfo.IsPublic.ToString().ToLower();
\r
745 client.Headers.Add("X-Object-Public", isPublic);
\r
748 /*var uriBuilder = client.GetAddressBuilder(objectInfo.Container, objectInfo.Name);
\r
749 uriBuilder.Query = "update=";
\r
750 var uri = uriBuilder.Uri.MakeRelativeUri(this.RootAddressUri);*/
\r
751 var address = String.Format("{0}/{1}?update=",objectInfo.Container, objectInfo.Name);
\r
752 client.PostWithRetry(new Uri(address,UriKind.Relative),"application/xml");
\r
754 //client.UploadValues(uri,new NameValueCollection());
\r
757 client.AssertStatusOK("UpdateMetadata failed");
\r
758 //If the status is NOT ACCEPTED or OK we have a problem
\r
759 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
\r
761 Log.Error("Failed to update metadata");
\r
762 throw new Exception("Failed to update metadata");
\r
765 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
771 public void UpdateMetadata(ContainerInfo containerInfo)
\r
773 if (containerInfo == null)
\r
774 throw new ArgumentNullException("containerInfo");
\r
775 Contract.EndContractBlock();
\r
777 using (ThreadContext.Stacks["Containers"].Push("UpdateMetadata"))
\r
779 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
782 using(var client=new RestClient(_baseClient))
\r
785 client.BaseAddress = GetAccountUrl(containerInfo.Account);
\r
787 client.Parameters.Clear();
\r
791 foreach (var tag in containerInfo.Tags)
\r
793 var headerTag = String.Format("X-Container-Meta-{0}", tag.Key);
\r
794 client.Headers.Add(headerTag, tag.Value);
\r
799 foreach (var policy in containerInfo.Policies)
\r
801 var headerPolicy = String.Format("X-Container-Policy-{0}", policy.Key);
\r
802 client.Headers.Add(headerPolicy, policy.Value);
\r
806 var uriBuilder = client.GetAddressBuilder(containerInfo.Name,_emptyUri);
\r
807 var uri = uriBuilder.Uri;
\r
809 client.UploadValues(uri,new NameValueCollection());
\r
812 client.AssertStatusOK("UpdateMetadata failed");
\r
813 //If the status is NOT ACCEPTED or OK we have a problem
\r
814 if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
\r
816 Log.Error("Failed to update metadata");
\r
817 throw new Exception("Failed to update metadata");
\r
820 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
829 public IList<ObjectInfo> ListObjects(string account, Uri container, DateTime? since = null)
\r
831 if (container==null)
\r
832 throw new ArgumentNullException("container");
\r
833 if (container.IsAbsoluteUri)
\r
834 throw new ArgumentException("container");
\r
835 Contract.EndContractBlock();
\r
837 using (ThreadContext.Stacks["Objects"].Push("List"))
\r
839 /*if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
841 var targetUrl = String.IsNullOrWhiteSpace(account)
\r
842 ? _baseHttpClient.BaseAddress.ToString()
\r
843 : GetAccountUrl(account);
\r
844 var targetUri = new Uri(String.Format("{0}?format=json", targetUrl));
\r
845 var result = GetString(targetUri, "ListObjects failed",since).Result;
\r
847 if (String.IsNullOrWhiteSpace(result))
\r
848 return new[]{new NoModificationInfo(account,container)};
\r
849 //If the result is empty, return an empty list,
\r
850 var infos = String.IsNullOrWhiteSpace(result)
\r
851 ? new List<ObjectInfo>()
\r
852 //Otherwise deserialize the object list into a list of ObjectInfos
\r
853 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(result);
\r
855 foreach (var info in infos)
\r
857 info.Container = container;
\r
858 info.Account = account;
\r
859 info.StorageUri = StorageUrl;
\r
861 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
864 var containerUri = GetTargetUri(account).Combine(container);
\r
865 var targetUri = new Uri(String.Format("{0}?format=json", containerUri), UriKind.Absolute);
\r
867 var content =TaskEx.Run(async ()=>await GetStringAsync(targetUri, "ListObjects failed", since).ConfigureAwait(false)).Result;
\r
869 //304 will result in an empty string. Empty containers return an empty json array
\r
870 if (String.IsNullOrWhiteSpace(content))
\r
871 return new[] {new NoModificationInfo(account, container)};
\r
873 //If the result is empty, return an empty list,
\r
874 var infos = String.IsNullOrWhiteSpace(content)
\r
875 ? new List<ObjectInfo>()
\r
876 //Otherwise deserialize the object list into a list of ObjectInfos
\r
877 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
879 foreach (var info in infos)
\r
881 info.Container = container;
\r
882 info.Account = account;
\r
883 info.StorageUri = StorageUrl;
\r
885 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
888 using (var client = new RestClient(_baseClient))
\r
890 if (!String.IsNullOrWhiteSpace(account))
\r
891 client.BaseAddress = GetAccountUrl(account);
\r
893 client.Parameters.Clear();
\r
894 client.Parameters.Add("format", "json");
\r
895 client.IfModifiedSince = since;
\r
896 var content = client.DownloadStringWithRetryRelative(container, 3);
\r
898 client.AssertStatusOK("ListObjects failed");
\r
900 if (client.StatusCode == HttpStatusCode.NotModified)
\r
901 return new[] {new NoModificationInfo(account, container)};
\r
902 //If the result is empty, return an empty list,
\r
903 var infos = String.IsNullOrWhiteSpace(content)
\r
904 ? new List<ObjectInfo>()
\r
905 //Otherwise deserialize the object list into a list of ObjectInfos
\r
906 : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
908 foreach (var info in infos)
\r
910 info.Container = container;
\r
911 info.Account = account;
\r
912 info.StorageUri = StorageUrl;
\r
914 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
921 public IList<ObjectInfo> ListObjects(string account, Uri container, Uri folder, DateTime? since = null)
\r
923 if (container==null)
\r
924 throw new ArgumentNullException("container");
\r
925 if (container.IsAbsoluteUri)
\r
926 throw new ArgumentException("container");
\r
928 if (String.IsNullOrWhiteSpace(folder))
\r
929 throw new ArgumentNullException("folder");
\r
931 Contract.EndContractBlock();
\r
933 using (ThreadContext.Stacks["Objects"].Push("List"))
\r
935 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
937 var containerUri = GetTargetUri(account).Combine(container);
\r
938 var targetUri = new Uri(String.Format("{0}?format=json&path={1}", containerUri,folder), UriKind.Absolute);
\r
939 var content = TaskEx.Run(async ()=>await GetStringAsync(targetUri, "ListObjects failed", since).ConfigureAwait(false)).Result;
\r
941 //304 will result in an empty string. Empty containers return an empty json array
\r
942 if (String.IsNullOrWhiteSpace(content))
\r
943 return new[] { new NoModificationInfo(account, container) };
\r
946 var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
947 foreach (var info in infos)
\r
949 info.Account = account;
\r
950 if (info.Container == null)
\r
951 info.Container = container;
\r
952 info.StorageUri = StorageUrl;
\r
954 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
957 using (var client = new RestClient(_baseClient))
\r
959 if (!String.IsNullOrWhiteSpace(account))
\r
960 client.BaseAddress = GetAccountUrl(account);
\r
962 client.Parameters.Clear();
\r
963 client.Parameters.Add("format", "json");
\r
964 client.Parameters.Add("path", folder.ToString());
\r
965 client.IfModifiedSince = since;
\r
966 var content = client.DownloadStringWithRetryRelative(container, 3);
\r
967 client.AssertStatusOK("ListObjects failed");
\r
969 if (client.StatusCode==HttpStatusCode.NotModified)
\r
970 return new[]{new NoModificationInfo(account,container,folder)};
\r
972 var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
\r
973 foreach (var info in infos)
\r
975 info.Account = account;
\r
976 if (info.Container == null)
\r
977 info.Container = container;
\r
978 info.StorageUri = StorageUrl;
\r
980 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
988 public bool ContainerExists(string account, Uri container)
\r
990 if (container==null)
\r
991 throw new ArgumentNullException("container", "The container property can't be empty");
\r
992 if (container.IsAbsoluteUri)
\r
993 throw new ArgumentException( "The container must be relative","container");
\r
994 Contract.EndContractBlock();
\r
996 using (ThreadContext.Stacks["Containters"].Push("Exists"))
\r
998 if (Log.IsDebugEnabled) Log.DebugFormat("START");
\r
1000 var targetUri = GetTargetUri(account).Combine(container);
\r
1002 using (var response = _baseHttpClient.HeadAsyncWithRetries(targetUri, 3).Result)
\r
1006 switch (response.StatusCode)
\r
1008 case HttpStatusCode.OK:
\r
1009 case HttpStatusCode.NoContent:
\r
1012 case HttpStatusCode.NotFound:
\r
1016 throw CreateWebException("ContainerExists", response.StatusCode);
\r
1018 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
1023 using (var client = new RestClient(_baseClient))
\r
1025 if (!String.IsNullOrWhiteSpace(account))
\r
1026 client.BaseAddress = GetAccountUrl(account);
\r
1028 client.Parameters.Clear();
\r
1029 client.Head(container, 3);
\r
1032 switch (client.StatusCode)
\r
1034 case HttpStatusCode.OK:
\r
1035 case HttpStatusCode.NoContent:
\r
1038 case HttpStatusCode.NotFound:
\r
1042 throw CreateWebException("ContainerExists", client.StatusCode);
\r
1044 if (Log.IsDebugEnabled) Log.DebugFormat("END");
\r
1053 private Uri GetTargetUri(string account)
\r
1055 return new Uri(GetTargetUrl(account),UriKind.Absolute);
\r
1058 private string GetTargetUrl(string account)
\r
1060 return String.IsNullOrWhiteSpace(account)
\r
1061 ? _baseHttpClient.BaseAddress.ToString()
\r
1062 : GetAccountUrl(account);
\r
1065 public bool ObjectExists(string account, Uri container, Uri objectName)
\r
1067 if (container == null)
\r
1068 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1069 if (container.IsAbsoluteUri)
\r
1070 throw new ArgumentException("The container must be relative","container");
\r
1071 if (objectName == null)
\r
1072 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1073 if (objectName.IsAbsoluteUri)
\r
1074 throw new ArgumentException("The objectName must be relative","objectName");
\r
1075 Contract.EndContractBlock();
\r
1077 var targetUri=GetTargetUri(account).Combine(container).Combine(objectName);
\r
1079 using (var response = _baseHttpClient.HeadAsyncWithRetries(targetUri, 3).Result)
\r
1081 switch (response.StatusCode)
\r
1083 case HttpStatusCode.OK:
\r
1084 case HttpStatusCode.NoContent:
\r
1086 case HttpStatusCode.NotFound:
\r
1089 throw CreateWebException("ObjectExists", response.StatusCode);
\r
1094 using (var client = new RestClient(_baseClient))
\r
1096 if (!String.IsNullOrWhiteSpace(account))
\r
1097 client.BaseAddress = GetAccountUrl(account);
\r
1099 client.Parameters.Clear();
\r
1100 client.Head(container.Combine(objectName), 3);
\r
1102 switch (client.StatusCode)
\r
1104 case HttpStatusCode.OK:
\r
1105 case HttpStatusCode.NoContent:
\r
1107 case HttpStatusCode.NotFound:
\r
1110 throw CreateWebException("ObjectExists", client.StatusCode);
\r
1117 public ObjectInfo GetObjectInfo(string account, Uri container, Uri objectName)
\r
1119 if (container == null)
\r
1120 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1121 if (container.IsAbsoluteUri)
\r
1122 throw new ArgumentException("The container must be relative","container");
\r
1123 if (objectName == null)
\r
1124 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1125 if (objectName.IsAbsoluteUri)
\r
1126 throw new ArgumentException("The objectName must be relative","objectName");
\r
1127 Contract.EndContractBlock();
\r
1129 using (ThreadContext.Stacks["Objects"].Push("GetObjectInfo"))
\r
1132 using (var client = new RestClient(_baseClient))
\r
1134 if (!String.IsNullOrWhiteSpace(account))
\r
1135 client.BaseAddress = GetAccountUrl(account);
\r
1138 client.Parameters.Clear();
\r
1140 client.Head(container.Combine(objectName), 3);
\r
1142 if (client.TimedOut)
\r
1143 return ObjectInfo.Empty;
\r
1145 switch (client.StatusCode)
\r
1147 case HttpStatusCode.OK:
\r
1148 case HttpStatusCode.NoContent:
\r
1149 var keys = client.ResponseHeaders.AllKeys.AsQueryable();
\r
1150 var tags = client.GetMeta("X-Object-Meta-");
\r
1151 var extensions = (from key in keys
\r
1152 where key.StartsWith("X-Object-") && !key.StartsWith("X-Object-Meta-")
\r
1153 select new {Name = key, Value = client.ResponseHeaders[key]})
\r
1154 .ToDictionary(t => t.Name, t => t.Value);
\r
1156 var permissions=client.GetHeaderValue("X-Object-Sharing", true);
\r
1159 var info = new ObjectInfo
\r
1161 Account = account,
\r
1162 Container = container,
\r
1163 Name = objectName,
\r
1164 ETag = client.GetHeaderValue("ETag"),
\r
1165 UUID=client.GetHeaderValue("X-Object-UUID"),
\r
1166 X_Object_Hash = client.GetHeaderValue("X-Object-Hash"),
\r
1167 Content_Type = client.GetHeaderValue("Content-Type"),
\r
1168 Bytes = Convert.ToInt64(client.GetHeaderValue("Content-Length",true)),
\r
1170 Last_Modified = client.LastModified,
\r
1171 Extensions = extensions,
\r
1172 ContentEncoding=client.GetHeaderValue("Content-Encoding",true),
\r
1173 ContendDisposition = client.GetHeaderValue("Content-Disposition",true),
\r
1174 Manifest=client.GetHeaderValue("X-Object-Manifest",true),
\r
1175 PublicUrl=client.GetHeaderValue("X-Object-Public",true),
\r
1176 StorageUri=StorageUrl,
\r
1178 info.SetPermissions(permissions);
\r
1180 case HttpStatusCode.NotFound:
\r
1181 return ObjectInfo.Empty;
\r
1183 throw new WebException(
\r
1184 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
\r
1185 objectName, client.StatusCode));
\r
1189 catch (RetryException)
\r
1191 Log.WarnFormat("[RETRY FAIL] GetObjectInfo for {0} failed.",objectName);
\r
1192 return ObjectInfo.Empty;
\r
1194 catch (WebException e)
\r
1197 String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
\r
1198 objectName, client.StatusCode), e);
\r
1208 public void CreateFolder(string account, Uri container, Uri folder)
\r
1210 if (container == null)
\r
1211 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1212 if (container.IsAbsoluteUri)
\r
1213 throw new ArgumentException("The container must be relative","container");
\r
1214 if (folder == null)
\r
1215 throw new ArgumentNullException("folder", "The objectName property can't be empty");
\r
1216 if (folder.IsAbsoluteUri)
\r
1217 throw new ArgumentException("The objectName must be relative","folder");
\r
1218 Contract.EndContractBlock();
\r
1220 var folderUri=container.Combine(folder);
\r
1221 var targetUri = GetTargetUri(account).Combine(folderUri);
\r
1222 var message = new HttpRequestMessage(HttpMethod.Put, targetUri);
\r
1224 message.Headers.Add("Content-Type", ObjectInfo.CONTENT_TYPE_DIRECTORY);
\r
1225 message.Headers.Add("Content-Length", "0");
\r
1226 using (var response = _baseHttpClient.SendAsyncWithRetries(message, 3).Result)
\r
1228 if (response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.Accepted)
\r
1229 throw CreateWebException("CreateFolder", response.StatusCode);
\r
1232 using (var client = new RestClient(_baseClient))
\r
1234 if (!String.IsNullOrWhiteSpace(account))
\r
1235 client.BaseAddress = GetAccountUrl(account);
\r
1237 client.Parameters.Clear();
\r
1238 client.Headers.Add("Content-Type", ObjectInfo.CONTENT_TYPE_DIRECTORY);
\r
1239 client.Headers.Add("Content-Length", "0");
\r
1240 client.PutWithRetry(folderUri, 3);
\r
1242 if (client.StatusCode != HttpStatusCode.Created && client.StatusCode != HttpStatusCode.Accepted)
\r
1243 throw CreateWebException("CreateFolder", client.StatusCode);
\r
1250 public ContainerInfo GetContainerInfo(string account, Uri container)
\r
1252 if (container == null)
\r
1253 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1254 if (container.IsAbsoluteUri)
\r
1255 throw new ArgumentException("The container must be relative","container");
\r
1256 Contract.EndContractBlock();
\r
1258 using (var client = new RestClient(_baseClient))
\r
1260 if (!String.IsNullOrWhiteSpace(account))
\r
1261 client.BaseAddress = GetAccountUrl(account);
\r
1263 client.Head(container);
\r
1264 switch (client.StatusCode)
\r
1266 case HttpStatusCode.OK:
\r
1267 case HttpStatusCode.NoContent:
\r
1268 var tags = client.GetMeta("X-Container-Meta-");
\r
1269 var policies = client.GetMeta("X-Container-Policy-");
\r
1271 var containerInfo = new ContainerInfo
\r
1275 StorageUrl=StorageUrl.ToString(),
\r
1277 long.Parse(client.GetHeaderValue("X-Container-Object-Count")),
\r
1278 Bytes = long.Parse(client.GetHeaderValue("X-Container-Bytes-Used")),
\r
1279 BlockHash = client.GetHeaderValue("X-Container-Block-Hash"),
\r
1280 BlockSize=int.Parse(client.GetHeaderValue("X-Container-Block-Size")),
\r
1281 Last_Modified=client.LastModified,
\r
1287 return containerInfo;
\r
1288 case HttpStatusCode.NotFound:
\r
1289 return ContainerInfo.Empty;
\r
1291 throw CreateWebException("GetContainerInfo", client.StatusCode);
\r
1296 public void CreateContainer(string account, Uri container)
\r
1298 if (container == null)
\r
1299 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1300 if (container.IsAbsoluteUri)
\r
1301 throw new ArgumentException("The container must be relative","container");
\r
1302 Contract.EndContractBlock();
\r
1304 var targetUri=GetTargetUri(account).Combine(container);
\r
1305 var message = new HttpRequestMessage(HttpMethod.Put, targetUri);
\r
1306 message.Headers.Add("Content-Length", "0");
\r
1307 using (var response = _baseHttpClient.SendAsyncWithRetries(message, 3).Result)
\r
1309 var expectedCodes = new[] {HttpStatusCode.Created, HttpStatusCode.Accepted, HttpStatusCode.OK};
\r
1310 if (!expectedCodes.Contains(response.StatusCode))
\r
1311 throw CreateWebException("CreateContainer", response.StatusCode);
\r
1314 using (var client = new RestClient(_baseClient))
\r
1316 if (!String.IsNullOrWhiteSpace(account))
\r
1317 client.BaseAddress = GetAccountUrl(account);
\r
1319 client.PutWithRetry(container, 3);
\r
1320 var expectedCodes = new[] {HttpStatusCode.Created, HttpStatusCode.Accepted, HttpStatusCode.OK};
\r
1321 if (!expectedCodes.Contains(client.StatusCode))
\r
1322 throw CreateWebException("CreateContainer", client.StatusCode);
\r
1327 public void DeleteContainer(string account, Uri container)
\r
1329 if (container == null)
\r
1330 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1331 if (container.IsAbsoluteUri)
\r
1332 throw new ArgumentException("The container must be relative","container");
\r
1333 Contract.EndContractBlock();
\r
1335 var targetUri = GetTargetUri(account).Combine(container);
\r
1336 var message = new HttpRequestMessage(HttpMethod.Delete, targetUri);
\r
1337 using (var response = _baseHttpClient.SendAsyncWithRetries(message, 3).Result)
\r
1339 var expectedCodes = new[] { HttpStatusCode.NotFound, HttpStatusCode.NoContent };
\r
1340 if (!expectedCodes.Contains(response.StatusCode))
\r
1341 throw CreateWebException("DeleteContainer", response.StatusCode);
\r
1344 using (var client = new RestClient(_baseClient))
\r
1346 if (!String.IsNullOrWhiteSpace(account))
\r
1347 client.BaseAddress = GetAccountUrl(account);
\r
1349 client.DeleteWithRetry(container, 3);
\r
1350 var expectedCodes = new[] {HttpStatusCode.NotFound, HttpStatusCode.NoContent};
\r
1351 if (!expectedCodes.Contains(client.StatusCode))
\r
1352 throw CreateWebException("DeleteContainer", client.StatusCode);
\r
1361 /// <param name="account"></param>
\r
1362 /// <param name="container"></param>
\r
1363 /// <param name="objectName"></param>
\r
1364 /// <param name="fileName"></param>
\r
1365 /// <param name="cancellationToken"> </param>
\r
1366 /// <returns></returns>
\r
1367 /// <remarks>This method should have no timeout or a very long one</remarks>
\r
1368 //Asynchronously download the object specified by *objectName* in a specific *container* to
\r
1370 public async Task GetObject(string account, Uri container, Uri objectName, string fileName,CancellationToken cancellationToken)
\r
1372 if (container == null)
\r
1373 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1374 if (container.IsAbsoluteUri)
\r
1375 throw new ArgumentException("The container must be relative","container");
\r
1376 if (objectName == null)
\r
1377 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1378 if (objectName.IsAbsoluteUri)
\r
1379 throw new ArgumentException("The objectName must be relative","objectName");
\r
1380 Contract.EndContractBlock();
\r
1385 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
\r
1386 //object to avoid concurrency errors.
\r
1388 //Download operations take a long time therefore they have no timeout.
\r
1389 using(var client = new RestClient(_baseClient) { Timeout = 0 })
\r
1391 if (!String.IsNullOrWhiteSpace(account))
\r
1392 client.BaseAddress = GetAccountUrl(account);
\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
1396 var builder = client.GetAddressBuilder(container, objectName);
\r
1397 var uri = builder.Uri;
\r
1399 //Download progress is reported to the Trace log
\r
1400 Log.InfoFormat("[GET] START {0}", objectName);
\r
1401 /*client.DownloadProgressChanged += (sender, args) =>
\r
1402 Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1403 fileName, args.ProgressPercentage,
\r
1404 args.BytesReceived,
\r
1405 args.TotalBytesToReceive);*/
\r
1406 var progress = new Progress<DownloadProgressChangedEventArgs>(args =>
\r
1408 Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1409 fileName, args.ProgressPercentage,
\r
1410 args.BytesReceived,
\r
1411 args.TotalBytesToReceive);
\r
1412 if (DownloadProgressChanged!=null)
\r
1413 DownloadProgressChanged(this, new DownloadArgs(args));
\r
1416 //Start downloading the object asynchronously
\r
1417 await client.DownloadFileTaskAsync(uri, fileName, cancellationToken,progress).ConfigureAwait(false);
\r
1419 //Once the download completes
\r
1420 //Delete the local client object
\r
1422 //And report failure or completion
\r
1424 catch (Exception exc)
\r
1426 Log.ErrorFormat("[GET] FAIL {0} with {1}", objectName, exc);
\r
1430 Log.InfoFormat("[GET] END {0}", objectName);
\r
1435 public async Task<IList<string>> PutHashMap(string account, Uri container, Uri objectName, TreeHash hash)
\r
1437 if (container == null)
\r
1438 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1439 if (container.IsAbsoluteUri)
\r
1440 throw new ArgumentException("The container must be relative","container");
\r
1441 if (objectName == null)
\r
1442 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1443 if (objectName.IsAbsoluteUri)
\r
1444 throw new ArgumentException("The objectName must be relative","objectName");
\r
1446 throw new ArgumentNullException("hash");
\r
1447 if (String.IsNullOrWhiteSpace(Token))
\r
1448 throw new InvalidOperationException("Invalid Token");
\r
1449 if (StorageUrl == null)
\r
1450 throw new InvalidOperationException("Invalid Storage Url");
\r
1451 Contract.EndContractBlock();
\r
1455 //The container and objectName are relative names. They are joined with the client's
\r
1456 //BaseAddress to create the object's absolute address
\r
1458 var targetUri = GetTargetUri(account).Combine(container).Combine(objectName);
\r
1461 var uri = new Uri(String.Format("{0}?format=json&hashmap",targetUri),UriKind.Absolute);
\r
1464 //Send the tree hash as Json to the server
\r
1465 var jsonHash = hash.ToJson();
\r
1466 if (Log.IsDebugEnabled)
\r
1467 Log.DebugFormat("Hashes:\r\n{0}", jsonHash);
\r
1469 var message = new HttpRequestMessage(HttpMethod.Put, uri)
\r
1471 Content = new StringContent(jsonHash)
\r
1473 message.Headers.Add("ETag",hash.TopHash.ToHashString());
\r
1475 //Don't use a timeout because putting the hashmap may be a long process
\r
1477 using (var response = await _baseHttpClientNoTimeout.SendAsyncWithRetries(message, 3).ConfigureAwait(false))
\r
1479 var empty = (IList<string>)new List<string>();
\r
1481 switch (response.StatusCode)
\r
1483 case HttpStatusCode.Created:
\r
1484 //The server will respond either with 201-created if all blocks were already on the server
\r
1486 case HttpStatusCode.Conflict:
\r
1487 //or with a 409-conflict and return the list of missing parts
\r
1488 using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
\r
1489 using(var reader=stream.GetLoggedReader(Log))
\r
1491 var serializer = new JsonSerializer();
\r
1492 serializer.Error += (sender, args) => Log.ErrorFormat("Deserialization error at [{0}] [{1}]", args.ErrorContext.Error, args.ErrorContext.Member);
\r
1493 var hashes = (List<string>)serializer.Deserialize(reader, typeof(List<string>));
\r
1497 //All other cases are unexpected
\r
1498 //Ensure that failure codes raise exceptions
\r
1499 response.EnsureSuccessStatusCode();
\r
1500 //And log any other codes as warngings, but continute processing
\r
1501 Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",response.StatusCode,response.ReasonPhrase);
\r
1509 public Task<IList<string>> PutHashMap(string account, Uri container, Uri objectName, TreeHash hash)
\r
1511 if (container == null)
\r
1512 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1513 if (container.IsAbsoluteUri)
\r
1514 throw new ArgumentException("The container must be relative","container");
\r
1515 if (objectName == null)
\r
1516 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1517 if (objectName.IsAbsoluteUri)
\r
1518 throw new ArgumentException("The objectName must be relative","objectName");
\r
1520 throw new ArgumentNullException("hash");
\r
1521 if (String.IsNullOrWhiteSpace(Token))
\r
1522 throw new InvalidOperationException("Invalid Token");
\r
1523 if (StorageUrl == null)
\r
1524 throw new InvalidOperationException("Invalid Storage Url");
\r
1525 Contract.EndContractBlock();
\r
1529 //Don't use a timeout because putting the hashmap may be a long process
\r
1530 var client = new RestClient(_baseClient) { Timeout = 0 };
\r
1531 if (!String.IsNullOrWhiteSpace(account))
\r
1532 client.BaseAddress = GetAccountUrl(account);
\r
1534 //The container and objectName are relative names. They are joined with the client's
\r
1535 //BaseAddress to create the object's absolute address
\r
1536 var builder = client.GetAddressBuilder(container, objectName);
\r
1537 builder.Query = "format=json&hashmap";
\r
1538 var uri = builder.Uri;
\r
1541 //Send the tree hash as Json to the server
\r
1542 client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
\r
1543 var jsonHash = hash.ToJson();
\r
1545 client.Headers.Add("ETag",hash.TopHash.ToHashString());
\r
1546 var uploadTask=client.UploadStringTask(uri, "PUT", jsonHash);
\r
1547 if (Log.IsDebugEnabled)
\r
1548 Log.DebugFormat("Hashes:\r\n{0}", jsonHash);
\r
1549 return uploadTask.ContinueWith(t =>
\r
1552 var empty = (IList<string>)new List<string>();
\r
1555 //The server will respond either with 201-created if all blocks were already on the server
\r
1556 if (client.StatusCode == HttpStatusCode.Created)
\r
1558 //in which case we return an empty hash list
\r
1561 //or with a 409-conflict and return the list of missing parts
\r
1562 //A 409 will cause an exception so we need to check t.IsFaulted to avoid propagating the exception
\r
1565 var ex = t.Exception.InnerException;
\r
1566 var we = ex as WebException;
\r
1567 var response = we.Response as HttpWebResponse;
\r
1568 if (response!=null && response.StatusCode==HttpStatusCode.Conflict)
\r
1570 //In case of 409 the missing parts will be in the response content
\r
1571 using (var stream = response.GetResponseStream())
\r
1572 using(var reader=stream.GetLoggedReader(Log))
\r
1574 //We used to have to cleanup the content before returning it because it contains
\r
1575 //error content after the list of hashes
\r
1577 //As of 30/1/2012, the result is a proper Json array so we don't need to read the content
\r
1580 var serializer = new JsonSerializer();
\r
1581 serializer.Error += (sender, args) => Log.ErrorFormat("Deserialization error at [{0}] [{1}]", args.ErrorContext.Error, args.ErrorContext.Member);
\r
1582 var hashes = (List<string>)serializer.Deserialize(reader, typeof(List<string>));
\r
1586 //Any other status code is unexpected and the exception should be rethrown
\r
1587 Log.LogError(response);
\r
1592 //Any other status code is unexpected but there was no exception. We can probably continue processing
\r
1593 Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",client.StatusCode,client.StatusDescription);
\r
1604 public async Task<byte[]> GetBlock(string account, Uri container, Uri relativeUrl, long start, long? end, CancellationToken cancellationToken)
\r
1606 if (String.IsNullOrWhiteSpace(Token))
\r
1607 throw new InvalidOperationException("Invalid Token");
\r
1608 if (StorageUrl == null)
\r
1609 throw new InvalidOperationException("Invalid Storage Url");
\r
1610 if (container == null)
\r
1611 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1612 if (container.IsAbsoluteUri)
\r
1613 throw new ArgumentException("The container must be relative","container");
\r
1614 if (relativeUrl == null)
\r
1615 throw new ArgumentNullException("relativeUrl");
\r
1616 if (end.HasValue && end < 0)
\r
1617 throw new ArgumentOutOfRangeException("end");
\r
1619 throw new ArgumentOutOfRangeException("start");
\r
1620 Contract.EndContractBlock();
\r
1623 var targetUri = GetTargetUri(account).Combine(container).Combine(relativeUrl);
\r
1624 var message = new HttpRequestMessage(HttpMethod.Get, targetUri);
\r
1625 message.Headers.Range.Ranges.Add(new RangeItemHeaderValue(start,end));
\r
1627 //Don't use a timeout because putting the hashmap may be a long process
\r
1629 IProgress<DownloadArgs> progress = new Progress<DownloadArgs>(args =>
\r
1631 Log.DebugFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1632 targetUri.Segments.Last(), args.ProgressPercentage,
\r
1633 args.BytesReceived,
\r
1634 args.TotalBytesToReceive);
\r
1636 if (DownloadProgressChanged!=null)
\r
1637 DownloadProgressChanged(this, args);
\r
1641 using (var response = await _baseHttpClientNoTimeout.SendAsyncWithRetries(message, 3, HttpCompletionOption.ResponseHeadersRead,
\r
1642 cancellationToken).ConfigureAwait(false))
\r
1643 using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
\r
1644 using(var targetStream=new MemoryStream())
\r
1647 long totalSize = response.Content.Headers.ContentLength ?? 0;
\r
1649 var buffer = new byte[65536];
\r
1651 while ((read = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0)
\r
1654 progress.Report(new DownloadArgs(total, totalSize));
\r
1655 await targetStream.WriteAsync(buffer, 0, read).ConfigureAwait(false);
\r
1658 var result = targetStream.ToArray();
\r
1661 /*using (var client = new RestClient(_baseClient) {Timeout = 0, RangeFrom = start, RangeTo = end})
\r
1663 if (!String.IsNullOrWhiteSpace(account))
\r
1664 client.BaseAddress = GetAccountUrl(account);
\r
1666 var builder = client.GetAddressBuilder(container, relativeUrl);
\r
1667 var uri = builder.Uri;
\r
1669 /* client.DownloadProgressChanged += (sender, args) =>
\r
1671 Log.DebugFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1672 uri.Segments.Last(), args.ProgressPercentage,
\r
1673 args.BytesReceived,
\r
1674 args.TotalBytesToReceive);
\r
1675 DownloadProgressChanged(sender, args);
\r
1677 var progress = new Progress<DownloadProgressChangedEventArgs>(args =>
\r
1679 Log.DebugFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
\r
1680 uri.Segments.Last(), args.ProgressPercentage,
\r
1681 args.BytesReceived,
\r
1682 args.TotalBytesToReceive);
\r
1683 if (DownloadProgressChanged!=null)
\r
1684 DownloadProgressChanged(this, args);
\r
1688 var result = await client.DownloadDataTaskAsync(uri, cancellationToken,progress).ConfigureAwait(false);
\r
1693 public event EventHandler<UploadArgs> UploadProgressChanged;
\r
1694 public event EventHandler<DownloadArgs> DownloadProgressChanged;
\r
1697 public async Task PostBlock(string account, Uri container, byte[] block, int offset, int count,CancellationToken token)
\r
1699 if (container == null)
\r
1700 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1701 if (container.IsAbsoluteUri)
\r
1702 throw new ArgumentException("The container must be relative","container");
\r
1703 if (block == null)
\r
1704 throw new ArgumentNullException("block");
\r
1705 if (offset < 0 || offset >= block.Length)
\r
1706 throw new ArgumentOutOfRangeException("offset");
\r
1707 if (count < 0 || count > block.Length)
\r
1708 throw new ArgumentOutOfRangeException("count");
\r
1709 if (String.IsNullOrWhiteSpace(Token))
\r
1710 throw new InvalidOperationException("Invalid Token");
\r
1711 if (StorageUrl == null)
\r
1712 throw new InvalidOperationException("Invalid Storage Url");
\r
1713 Contract.EndContractBlock();
\r
1719 var containerUri = GetTargetUri(account).Combine(container);
\r
1720 var targetUri = new Uri(String.Format("{0}?update", containerUri));
\r
1723 //Don't use a timeout because putting the hashmap may be a long process
\r
1726 Log.InfoFormat("[BLOCK POST] START");
\r
1729 var progress = new Progress<UploadArgs>(args =>
\r
1731 Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",
\r
1732 args.ProgressPercentage,
\r
1734 args.TotalBytesToSend);
\r
1735 if (UploadProgressChanged != null)
\r
1736 UploadProgressChanged(this,args);
\r
1739 var message = new HttpRequestMessage(HttpMethod.Post, targetUri);
\r
1740 message.Content = new ByteArrayContent(block, offset, count);
\r
1743 using (var response = await _baseHttpClientNoTimeout.SendAsyncWithRetries(message, 3).ConfigureAwait(false))
\r
1745 Log.InfoFormat("[BLOCK POST PROGRESS] Completed ");
\r
1746 response.EnsureSuccessStatusCode();
\r
1748 Log.InfoFormat("[BLOCK POST] END");*/
\r
1749 using (var client = new RestClient(_baseClient) { Timeout = 0 })
\r
1751 if (!String.IsNullOrWhiteSpace(account))
\r
1752 client.BaseAddress = GetAccountUrl(account);
\r
1754 var builder = client.GetAddressBuilder(container, _emptyUri);
\r
1755 //We are doing an update
\r
1756 builder.Query = "update";
\r
1757 var uri = builder.Uri;
\r
1759 client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
\r
1761 Log.InfoFormat("[BLOCK POST] START");
\r
1763 client.UploadFileCompleted += (sender, args) =>
\r
1764 Log.InfoFormat("[BLOCK POST PROGRESS] Completed ");
\r
1766 var progress=new Progress<UploadProgressChangedEventArgs>(args=>
\r
1768 Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",
\r
1769 args.ProgressPercentage, args.BytesSent,
\r
1770 args.TotalBytesToSend);
\r
1771 if (UploadProgressChanged!=null)
\r
1772 UploadProgressChanged(this, new UploadArgs(args));
\r
1774 var buffer = new byte[count];
\r
1775 Buffer.BlockCopy(block, offset, buffer, 0, count);
\r
1777 await client.UploadDataTaskAsync(uri, "POST", buffer,token,progress).ConfigureAwait(false);
\r
1778 Log.InfoFormat("[BLOCK POST] END");
\r
1781 catch (TaskCanceledException )
\r
1783 Log.Info("Aborting block");
\r
1786 catch (Exception exc)
\r
1788 Log.ErrorFormat("[BLOCK POST] FAIL with \r{0}", exc);
\r
1794 public async Task<TreeHash> GetHashMap(string account, Uri container, Uri objectName)
\r
1796 if (container == null)
\r
1797 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1798 if (container.IsAbsoluteUri)
\r
1799 throw new ArgumentException("The container must be relative","container");
\r
1800 if (objectName == null)
\r
1801 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1802 if (objectName.IsAbsoluteUri)
\r
1803 throw new ArgumentException("The objectName must be relative","objectName");
\r
1804 if (String.IsNullOrWhiteSpace(Token))
\r
1805 throw new InvalidOperationException("Invalid Token");
\r
1806 if (StorageUrl == null)
\r
1807 throw new InvalidOperationException("Invalid Storage Url");
\r
1808 Contract.EndContractBlock();
\r
1813 var objectUri = GetTargetUri(account).Combine(container).Combine(objectName);
\r
1814 var targetUri = new Uri(String.Format("{0}?format=json&hashmap", objectUri));
\r
1816 //Start downloading the object asynchronously
\r
1817 var json = await GetStringAsync(targetUri, "").ConfigureAwait(false);
\r
1818 var treeHash = TreeHash.Parse(json);
\r
1819 Log.InfoFormat("[GET HASH] END {0}", objectName);
\r
1822 using (var client = new RestClient(_baseClient) { Timeout = 0 })
\r
1824 if (!String.IsNullOrWhiteSpace(account))
\r
1825 client.BaseAddress = GetAccountUrl(account);
\r
1827 //The container and objectName are relative names. They are joined with the client's
\r
1828 //BaseAddress to create the object's absolute address
\r
1830 var builder = client.GetAddressBuilder(container, objectName);
\r
1831 builder.Query = "format=json&hashmap";
\r
1832 var uri = builder.Uri;
\r
1835 var json = await client.DownloadStringTaskAsync(uri).ConfigureAwait(false);
\r
1836 var treeHash = TreeHash.Parse(json);
\r
1837 Log.InfoFormat("[GET HASH] END {0}", objectName);
\r
1842 catch (Exception exc)
\r
1844 Log.ErrorFormat("[GET HASH] END {0} with {1}", objectName, exc);
\r
1854 /// <param name="account"></param>
\r
1855 /// <param name="container"></param>
\r
1856 /// <param name="objectName"></param>
\r
1857 /// <param name="fileName"></param>
\r
1858 /// <param name="hash">Optional hash value for the file. If no hash is provided, the method calculates a new hash</param>
\r
1859 /// <param name="contentType"> </param>
\r
1860 /// <remarks>>This method should have no timeout or a very long one</remarks>
\r
1861 public async Task PutObject(string account, Uri container, Uri objectName, string fileName, string hash = Signature.MERKLE_EMPTY, string contentType = "application/octet-stream")
\r
1863 if (container == null)
\r
1864 throw new ArgumentNullException("container", "The container property can't be empty");
\r
1865 if (container.IsAbsoluteUri)
\r
1866 throw new ArgumentException("The container must be relative","container");
\r
1867 if (objectName == null)
\r
1868 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
1869 if (objectName.IsAbsoluteUri)
\r
1870 throw new ArgumentException("The objectName must be relative","objectName");
\r
1871 if (String.IsNullOrWhiteSpace(fileName))
\r
1872 throw new ArgumentNullException("fileName", "The fileName property can't be empty");
\r
1874 if (!File.Exists(fileName) && !Directory.Exists(fileName))
\r
1875 throw new FileNotFoundException("The file or directory does not exist",fileName);
\r
1881 using (var client = new RestClient(_baseClient) { Timeout = 0 })
\r
1883 if (!String.IsNullOrWhiteSpace(account))
\r
1884 client.BaseAddress = GetAccountUrl(account);
\r
1886 var builder = client.GetAddressBuilder(container, objectName);
\r
1887 var uri = builder.Uri;
\r
1889 string etag = hash ;
\r
1891 client.Headers.Add("Content-Type", contentType);
\r
1892 if (contentType!=ObjectInfo.CONTENT_TYPE_DIRECTORY)
\r
1893 client.Headers.Add("ETag", etag);
\r
1896 Log.InfoFormat("[PUT] START {0}", objectName);
\r
1897 client.UploadProgressChanged += (sender, args) =>
\r
1899 using (ThreadContext.Stacks["PUT"].Push("Progress"))
\r
1901 Log.InfoFormat("{0} {1}% {2} of {3}", fileName,
\r
1902 args.ProgressPercentage,
\r
1903 args.BytesSent, args.TotalBytesToSend);
\r
1907 client.UploadFileCompleted += (sender, args) =>
\r
1909 using (ThreadContext.Stacks["PUT"].Push("Progress"))
\r
1911 Log.InfoFormat("Completed {0}", fileName);
\r
1915 if (contentType==ObjectInfo.CONTENT_TYPE_DIRECTORY)
\r
1916 await client.UploadDataTaskAsync(uri, "PUT", new byte[0]).ConfigureAwait(false);
\r
1918 await client.UploadFileTaskAsync(uri, "PUT", fileName).ConfigureAwait(false);
\r
1921 Log.InfoFormat("[PUT] END {0}", objectName);
\r
1923 catch (Exception exc)
\r
1925 Log.ErrorFormat("[PUT] END {0} with {1}", objectName, exc);
\r
1933 private static string CalculateHash(string fileName)
\r
1935 Contract.Requires(!String.IsNullOrWhiteSpace(fileName));
\r
1936 Contract.EndContractBlock();
\r
1939 using (var hasher = MD5.Create())
\r
1940 using(var stream=File.OpenRead(fileName))
\r
1942 var hashBuilder=new StringBuilder();
\r
1943 foreach (byte b in hasher.ComputeHash(stream))
\r
1944 hashBuilder.Append(b.ToString("x2").ToLower());
\r
1945 hash = hashBuilder.ToString();
\r
1952 public void MoveObject(string account, Uri sourceContainer, Uri oldObjectName, Uri targetContainer, Uri newObjectName)
\r
1954 if (sourceContainer == null)
\r
1955 throw new ArgumentNullException("sourceContainer", "The sourceContainer property can't be empty");
\r
1956 if (sourceContainer.IsAbsoluteUri)
\r
1957 throw new ArgumentException("The sourceContainer must be relative","sourceContainer");
\r
1958 if (oldObjectName == null)
\r
1959 throw new ArgumentNullException("oldObjectName", "The oldObjectName property can't be empty");
\r
1960 if (oldObjectName.IsAbsoluteUri)
\r
1961 throw new ArgumentException("The oldObjectName must be relative","oldObjectName");
\r
1962 if (targetContainer == null)
\r
1963 throw new ArgumentNullException("targetContainer", "The targetContainer property can't be empty");
\r
1964 if (targetContainer.IsAbsoluteUri)
\r
1965 throw new ArgumentException("The targetContainer must be relative","targetContainer");
\r
1966 if (newObjectName == null)
\r
1967 throw new ArgumentNullException("newObjectName", "The newObjectName property can't be empty");
\r
1968 if (newObjectName.IsAbsoluteUri)
\r
1969 throw new ArgumentException("The newObjectName must be relative","newObjectName");
\r
1970 Contract.EndContractBlock();
\r
1972 var baseUri = GetTargetUri(account);
\r
1973 var targetUri = baseUri.Combine(targetContainer).Combine(newObjectName);
\r
1974 var sourceUri = new Uri(String.Format("/{0}/{1}", sourceContainer, oldObjectName),UriKind.Relative);
\r
1976 var message = new HttpRequestMessage(HttpMethod.Put, targetUri);
\r
1977 message.Headers.Add("X-Move-From", sourceUri.ToString());
\r
1978 using (var response = _baseHttpClient.SendAsyncWithRetries(message, 3).Result)
\r
1980 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
\r
1981 if (!expectedCodes.Contains(response.StatusCode))
\r
1982 throw CreateWebException("MoveObject", response.StatusCode);
\r
1985 using (var client = new RestClient(_baseClient))
\r
1987 if (!String.IsNullOrWhiteSpace(account))
\r
1988 client.BaseAddress = GetAccountUrl(account);
\r
1990 client.Headers.Add("X-Move-From", sourceUri.ToString());
\r
1991 client.PutWithRetry(targetUri, 3);
\r
1993 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
\r
1994 if (!expectedCodes.Contains(client.StatusCode))
\r
1995 throw CreateWebException("MoveObject", client.StatusCode);
\r
2000 public void DeleteObject(string account, Uri sourceContainer, Uri objectName, bool isDirectory)
\r
2002 if (sourceContainer == null)
\r
2003 throw new ArgumentNullException("sourceContainer", "The sourceContainer property can't be empty");
\r
2004 if (sourceContainer.IsAbsoluteUri)
\r
2005 throw new ArgumentException("The sourceContainer must be relative","sourceContainer");
\r
2006 if (objectName == null)
\r
2007 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
\r
2008 if (objectName.IsAbsoluteUri)
\r
2009 throw new ArgumentException("The objectName must be relative","objectName");
\r
2010 Contract.EndContractBlock();
\r
2012 var targetUrl = FolderConstants.TrashContainer + "/" + objectName;
\r
2015 targetUrl = targetUrl + "?delimiter=/";
\r
2018 var sourceUrl = String.Format("/{0}/{1}", sourceContainer, objectName);
\r
2020 using (var client = new RestClient(_baseClient))
\r
2022 if (!String.IsNullOrWhiteSpace(account))
\r
2023 client.BaseAddress = GetAccountUrl(account);
\r
2025 client.Headers.Add("X-Move-From", sourceUrl);
\r
2026 client.AllowedStatusCodes.Add(HttpStatusCode.NotFound);
\r
2027 Log.InfoFormat("[TRASH] [{0}] to [{1}]",sourceUrl,targetUrl);
\r
2028 client.PutWithRetry(new Uri(targetUrl,UriKind.Relative), 3);
\r
2030 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created,HttpStatusCode.NotFound};
\r
2031 if (!expectedCodes.Contains(client.StatusCode))
\r
2032 throw CreateWebException("DeleteObject", client.StatusCode);
\r
2037 private static WebException CreateWebException(string operation, HttpStatusCode statusCode)
\r
2039 return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode));
\r
2044 public IEnumerable<ObjectInfo> ListDirectories(ContainerInfo container)
\r
2046 var directories=this.ListObjects(container.Account, container.Name, "/");
\r
2050 public bool CanUpload(string account, ObjectInfo cloudFile)
\r
2052 Contract.Requires(!String.IsNullOrWhiteSpace(account));
\r
2053 Contract.Requires(cloudFile!=null);
\r
2055 using (var client = new RestClient(_baseClient))
\r
2057 if (!String.IsNullOrWhiteSpace(account))
\r
2058 client.BaseAddress = GetAccountUrl(account);
\r
2061 var parts = cloudFile.Name.ToString().Split('/');
\r
2062 var folder = String.Join("/", parts,0,parts.Length-1);
\r
2064 var fileName = String.Format("{0}/{1}.pithos.ignore", folder, Guid.NewGuid());
\r
2065 var fileUri=fileName.ToEscapedUri();
\r
2067 client.Parameters.Clear();
\r
2070 var relativeUri = cloudFile.Container.Combine(fileUri);
\r
2071 client.PutWithRetry(relativeUri, 3, @"application/octet-stream");
\r
2073 var expectedCodes = new[] { HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
\r
2074 var result=(expectedCodes.Contains(client.StatusCode));
\r
2075 DeleteObject(account, cloudFile.Container, fileUri, cloudFile.IsDirectory);
\r
2085 ~CloudFilesClient()
\r
2090 public void Dispose()
\r
2093 GC.SuppressFinalize(this);
\r
2096 protected virtual void Dispose(bool disposing)
\r
2100 if (_httpClientHandler!=null)
\r
2101 _httpClientHandler.Dispose();
\r
2102 if (_baseClient!=null)
\r
2103 _baseClient.Dispose();
\r
2104 if(_baseHttpClient!=null)
\r
2105 _baseHttpClient.Dispose();
\r
2106 if (_baseHttpClientNoTimeout!=null)
\r
2107 _baseHttpClientNoTimeout.Dispose();
\r
2109 _httpClientHandler = null;
\r
2110 _baseClient = null;
\r
2111 _baseHttpClient = null;
\r
2112 _baseHttpClientNoTimeout = null;
\r
2116 public class ShareAccountInfo
\r
2118 public DateTime? last_modified { get; set; }
\r
2119 public string name { get; set; }
\r