private AWSCredentials GetAwsCredentials(AWSS3Labs.Web.Configuration.AmazonS3Config config)
        {
            if (!string.IsNullOrWhiteSpace(config.Profile))
            {
                var credentialProfileStoreChain = new CredentialProfileStoreChain();

                if (credentialProfileStoreChain.TryGetAWSCredentials(config.Profile, out AWSCredentials profileCredentials))
                {
                    return(profileCredentials);
                }
                else
                {
                    throw new AmazonClientException($"Failed to find AWS credentials for the profile {config.Profile}");
                }
            }

            if (!string.IsNullOrEmpty(config.AccessKeyId) && !string.IsNullOrWhiteSpace(config.SecretAccessKey))
            {
                return(new BasicAWSCredentials(config.AccessKeyId, config.SecretAccessKey));
            }

            var credentials = FallbackCredentialsFactory.GetCredentials();

            if (credentials == null)
            {
                throw new AmazonClientException("Failed to find AWS Credentials for constructing AWS service client");
            }

            return(credentials);
        }
        private IAmazonS3 GetAmazonS3Client(AWSS3Labs.Web.Configuration.AmazonS3Config config)
        {
            var credentials = GetAwsCredentials(config);

            RegionEndpoint region = null;

            if (!string.IsNullOrEmpty(config.Region))
            {
                region = RegionEndpoint.GetBySystemName(config.Region);

                if (region.DisplayName == "Unknown")
                {
                    region = FallbackRegionFactory.GetRegionEndpoint();
                }
            }


            if (string.IsNullOrEmpty(_serviceUrl))
            {
                return(new AmazonS3Client(credentials, region));
            }
            else
            {
                var s3Config = new Amazon.S3.AmazonS3Config
                {
                    ServiceURL     = _serviceUrl,
                    RegionEndpoint = region
                };

                return(new AmazonS3Client(credentials, s3Config));
            }
        }
        public AmazonS3Service(AWSS3Labs.Web.Configuration.AmazonS3Config config)
        {
            _config = config;

            _bucketName = config.BucketName;
            //_serviceUrl = config.ServiceUrl;
            _serviceUrl = string.IsNullOrEmpty(config.ServiceUrl) ? AmazonS3Defaults.DefaultServiceUrl : config.ServiceUrl;
            _serverSideEncryptionMethod = string.IsNullOrEmpty(config.ServiceUrl) ? AmazonS3Defaults.ServerSideEncryptionMethod : config.ServerSideEncryptionMethod;
            _useChunkEncoding           = true;
            _cannedAcl = S3CannedACL.Private;  // TODO: Convert it properly

            _client = GetAmazonS3Client(_config);
        }