Пример #1
0
        public bool Update(string filesystem, string path, string action, byte[] content = null, long?position = 0, bool?close = null)
        {
            if (action.Equals("flush", StringComparison.InvariantCultureIgnoreCase) && !position.HasValue)
            {
                throw new ArgumentException("Action `flush` must be performed with a position parameter.");
            }
            var parameters = new List <string>
            {
                $"action={action}".ToLowerInvariant()
            };

            if (position.HasValue)
            {
                parameters.Add($"position={position}");
            }
            if (close.HasValue)
            {
                parameters.Add($"close={close}".ToLowerInvariant());
            }
            return(_retryPolicy.Execute(() =>
            {
                using (var request = new HttpRequestMessage(HttpMethod.Patch,
                                                            $"https://{_account}.dfs.core.windows.net/{filesystem}/{path}?{string.Join("&", parameters)}"))
                {
                    if (content != null)
                    {
                        request.Content = new ByteArrayContent(content);
                    }
                    DateTime now = DateTime.UtcNow;
                    request.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture));
                    request.Headers.Add("x-ms-version", _version);
                    request.Headers.Authorization = _authorizationMethod == AuthorizationMethod.SharedKey ? AzureStorageAuthenticationHelper.BuildSignedAuthorizationHeader(_account, _key, now, request, contentLength: content?.LongLength)
                        : _authorizationMethod == AuthorizationMethod.Oauth ? AzureStorageAuthenticationHelper.BuildBearerTokenHeader(_httpClient, _tenantId, _clientId, _clientSecret)
                        : null;
                    if (_authorizationMethod == AuthorizationMethod.SharedAccessSignature)
                    {
                        request.RequestUri = new Uri(request.RequestUri + _sas.Replace('?', '&'));
                    }
                    using (var response =
                               _httpClient.SendAsync(request).GetAwaiter().GetResult())
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return true;
                        }
                        if (response.StatusCode == HttpStatusCode.InternalServerError && response.Content
                            .ReadAsStringAsync().GetAwaiter().GetResult()
                            .Contains("Operation could not be completed within the specified time."))
                        {
                            throw new AdlOperationTimedOutException();
                        }
                        if (response.StatusCode == HttpStatusCode.Unauthorized && _authorizationMethod == AuthorizationMethod.Oauth)
                        {
                            throw new AuthTokenInvalidException();
                        }
                        throw new Exception(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
                    }
                }
            }));
        }
