Пример #1
0
        public Task <SwiftResponse> PutObject(string containerId, string objectId, byte[] data, Dictionary <string, string> headers = null, Dictionary <string, string> queryParams = null)
        {
            return(AuthorizeAndExecute(async(auth) =>
            {
                var url = SwiftUrlBuilder.GetObjectUrl(auth.StorageUrl, containerId, objectId, queryParams);

                var request = new HttpRequestMessage(HttpMethod.Put, url);

                FillRequest(request, auth, headers);

                try
                {
                    request.Content = new ByteArrayContent(data);

                    using (var response = await _client.SendAsync(request).ConfigureAwait(false))
                    {
                        var result = GetResponse <SwiftResponse>(response);

                        // container not found
                        if (result.StatusCode == HttpStatusCode.NotFound)
                        {
                            return await EnsurePutContainer(containerId, () => PutObject(containerId, objectId, data, headers, queryParams)).ConfigureAwait(false);
                        }

                        return result;
                    }
                }
                catch (Exception ex)
                {
                    return GetExceptionResponse <SwiftResponse>(ex, url);
                }
            }));
        }
Пример #2
0
        public Task <SwiftResponse> GetObject(string containerId, string objectId, Dictionary <string, string> headers = null, Dictionary <string, string> queryParams = null)
        {
            return(AuthorizeAndExecute(async(auth) =>
            {
                var url = SwiftUrlBuilder.GetObjectUrl(auth.StorageUrl, containerId, objectId, queryParams);

                var request = new HttpRequestMessage(HttpMethod.Get, url);

                FillRequest(request, auth, headers);

                try
                {
                    var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);

                    var result = GetResponse <SwiftResponse>(response);

                    if (response.IsSuccessStatusCode)
                    {
                        result.Stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
                    }

                    return result;
                }
                catch (Exception ex)
                {
                    return GetExceptionResponse <SwiftResponse>(ex, url);
                }
            }));
        }
        private async Task <SwiftAuthData> Authenticate(string username, string password, string endpoint)
        {
            var url = SwiftUrlBuilder.GetAuthUrl(endpoint);

            var request = new HttpRequestMessage(HttpMethod.Get, url);

            FillRequest(request, null, new Dictionary <string, string>
            {
                { SwiftHeaderKeys.AuthUser, username },
                { SwiftHeaderKeys.AuthKey, password }
            });

            try
            {
                using (var response = await _client.SendAsync(request).ConfigureAwait(false))
                {
                    response.EnsureSuccessStatusCode();

                    return(new SwiftAuthData
                    {
                        AuthToken = response.GetHeader(SwiftHeaderKeys.AuthToken),
                        StorageUrl = response.GetHeader(SwiftHeaderKeys.StorageUrl)
                    });
                }
            }
            catch (Exception ex)
            {
                if (_logger != null)
                {
                    _logger.LogAuthenticationError(ex, username, password, endpoint);
                }

                return(null);
            }
        }
Пример #4
0
        public Task <SwiftAccountResponse> GetAccount(Dictionary <string, string> queryParams = null)
        {
            return(AuthorizeAndExecute(async(auth) =>
            {
                if (queryParams == null)
                {
                    queryParams = new Dictionary <string, string>();
                }

                queryParams["format"] = "json";

                var url = SwiftUrlBuilder.GetAccountUrl(auth.StorageUrl, queryParams);

                var request = new HttpRequestMessage(HttpMethod.Get, url);

                FillRequest(request, auth);

                try
                {
                    using (var response = await _client.SendAsync(request).ConfigureAwait(false))
                    {
                        var result = GetResponse <SwiftAccountResponse>(response);

                        if (response.IsSuccessStatusCode)
                        {
                            long totalBytes, containersCount, objectsCount;

                            if (long.TryParse(response.GetHeader(SwiftHeaderKeys.AccountBytesUsed), out totalBytes))
                            {
                                result.TotalBytes = totalBytes;
                            }

                            if (long.TryParse(response.GetHeader(SwiftHeaderKeys.AccountContainerCount), out containersCount))
                            {
                                result.ContainersCount = containersCount;
                            }

                            if (long.TryParse(response.GetHeader(SwiftHeaderKeys.AccountObjectCount), out objectsCount))
                            {
                                result.ObjectsCount = objectsCount;
                            }

                            var info = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                            if (!string.IsNullOrEmpty(info))
                            {
                                result.Containers = JsonConvert.DeserializeObject <List <SwiftContainerModel> >(info);
                            }
                        }

                        return result;
                    }
                }
                catch (Exception ex)
                {
                    return GetExceptionResponse <SwiftAccountResponse>(ex, auth.StorageUrl);
                }
            }));
        }
