Пример #1
0
        public async Task <AccessControlList> GetBucketAcl(string bucketName)
        {
            AccessControlList     result             = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("acl", null);
                httpRequestMessage = new OssHttpRequestMessage(bucketName, null, parameters);

                httpRequestMessage.Method       = HttpMethod.Get;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
                var temp = DeserializerFactory.GetFactory().CreateGetAclResultDeserializer();
                result = await temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
            return(result);
        }
Пример #2
0
        public async Task <ObjectMetadata> GetObjectMetadata(string bucketName, string key)
        {
            ObjectMetadata        result             = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                httpRequestMessage = new OssHttpRequestMessage(bucketName, key);

                httpRequestMessage.Method       = HttpMethod.Head;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }

                var temp = DeserializerFactory.GetFactory().CreateGetObjectMetadataResultDeserializer();
                result = temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
            return(result);
        }
Пример #3
0
        /// <inheritdoc />
        public async Task <QueryResponse> QueryEmailAsync(string emailAddress, CancellationToken cancellationToken = default)
        {
            // validate the email address; this is basic checking as many different opinions on how to validate email addresses
            // and none of them are bullet proof. I am keeping away from the regex route as there is no perfect catch all one.
            if (string.IsNullOrWhiteSpace(emailAddress))
            {
                throw new EmailRepException("Invalid email address. Email must be valid.");
            }

            // make the request and handle the response if error returned
            var response = await _httpClient.GetAsync($"{emailAddress}", cancellationToken);

            await ErrorResponseHandler.HandleResponse(response);

            // if we've got here then we have a valid response and we can read and map accordingly
            var source = await response.Content.ReadAsAsync <Internal.QueryResponse>();

            // map and push out the door
            return(await QueryResponseMapper.MapAsync(source));
        }
Пример #4
0
        public async Task DeleteMultipartUpload(MultiUploadRequestData multiUploadObject)
        {
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("partNumber", multiUploadObject.PartNumber);
                parameters.Add("uploadId", multiUploadObject.UploadId);

                httpRequestMessage = new OssHttpRequestMessage(multiUploadObject.Bucket, multiUploadObject.Key, parameters);

                httpRequestMessage.Method       = HttpMethod.Delete;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
        }
Пример #5
0
        public async Task SetBucketAcl(string bucketName, CannedAccessControlList acl)
        {
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("acl", null);
                httpRequestMessage = new OssHttpRequestMessage(bucketName, null, parameters);

                httpRequestMessage.Method       = HttpMethod.Put;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;
                httpRequestMessage.Headers.Add("x-oss-acl", acl.GetStringValue());
                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
        }