Пример #2
0
        public bool Delete(string filesystem, string path, bool recursive, string continuation = null)
        {
            var parameters = new List <string>
            {
                $"recursive={recursive}".ToLowerInvariant()
            };

            if (!string.IsNullOrEmpty(continuation))
            {
                parameters.Add($"continuation={HttpUtility.UrlEncode(continuation)}");
            }
            return(_retryPolicy.Execute(() =>
            {
                using (var request = new HttpRequestMessage(HttpMethod.Delete,
                                                            $"https://{_account}.dfs.core.windows.net/{filesystem}/{path}?{string.Join("&", parameters)}"))
                {
                    DateTime now = DateTime.UtcNow;
                    request.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture));
                    request.Headers.Add("x-ms-version", _version);
                    request.Headers.Authorization = _authorizationMethod == AuthorizationMethod.SharedKey ? AzureStorageAuthenticationHelper.BuildSignedAuthorizationHeader(_account, _key, now, request)
                        : _authorizationMethod == AuthorizationMethod.Oauth ? AzureStorageAuthenticationHelper.BuildBearerTokenHeader(_httpClient, _tenantId, _clientId, _clientSecret)
                        : null;
                    if (_authorizationMethod == AuthorizationMethod.SharedAccessSignature)
                    {
                        request.RequestUri = new Uri(request.RequestUri + _sas.Replace('?', '&'));
                    }
                    using (var response = _httpClient.SendAsync(request).GetAwaiter().GetResult())
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            if (response.StatusCode == HttpStatusCode.NotFound)
                            {
                                return false;
                            }
                            if (response.StatusCode == HttpStatusCode.Conflict)
                            {
                                return false;
                            }
                            if (response.StatusCode == HttpStatusCode.Unauthorized &&
                                _authorizationMethod == AuthorizationMethod.Oauth)
                            {
                                throw new AuthTokenInvalidException();
                            }
                            throw new Exception(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
                        }

                        var continuationHeader = response.Headers.Any(h => h.Key == "x-ms-continuation")
                            ? response.Headers.Single(h => h.Key == "x-ms-continuation").Value.Single()
                            : null;
                        if (!string.IsNullOrEmpty(continuationHeader))
                        {
                            return Delete(filesystem, path, recursive, continuationHeader);
                        }

                        return true;
                    }
                }
            }));
        }
        public AdlFilesystemProperties GetProperties(string filesystem)
        {
            AssertValidity(filesystem);
            var parameters = new List <string>
            {
                $"resource=filesystem"
            };

            return(_retryPolicy.Execute(() =>
            {
                using (var request = new HttpRequestMessage(HttpMethod.Head,
                                                            $"https://{_account}.dfs.core.windows.net/{filesystem}?{string.Join("&", parameters)}"))
                {
                    DateTime now = DateTime.UtcNow;
                    request.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture));
                    request.Headers.Add("x-ms-version", _version);
                    request.Headers.Authorization = _authorizationMethod == AuthorizationMethod.SharedKey ? AzureStorageAuthenticationHelper.BuildSignedAuthorizationHeader(_account, _key, now, request)
                        : _authorizationMethod == AuthorizationMethod.Oauth ? AzureStorageAuthenticationHelper.BuildBearerTokenHeader(_httpClient, _tenantId, _clientId, _clientSecret)
                        : null;
                    if (_authorizationMethod == AuthorizationMethod.SharedAccessSignature)
                    {
                        request.RequestUri = new Uri(request.RequestUri + _sas.Replace('?', '&'));
                    }
                    using (var response = _httpClient.SendAsync(request).GetAwaiter().GetResult())
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            if (response.StatusCode == HttpStatusCode.InternalServerError && response.Content
                                .ReadAsStringAsync().GetAwaiter().GetResult()
                                .Contains("Operation could not be completed within the specified time."))
                            {
                                throw new AdlOperationTimedOutException();
                            }
                            if (response.StatusCode == HttpStatusCode.NotFound)
                            {
                                return null;
                            }
                            throw new Exception(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
                        }

                        return new AdlFilesystemProperties
                        {
                            NamespaceEnabled = response.Headers.SingleOrDefault(h => h.Key == "x-ms-namespace-enabled")
                                               .Value?.FirstOrDefault(),
                            Properties = response.Headers.SingleOrDefault(h => h.Key == "x-ms-properties").Value
                                         ?.FirstOrDefault()
                        };
                    }
                }
            }));
        }
        public bool Delete(string filesystem)
        {
            AssertValidity(filesystem);
            var parameters = new List <string>
            {
                $"resource=filesystem".ToLowerInvariant()
            };

            return(_retryPolicy.Execute(() =>
            {
                using (var request = new HttpRequestMessage(HttpMethod.Delete,
                                                            $"https://{_account}.dfs.core.windows.net/{filesystem}?{string.Join("&", parameters)}"))
                {
                    DateTime now = DateTime.UtcNow;
                    request.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture));
                    request.Headers.Add("x-ms-version", _version);
                    request.Headers.Authorization = _authorizationMethod == AuthorizationMethod.SharedKey ? AzureStorageAuthenticationHelper.BuildSignedAuthorizationHeader(_account, _key, now, request)
                        : _authorizationMethod == AuthorizationMethod.Oauth ? AzureStorageAuthenticationHelper.BuildBearerTokenHeader(_httpClient, _tenantId, _clientId, _clientSecret)
                        : null;
                    if (_authorizationMethod == AuthorizationMethod.SharedAccessSignature)
                    {
                        request.RequestUri = new Uri(request.RequestUri + _sas.Replace('?', '&'));
                    }
                    using (HttpResponseMessage response =
                               _httpClient.SendAsync(request).GetAwaiter().GetResult())
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            if (response.StatusCode == HttpStatusCode.Conflict)
                            {
                                return false;
                            }
                            if (response.StatusCode == HttpStatusCode.NotFound)
                            {
                                return false;
                            }
                            if (response.StatusCode == HttpStatusCode.Unauthorized &&
                                _authorizationMethod == AuthorizationMethod.Oauth)
                            {
                                throw new AuthTokenInvalidException();
                            }
                            throw new Exception(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
                        }

                        return response.StatusCode == HttpStatusCode.Accepted;
                    }
                }
            }));
        }
        public bool SetProperties(string filesystem, Dictionary <string, string> properties = null)
        {
            AssertValidity(filesystem);
            var parameters = new List <string>
            {
                $"resource=filesystem"
            };

            return(_retryPolicy.Execute(() =>
            {
                using (var request = new HttpRequestMessage(HttpMethod.Patch,
                                                            $"https://{_account}.dfs.core.windows.net/{filesystem}?{string.Join("&", parameters)}"))
                {
                    DateTime now = DateTime.UtcNow;
                    request.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture));
                    request.Headers.Add("x-ms-version", _version);
                    if (properties?.Any() ?? false)
                    {
                        request.Headers.Add("x-ms-properties",
                                            string.Join(",",
                                                        properties.Select(kvp =>
                                                                          $"{kvp.Key}={Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(kvp.Value))}")));
                    }
                    request.Headers.Authorization = _authorizationMethod == AuthorizationMethod.SharedKey ? AzureStorageAuthenticationHelper.BuildSignedAuthorizationHeader(_account, _key, now, request)
                        : _authorizationMethod == AuthorizationMethod.Oauth ? AzureStorageAuthenticationHelper.BuildBearerTokenHeader(_httpClient, _tenantId, _clientId, _clientSecret)
                        : null;
                    if (_authorizationMethod == AuthorizationMethod.SharedAccessSignature)
                    {
                        request.RequestUri = new Uri(request.RequestUri + _sas.Replace('?', '&'));
                    }
                    using (var response = _httpClient.SendAsync(request).GetAwaiter().GetResult())
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            if (response.StatusCode == HttpStatusCode.InternalServerError && response.Content
                                .ReadAsStringAsync().GetAwaiter().GetResult()
                                .Contains("Operation could not be completed within the specified time."))
                            {
                                throw new AdlOperationTimedOutException();
                            }
                            return false;
                        }

                        return true;
                    }
                }
            }));
        }