Пример #5
0
        public Task <SwiftAccountResponse> HeadAccount()
        {
            return(AuthorizeAndExecute(async(auth) =>
            {
                var url = SwiftUrlBuilder.GetAccountUrl(auth.StorageUrl);

                var request = new HttpRequestMessage(HttpMethod.Head, url);

                FillRequest(request, auth);

                try
                {
                    using (var response = await _client.SendAsync(request).ConfigureAwait(false))
                    {
                        var result = GetResponse <SwiftAccountResponse>(response);

                        if (response.IsSuccessStatusCode)
                        {
                            long totalBytes, containersCount, objectsCount;

                            if (long.TryParse(response.GetHeader(SwiftHeaderKeys.AccountBytesUsed), out totalBytes))
                            {
                                result.TotalBytes = totalBytes;
                            }

                            if (long.TryParse(response.GetHeader(SwiftHeaderKeys.AccountContainerCount), out containersCount))
                            {
                                result.ContainersCount = containersCount;
                            }

                            if (long.TryParse(response.GetHeader(SwiftHeaderKeys.AccountObjectCount), out objectsCount))
                            {
                                result.ObjectsCount = objectsCount;
                            }
                        }

                        return result;
                    }
                }
                catch (Exception ex)
                {
                    return GetExceptionResponse <SwiftAccountResponse>(ex, auth.StorageUrl);
                }
            }));
        }
Пример #6
0
        public Task <SwiftResponse> PostObject(string containerId, string objectId, Dictionary <string, string> headers = null)
        {
            return(AuthorizeAndExecute(async(auth) =>
            {
                var url = SwiftUrlBuilder.GetObjectUrl(auth.StorageUrl, containerId, objectId);

                var request = new HttpRequestMessage(HttpMethod.Post, url);

                FillRequest(request, auth, headers);

                try
                {
                    using (var response = await _client.SendAsync(request).ConfigureAwait(false))
                    {
                        return GetResponse <SwiftResponse>(response);
                    }
                }
                catch (Exception ex)
                {
                    return GetExceptionResponse <SwiftResponse>(ex, url);
                }
            }));
        }
Пример #7
0
 /// <summary>
 /// Delete object chunk.
 /// Unfortunately no api support for DLO delete ([filter:dlo]).
 /// Deleting the manifest file won't delete the object segments.
 /// </summary>
 /// <param name="containerId"></param>
 /// <param name="objectId"></param>
 /// <param name="segment"></param>
 /// <returns></returns>
 public Task <SwiftResponse> DeleteObjectChunk(string containerId, string objectId, int segment)
 {
     return(DeleteObject(containerId, SwiftUrlBuilder.GetObjectChunkId(objectId, segment)));
 }
Пример #8
0
 public Task <SwiftResponse> PutObjectChunk(string containerId, string objectId, byte[] data, int segment, Dictionary <string, string> headers = null, Dictionary <string, string> queryParams = null)
 {
     return(PutObject(containerId, SwiftUrlBuilder.GetObjectChunkId(objectId, segment), data, headers, queryParams));
 }