Пример #6
0
        public async Task DeleteBucket(string bucketName)
        {
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage   response           = null;

            try
            {
                httpRequestMessage = new OssHttpRequestMessage(bucketName, null);

                httpRequestMessage.Method       = HttpMethod.Delete;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
        }
Пример #7
0
        public async Task SendMessageAsync(Message msg)
        {
            await _networkCommunication.SendBytesAsync(new[] { (byte)msg.Type });

            await _networkCommunication.SendShortAsync((short)msg.Id);

            switch (msg)
            {
            // Error
            case ErrorResponse errorResponse:
                var errorResponseHandler = new ErrorResponseHandler(_networkCommunication);
                await errorResponseHandler.SendMessageAsync(errorResponse);

                break;

            // Login
            case LoginRequest loginRequest:
                var loginRequestHandler = new LoginRequestHandler(_networkCommunication);
                await loginRequestHandler.SendMessageAsync(loginRequest);

                break;

            case LoginResponse loginResponse:
                var loginResponseHandler = new LoginResponseHandler(_networkCommunication);
                await loginResponseHandler.SendMessageAsync(loginResponse);

                break;

            // Create User
            case CreateUserRequest createUserRequest:
                var createUserRequestHandler = new CreateUserRequestHandler(_networkCommunication);
                await createUserRequestHandler.SendMessageAsync(createUserRequest);

                break;

            case CreateUserResponse createUserResponse:
                var createUserResponseHandler = new CreateUserResponseHandler(_networkCommunication);
                await createUserResponseHandler.SendMessageAsync(createUserResponse);

                break;

            // Create photo
            case CreatePhotoRequest createPhotoRequest:
                var createPhotoRequestHandler = new CreatePhotoRequestHandler(
                    _networkCommunication,
                    _fileCommunication
                    );
                await createPhotoRequestHandler.SendMessageAsync(createPhotoRequest);

                break;

            case CreatePhotoResponse createPhotoResponse:
                var createPhotoResponseHandler = new CreatePhotoResponseHandler();
                await createPhotoResponseHandler.SendMessageAsync(createPhotoResponse);

                break;

            // Photo list
            case PhotoListRequest photoListRequest:
                var photoListRequestHandler = new PhotoListRequestHandler(_networkCommunication);
                await photoListRequestHandler.SendMessageAsync(photoListRequest);

                break;

            case PhotoListResponse photoListResponse:
                var photoListResponseHandler = new PhotoListResponseHandler(
                    _networkCommunication,
                    _fileCommunication
                    );
                await photoListResponseHandler.SendMessageAsync(photoListResponse);

                break;

            // User list
            case UserListRequest userListRequest:
                var userListRequestHandler = new UserListRequestHandler();
                await userListRequestHandler.SendMessageAsync(userListRequest);

                break;

            case UserListResponse userListResponse:
                var userListResponseHandler = new UserListResponseHandler(
                    _networkCommunication
                    );
                await userListResponseHandler.SendMessageAsync(userListResponse);

                break;

            // Comment photo
            case CommentPhotoRequest commentPhotoRequest:
                var commentPhotoRequestHandler = new CommentPhotoRequestHandler(_networkCommunication);
                await commentPhotoRequestHandler.SendMessageAsync(commentPhotoRequest);

                break;

            case CommentPhotoResponse commentPhotoResponse:
                var commentPhotoResponseHandler = new CommentPhotoResponseHandler(
                    _networkCommunication
                    );
                await commentPhotoResponseHandler.SendMessageAsync(commentPhotoResponse);

                break;

            // Comment List
            case CommentListRequest commentListRequest:
                var commentListRequestHandler = new CommentListRequestHandler(_networkCommunication);
                await commentListRequestHandler.SendMessageAsync(commentListRequest);

                break;

            case CommentListResponse commentListResponse:
                var commentListResponseHandler = new CommentListResponseHandler(
                    _networkCommunication
                    );
                await commentListResponseHandler.SendMessageAsync(commentListResponse);

                break;

            // Logout
            case LogoutRequest logoutRequest:
                var logoutRequestHandler = new LogoutRequestHandler();
                await logoutRequestHandler.SendMessageAsync(logoutRequest);

                break;

            case LogoutResponse logoutResponse:
                var logoutResponseHandler = new LogoutResponseHandler();
                await logoutResponseHandler.SendMessageAsync(logoutResponse);

                break;

            default:
                // TODO Create a custom exception
                throw new Exception($"Message not recognized ID={msg.Id}, type={msg.Type}");
            }
        }
Пример #8
0
        public async Task <MultipartUploadResult> MultipartUpload(MultiUploadRequestData multiUploadObject,
                                                                  Action <HttpProcessData> uploadProcessCallback = null, CancellationToken?cancellationToken = null)
        {
            MultipartUploadResult result = null;

            HttpClientHandler      hand = null;
            ProgressMessageHandler processMessageHander = null;
            HttpClient             localHttpClient      = null;
            OssHttpRequestMessage  httpRequestMessage   = null;
            HttpResponseMessage    response             = null;

            try
            {
                hand = new HttpClientHandler();
                processMessageHander     = new ProgressMessageHandler(hand);
                localHttpClient          = new HttpClient(processMessageHander);
                localHttpClient.Timeout += new TimeSpan(2 * TimeSpan.TicksPerHour);

                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("partNumber", multiUploadObject.PartNumber);
                parameters.Add("uploadId", multiUploadObject.UploadId);

                httpRequestMessage = new OssHttpRequestMessage(multiUploadObject.Bucket, multiUploadObject.Key, parameters);

                httpRequestMessage.Method       = HttpMethod.Put;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;
                httpRequestMessage.Content      = new StreamContent(multiUploadObject.Content);

                if (uploadProcessCallback != null)
                {
                    processMessageHander.HttpSendProgress += (sender, e) =>
                    {
                        uploadProcessCallback(new HttpProcessData()
                        {
                            TotalBytes         = e.TotalBytes,
                            BytesTransferred   = e.BytesTransferred,
                            ProgressPercentage = e.ProgressPercentage
                        });
                    };
                }


                OssRequestSigner.Sign(httpRequestMessage, networkCredential);

                if (cancellationToken != null)
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage, (CancellationToken)cancellationToken);
                }
                else
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage);
                }

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
                var deseserializer = DeserializerFactory.GetFactory().CreateMultipartUploadDeserializer();
                result = deseserializer.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (hand != null)
                {
                    hand.Dispose();
                }

                if (processMessageHander != null)
                {
                    processMessageHander.Dispose();
                }

                if (localHttpClient != null)
                {
                    localHttpClient.Dispose();
                }

                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }


                if (response != null)
                {
                    response.Dispose();
                }
            }
            return(result);
        }