Пример #6
0
 public Stream ReadStream(string filesystem, string path, long rangeStart, long rangeStop, int?timeout = null)
 {
     return(_retryPolicy.Execute(() =>
     {
         using (var request = new HttpRequestMessage(HttpMethod.Get,
                                                     $"https://{_account}.dfs.core.windows.net/{filesystem}/{path}"))
         {
             DateTime now = DateTime.UtcNow;
             request.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture));
             request.Headers.Add("x-ms-version", _version);
             request.Headers.Add("Range", $"bytes={rangeStart}-{rangeStop}");
             request.Headers.Authorization = _authorizationMethod == AuthorizationMethod.SharedKey ? AzureStorageAuthenticationHelper.BuildSignedAuthorizationHeader(_account, _key, now, request, range: $"bytes={rangeStart}-{rangeStop}")
                 : _authorizationMethod == AuthorizationMethod.Oauth ? AzureStorageAuthenticationHelper.BuildBearerTokenHeader(_httpClient, _tenantId, _clientId, _clientSecret)
                 : null;
             if (_authorizationMethod == AuthorizationMethod.SharedAccessSignature)
             {
                 request.RequestUri = new Uri(request.RequestUri + _sas.Replace('?', '&'));
             }
             using (var response = _httpClient.SendAsync(request).GetAwaiter().GetResult())
             {
                 if (!response.IsSuccessStatusCode)
                 {
                     if (response.StatusCode == HttpStatusCode.InternalServerError && response.Content
                         .ReadAsStringAsync().GetAwaiter().GetResult()
                         .Contains("Operation could not be completed within the specified time."))
                     {
                         throw new AdlOperationTimedOutException();
                     }
                     if (response.StatusCode == HttpStatusCode.Unauthorized && _authorizationMethod == AuthorizationMethod.Oauth)
                     {
                         throw new AuthTokenInvalidException();
                     }
                     throw new Exception(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
                 }
                 var ms = new MemoryStream();
                 response.Content.ReadAsStreamAsync().GetAwaiter().GetResult().CopyTo(ms);
                 return ms;
             }
         }
     }));
 }