Пример #9
0
        public async Task <OssObject> GetObject(GetObjectRequest getObjectRequest,
                                                Action <HttpProcessData> downloadProcessCallback = null, CancellationToken?cancellationToken = null)
        {
            OssObject result = null;

            HttpClientHandler      hand = null;
            ProgressMessageHandler processMessageHander = null;
            HttpClient             localHttpClient      = null;
            OssHttpRequestMessage  httpRequestMessage   = null;
            HttpResponseMessage    response             = null;

            try
            {
                hand = new HttpClientHandler();
                processMessageHander     = new ProgressMessageHandler(hand);
                localHttpClient          = new HttpClient(processMessageHander);
                localHttpClient.Timeout += new TimeSpan(2 * TimeSpan.TicksPerHour);

                httpRequestMessage = new OssHttpRequestMessage(getObjectRequest.BucketName, getObjectRequest.Key);
                getObjectRequest.ResponseHeaders.Populate(httpRequestMessage.Headers);
                getObjectRequest.Populate(httpRequestMessage.Headers);

                httpRequestMessage.Method       = HttpMethod.Get;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                if (downloadProcessCallback != null)
                {
                    processMessageHander.HttpReceiveProgress += (sender, e) =>
                    {
                        downloadProcessCallback(new HttpProcessData()
                        {
                            TotalBytes         = e.TotalBytes,
                            BytesTransferred   = e.BytesTransferred,
                            ProgressPercentage = e.ProgressPercentage
                        });;
                    };
                }

                if (cancellationToken != null)
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage, (CancellationToken)cancellationToken);
                }
                else
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage);
                }


                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }

                var temp = DeserializerFactory.GetFactory().CreateGetObjectResultDeserializer(getObjectRequest);
                result = await temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (hand != null)
                {
                    hand.Dispose();
                }

                if (processMessageHander != null)
                {
                    processMessageHander.Dispose();
                }

                if (localHttpClient != null)
                {
                    localHttpClient.Dispose();
                }

                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }
            }


            return(result);
        }
Пример #10
0
        public async Task <PutObjectResult> PutObject(string bucketName, string key, Stream content, ObjectMetadata metadata,
                                                      Action <HttpProcessData> uploadProcessCallback = null, CancellationToken?cancellationToken = null)
        {
            PutObjectResult        result = null;
            HttpClientHandler      hand   = null;
            ProgressMessageHandler processMessageHander = null;
            HttpClient             localHttpClient      = null;
            OssHttpRequestMessage  httpRequestMessage   = null;
            HttpResponseMessage    response             = null;

            try
            {
                hand = new HttpClientHandler();
                processMessageHander     = new ProgressMessageHandler(hand);
                localHttpClient          = new HttpClient(processMessageHander);
                localHttpClient.Timeout += new TimeSpan(2 * TimeSpan.TicksPerHour);
                httpRequestMessage       = new OssHttpRequestMessage(bucketName, key);



                httpRequestMessage.Method       = HttpMethod.Put;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;
                httpRequestMessage.Content      = new StreamContent(content);


                OssClientHelper.initialHttpRequestMessage(httpRequestMessage, metadata);



                OssRequestSigner.Sign(httpRequestMessage, networkCredential);

                if (uploadProcessCallback != null)
                {
                    processMessageHander.HttpSendProgress += (sender, e) =>
                    {
                        uploadProcessCallback(new HttpProcessData()
                        {
                            TotalBytes         = e.TotalBytes,
                            BytesTransferred   = e.BytesTransferred,
                            ProgressPercentage = e.ProgressPercentage
                        });
                    };
                }


                if (cancellationToken != null)
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage, (CancellationToken)cancellationToken);
                }
                else
                {
                    response = await localHttpClient.SendAsync(httpRequestMessage);
                }

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }

                var temp = DeserializerFactory.GetFactory().CreatePutObjectReusltDeserializer();
                result = temp.Deserialize(response);
                //localHttpClient.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (hand != null)
                {
                    hand.Dispose();
                }

                if (processMessageHander != null)
                {
                    processMessageHander.Dispose();
                }

                if (localHttpClient != null)
                {
                    localHttpClient.Dispose();
                }

                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }


                if (response != null)
                {
                    response.Dispose();
                }
            }

            return(result);
        }