Пример #7
0
        public AdlPathProperties GetProperties(string filesystem, string path, string action = null, string upn = null)
        {
            var parameters = new List <string>();

            if (!string.IsNullOrEmpty(action))
            {
                parameters.Add($"action={action}");
            }
            if (!string.IsNullOrEmpty(upn))
            {
                parameters.Add($"upn={upn}".ToLowerInvariant());
            }
            return(_retryPolicy.Execute(() =>
            {
                using (var request = new HttpRequestMessage(HttpMethod.Head,
                                                            $"https://{_account}.dfs.core.windows.net/{filesystem}/{path}?{string.Join("&", parameters)}"))
                {
                    DateTime now = DateTime.UtcNow;
                    request.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture));
                    request.Headers.Add("x-ms-version", _version);
                    request.Headers.Authorization = _authorizationMethod == AuthorizationMethod.SharedKey ? AzureStorageAuthenticationHelper.BuildSignedAuthorizationHeader(_account, _key, now, request)
                        : _authorizationMethod == AuthorizationMethod.Oauth ? AzureStorageAuthenticationHelper.BuildBearerTokenHeader(_httpClient, _tenantId, _clientId, _clientSecret)
                        : null;
                    if (_authorizationMethod == AuthorizationMethod.SharedAccessSignature)
                    {
                        request.RequestUri = new Uri(request.RequestUri + _sas.Replace('?', '&'));
                    }
                    using (var response =
                               _httpClient.SendAsync(request).GetAwaiter().GetResult())
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            if (response.StatusCode == HttpStatusCode.InternalServerError && response.Content
                                .ReadAsStringAsync().GetAwaiter().GetResult()
                                .Contains("Operation could not be completed within the specified time."))
                            {
                                throw new AdlOperationTimedOutException();
                            }
                            if (response.StatusCode == HttpStatusCode.Unauthorized &&
                                _authorizationMethod == AuthorizationMethod.Oauth)
                            {
                                throw new AuthTokenInvalidException();
                            }
                            throw new Exception(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
                        }

                        return new AdlPathProperties
                        {
                            Acl = response.Headers.SingleOrDefault(h => h.Key == "x-ms-acl").Value?.FirstOrDefault(),
                            Group =
                                response.Headers.SingleOrDefault(h => h.Key == "x-ms-group").Value?.FirstOrDefault(),
                            LeaseDuration = response.Headers.SingleOrDefault(h => h.Key == "x-ms-lease-duration").Value
                                            ?.FirstOrDefault(),
                            LeaseState = response.Headers.SingleOrDefault(h => h.Key == "x-ms-lease-state").Value
                                         ?.FirstOrDefault(),
                            LeaseStatus = response.Headers.SingleOrDefault(h => h.Key == "x-ms-lease-status").Value
                                          ?.FirstOrDefault(),
                            Owner =
                                response.Headers.SingleOrDefault(h => h.Key == "x-ms-owner").Value?.FirstOrDefault(),
                            Permissions = response.Headers.SingleOrDefault(h => h.Key == "x-ms-permissions").Value
                                          ?.FirstOrDefault(),
                            Properties = response.Headers.SingleOrDefault(h => h.Key == "x-ms-properties").Value
                                         ?.FirstOrDefault(),
                            ResourceType = response.Headers.SingleOrDefault(h => h.Key == "x-ms-resource-type").Value
                                           ?.FirstOrDefault(),
                        };
                    }
                }
            }));
        }
Пример #8
0
        public IEnumerable <AdlPath> List(string filesystem, bool recursive = false, string directory = null, string continuation = null, int maxResults = 5000, int?timeout = null)
        {
            var parameters = new List <string>
            {
                $"recursive={recursive}".ToLowerInvariant(),
                $"resource=filesystem",
                $"maxResults={maxResults}".ToLowerInvariant()
            };

            if (!string.IsNullOrEmpty(directory))
            {
                parameters.Add($"directory={HttpUtility.UrlEncode(directory)}");
            }
            if (!string.IsNullOrEmpty(continuation))
            {
                parameters.Add($"continuation={HttpUtility.UrlEncode(continuation)}");
            }
            if (timeout.HasValue)
            {
                parameters.Add($"timeout={timeout}");
            }
            return(_retryPolicy.Execute(() => {
                using (var request = new HttpRequestMessage(HttpMethod.Get, $"https://{_account}.dfs.core.windows.net/{filesystem}?{string.Join("&",parameters)}"))
                {
                    DateTime now = DateTime.UtcNow;
                    request.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture));
                    request.Headers.Add("x-ms-version", _version);
                    request.Headers.Authorization = _authorizationMethod == AuthorizationMethod.SharedKey ? AzureStorageAuthenticationHelper.BuildSignedAuthorizationHeader(_account, _key, now, request)
                    : _authorizationMethod == AuthorizationMethod.Oauth ? AzureStorageAuthenticationHelper.BuildBearerTokenHeader(_httpClient, _tenantId, _clientId, _clientSecret)
                    : null;
                    if (_authorizationMethod == AuthorizationMethod.SharedAccessSignature)
                    {
                        request.RequestUri = new Uri(request.RequestUri + _sas.Replace('?', '&'));
                    }
                    using (var response = _httpClient.SendAsync(request).GetAwaiter().GetResult())
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            if (response.StatusCode == HttpStatusCode.NotFound)
                            {
                                return Enumerable.Empty <AdlPath>();
                            }
                            if (response.StatusCode == HttpStatusCode.InternalServerError && response.Content
                                .ReadAsStringAsync().GetAwaiter().GetResult()
                                .Contains("Operation could not be completed within the specified time."))
                            {
                                throw new AdlOperationTimedOutException();
                            }
                            if (response.StatusCode == HttpStatusCode.Unauthorized && _authorizationMethod == AuthorizationMethod.Oauth)
                            {
                                throw new AuthTokenInvalidException();
                            }
                            throw new AdlUnexpectedException(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
                        }
                        var result = JsonConvert.DeserializeObject <AdlPathList>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
                        var continuationHeader = response.Headers.Any(h => h.Key == "x-ms-continuation") ? response.Headers.Single(h => h.Key == "x-ms-continuation").Value.Single() : null;
                        if (!string.IsNullOrEmpty(continuationHeader))
                        {
                            continuation = continuationHeader;
                            var continuedPaths = List(filesystem, recursive, directory, continuation, maxResults);
                            return result.Paths.Concat(continuedPaths);
                        }

                        return result.Paths;
                    }
                }
            }));
        }
Пример #9
0
        public bool Lease(string filesystem, string path, string action, out string returnedLeaseId, string proposedLeaseId = null, string leaseId = null, int?leaseDuration = null, int?leaseBreakPeriod = null)
        {
            if (leaseDuration != null && ((leaseDuration < 15 && leaseDuration != -1) || leaseDuration > 60))
            {
                throw new ArgumentException($"Lease duration is invalid. Valid lease durations are -1 and 15-60. Provided {leaseDuration}.", nameof(leaseDuration));
            }
            bool success;

            (success, returnedLeaseId) = _retryPolicy.Execute(() =>
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post,
                                                            $"https://{_account}.dfs.core.windows.net/{filesystem}/{path}"))
                {
                    DateTime now = DateTime.UtcNow;
                    request.Headers.Add("x-ms-lease-action", action);
                    if (leaseDuration != null)
                    {
                        request.Headers.Add("x-ms-lease-duration", leaseDuration.ToString());
                    }
                    if (leaseBreakPeriod != null)
                    {
                        request.Headers.Add("x-ms-lease-break-period", leaseBreakPeriod.ToString());
                    }
                    if (!string.IsNullOrEmpty(proposedLeaseId))
                    {
                        request.Headers.Add("x-ms-proposed-lease-id", proposedLeaseId);
                    }
                    if (!string.IsNullOrEmpty(leaseId))
                    {
                        request.Headers.Add("x-ms-lease-id", leaseId);
                    }
                    request.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture));
                    request.Headers.Add("x-ms-version", _version);
                    request.Headers.Authorization = _authorizationMethod == AuthorizationMethod.SharedKey ? AzureStorageAuthenticationHelper.BuildSignedAuthorizationHeader(_account, _key, now, request)
                        : _authorizationMethod == AuthorizationMethod.Oauth ? AzureStorageAuthenticationHelper.BuildBearerTokenHeader(_httpClient, _tenantId, _clientId, _clientSecret)
                        : null;
                    if (_authorizationMethod == AuthorizationMethod.SharedAccessSignature)
                    {
                        request.RequestUri = new Uri(request.RequestUri + _sas.Replace('?', '&'));
                    }
                    using (var response = _httpClient.SendAsync(request).GetAwaiter().GetResult())
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            if (response.StatusCode == HttpStatusCode.NotFound)
                            {
                                return(false, null);
                            }

                            if (response.StatusCode == HttpStatusCode.Conflict)
                            {
                                return(false, null);
                            }

                            if (response.StatusCode == HttpStatusCode.Unauthorized &&
                                _authorizationMethod == AuthorizationMethod.Oauth)
                            {
                                throw new AuthTokenInvalidException();
                            }
                            throw new Exception(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
                        }

                        return(true, response.Headers.Any(h => h.Key == "x-ms-lease-id")
                            ? response.Headers.Single(h => h.Key == "x-ms-lease-id").Value.Single()
                            : null);
                    }
                }
            });
            return(success);
        }