This class contains the endpoints available to the AWS clients. The static constants representing the regions can be used while constructing the AWS client instead of looking up the exact endpoint URL.
Example #1
0
        static void GetPreSigUrl()
        {
            String fileKey = "test-txt.log";

            Amazon.Runtime.BasicAWSCredentials cred = new Amazon.Runtime.BasicAWSCredentials("61140DEQBD8L2QSRATPS", "1oZedqSsM2DLjer2VvZ74ACpn998TamNbz4LEURN");
            Amazon.RegionEndpoint endpoint          = Amazon.RegionEndpoint.APNortheast1;


            //首先创建一个s3的客户端操作对象(需要amazon提供的密钥)
            AmazonS3Client s3 = new AmazonS3Client(cred, endpoint);

            try
            {
                // 验证名称为bucketName的bucket是否存在,不存在则创建

                Amazon.S3.Model.GetPreSignedUrlRequest urlRequest = new Amazon.S3.Model.GetPreSignedUrlRequest();
                urlRequest.BucketName = bucketName;
                urlRequest.Key        = fileKey;
                urlRequest.Protocol   = Protocol.HTTP;
                urlRequest.Verb       = HttpVerb.GET;
                urlRequest.Expires    = DateTime.Now.AddHours(12); // ;.Now + dtSpan; //new TimeSpan(12,0,0); //12小时有效期
                String strSinnedURL = s3.GetPreSignedURL(urlRequest);
                Console.WriteLine(strSinnedURL);
            }
            catch (Amazon.S3.AmazonS3Exception e)
            {
                Console.WriteLine("GenerateFileSignURL AmazonS3Exception:" + e.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("GenerateFileSignURL Exception:" + e.ToString());
            }
        }
 public AmazonS3Region(RegionEndpoint region)
 {
     Name = region.DisplayName;
     Identifier = region.SystemName;
     AmazonRegion = region;
     Hostname = region.GetEndpointForService("s3").Hostname;
 }
Example #3
0
 private DynamoDBConnection(RegionEndpoint endpoint = null)
 {
     if (endpoint == null) {
         endpoint = RegionEndpoint.USEast1;
     }
     client = new AmazonDynamoDBClient(endpoint);
 }
Example #4
0
    public static void init()
    {
        String identityPoolId = "YOUR_IDENTITY_POOL_ID";         //Set your Identity Pool here

        Amazon.RegionEndpoint region = Amazon.RegionEndpoint.USEast1;

        if (String.IsNullOrEmpty(identityPoolId))
        {
            throw new Exception("You need to set up an identity pool in CognitoSyncClientManager.cs");
        }

        // CognitoAWSCredentials is recommended in the place of Developer credentials{accessKey,secretKey} for reasons of security best practices
        // CognitoAWSCredentials uses the CognitoIdentityProvider & provides temporary scoped AWS credentials from AssumeRoleWithWebIdentity
        // ref: http://mobile.awsblog.com/post/TxR1UCU80YEJJZ/Using-the-Amazon-Cognito-Credentials-Provider

#if DEVELOPER_AUTHENTICATED_IDENTITIES
        // or to use developer authenticated identities
        CognitoAWSCredentials _credentials = new CognitoAWSCredentials(new ExampleIdentityProvider());
#else
        // Ref: http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/cognito-auth.html#create-an-identity-pool
        // for setting up Cognito Identity Pools, can you use the sample code for .NET SDK
        _credentials = new CognitoAWSCredentials(identityPoolId, region);
#endif
        // DefaultCognitoSyncManager is a high level CognitoSync Client which handles all Sync operations at a Dataset level.
        // Additionally, it also provides local storage of the Datasets which can be later Synchronized with the cloud(CognitoSync service)
        // This feature allows the user to continue working w/o internet access and sync with CognitoSync whenever possible
        _syncManager = new DefaultCognitoSyncManager(_credentials, new AmazonCognitoSyncConfig {
            RegionEndpoint = region
        });
    }
Example #5
0
        /// <summary>
        /// Constructs the transport with the specified settings
        /// </summary>
        public AmazonSqsTransport(string inputQueueAddress, string accessKeyId, string secretAccessKey, RegionEndpoint regionEndpoint, IRebusLoggerFactory rebusLoggerFactory)
        {
            if (accessKeyId == null) throw new ArgumentNullException("accessKeyId");
            if (secretAccessKey == null) throw new ArgumentNullException("secretAccessKey");
            if (regionEndpoint == null) throw new ArgumentNullException("regionEndpoint");
            if (rebusLoggerFactory == null) throw new ArgumentNullException("rebusLoggerFactory");

            _inputQueueAddress = inputQueueAddress;
            _log = rebusLoggerFactory.GetCurrentClassLogger();
            
            if (_inputQueueAddress != null)
            {
                if (_inputQueueAddress.Contains("/") && !Uri.IsWellFormedUriString(_inputQueueAddress, UriKind.Absolute))
                {
                    throw new ArgumentException(
                        "You could either have a simple queue name without slash (eg. \"inputqueue\") - or a complete URL for the queue endpoint. (eg. \"https://sqs.eu-central-1.amazonaws.com/234234234234234/somqueue\")",
                        "inputQueueAddress");
                }
            }

            _accessKeyId = accessKeyId;
            _secretAccessKey = secretAccessKey;
            _regionEndpoint = regionEndpoint;
            _rebusLoggerFactory = rebusLoggerFactory;
        }
Example #6
0
        public static void DeleteStack(RegionEndpoint awsEndpoint, string stackName)
        {
            var codeDeployClient = new AmazonCodeDeployClient(awsEndpoint);
            var apps = codeDeployClient.ListApplications().Applications.Where(name => name.StartsWith("HelloWorld"));
            foreach (var app in apps) {
                codeDeployClient.DeleteApplication(new DeleteApplicationRequest {ApplicationName = app});
            }

            var cloudFormationClient = new AmazonCloudFormationClient(awsEndpoint);
            try
            {
                cloudFormationClient.DeleteStack(new DeleteStackRequest { StackName = stackName });
                var testStackStatus = StackStatus.DELETE_IN_PROGRESS;
                while (testStackStatus == StackStatus.DELETE_IN_PROGRESS)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(10));
                    var stacksStatus =
                        cloudFormationClient.DescribeStacks(new DescribeStacksRequest { StackName = stackName });
                    testStackStatus = stacksStatus.Stacks.First(s => s.StackName == stackName).StackStatus;
                }
            }
            catch (AmazonCloudFormationException)
            {
            }
        }
Example #7
0
		/// <summary>
		/// Initializes a new instance of the <see cref="AmazonGlacierService"/>.
		/// </summary>
		/// <param name="endpoint">Region address.</param>
		/// <param name="vaultName">Storage name.</param>
		/// <param name="accessKey">Key.</param>
		/// <param name="secretKey">Secret.</param>
		public AmazonGlacierService(RegionEndpoint endpoint, string vaultName, string accessKey, string secretKey)
		{
			if (vaultName.IsEmpty())
				throw new ArgumentNullException("vaultName");

			_client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey), endpoint);
			_vaultName = vaultName;
		}
Example #8
0
		private const int _bufferSize = 1024 * 1024 * 10; // 10mb

		/// <summary>
		/// Initializes a new instance of the <see cref="AmazonS3Service"/>.
		/// </summary>
		/// <param name="endpoint">Region address.</param>
		/// <param name="bucket">Storage name.</param>
		/// <param name="accessKey">Key.</param>
		/// <param name="secretKey">Secret.</param>
		public AmazonS3Service(RegionEndpoint endpoint, string bucket, string accessKey, string secretKey)
		{
			if (bucket.IsEmpty())
				throw new ArgumentNullException(nameof(bucket));

			_client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey), endpoint);
			_bucket = bucket;
		}
Example #9
0
 public FastEmail(Amazon.RegionEndpoint region)
 {
     if (region != null)
     {
         endpoint = region;
     }
     Console.WriteLine($"endpoint: {endpoint}");
     init();
 }
 public static AmazonEC2Client CreateClient(RegionEndpoint region)
 {
     var key = ConfigurationManager.AppSettings["AWSAccessKey"];
     var secretkey = ConfigurationManager.AppSettings["AWSSecretKey"];
     if (!string.IsNullOrEmpty(key))
         return new AmazonEC2Client(key, secretkey, region);
     else
         return new AmazonEC2Client();
 }
            //public CloudAuthService(string appPoolID, string awsRegion):
            //    this(appPoolID, Amazon.RegionEndpoint.GetBySystemName(awsRegion)){ }

            public CloudAuthService(IOptions <AWSOptions> awsOptions)
            {
                _awsOptions = awsOptions.Value;
                _awsRegion  = Amazon.RegionEndpoint.GetBySystemName(
                    _awsOptions.Region);
                if (!InitService())
                {
                    throw new AmazonCognitoIdentityException("Couldn't connect to Cognito Service", new Exception("InitService - CognitoClient"));
                }
            }
        /// <summary>
        /// Specifies the endpoints available to AWS clients.
        /// </summary>
        /// <param name="settings">The CloudFront settings.</param>
        /// <param name="region">The endpoints available to AWS clients.</param>
        /// <returns>The same <see cref="CloudFrontSettings"/> instance so that multiple calls can be chained.</returns>
        public static CloudFrontSettings SetRegion(this CloudFrontSettings settings, RegionEndpoint region)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            settings.Region = region;
            return settings;
        }
        /// <summary>
        /// Specifies the endpoints available to AWS clients.
        /// </summary>
        /// <param name="settings">The LoadBalancing settings.</param>
        /// <param name="region">The endpoints available to AWS clients.</param>
        /// <returns>The same <see cref="LoadBalancingSettings"/> instance so that multiple calls can be chained.</returns>
        public static LoadBalancingSettings SetRegion(this LoadBalancingSettings settings, RegionEndpoint region)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            settings.Region = region;
            return settings;
        }
        /// <summary>
        /// Configures Rebus to use Amazon Simple Queue Service as the message transport
        /// </summary>
        public static void UseAmazonSqsAsOneWayClient(this StandardConfigurer<ITransport> configurer, string accessKeyId, string secretAccessKey, RegionEndpoint regionEndpoint)
        {
            configurer.Register(c =>
            {
                var rebusLoggerFactory = c.Get<IRebusLoggerFactory>();
                var asyncTaskFactory = c.Get<IAsyncTaskFactory>();

                return new AmazonSqsTransport(null, accessKeyId, secretAccessKey, regionEndpoint, rebusLoggerFactory, asyncTaskFactory);
            });

            OneWayClientBackdoor.ConfigureOneWayClient(configurer);
        }
Example #15
0
        /// <summary>
        /// This function creates a set of instances into EC2 Classic. It returns the Ids of the created instances if successful, or 
        /// sets the error code and message otherwise
        /// </summary>
        /// <param name="regionEndpoint">Region where instances should be created</param>
        /// <param name="AMI_ID">Id of the AMI that will be used as a base for the instances</param>
        /// <param name="SecurityGroupId">The name of the security group to be assigned to the instance(s)</param>
        /// <param name="KeyPairName">The name of the keypair to be assigned to the instance(s)</param>
        /// <param name="InstanceType">The type of the instance(s)</param>
        /// <param name="InstanceCount">The number of instances to be launched</param>
        /// <param name="UserData">The user-data script that will be run as the instance(s) is(are) initialized</param>
        /// <returns>The list of Instance Ids if successful</returns>
        public List<string> CreateClassicInstances(RegionEndpoint regionEndpoint, string AMI_ID, string SecurityGroupId, string KeyPairName, string InstanceType, int InstanceCount = 1, string UserData = "")
        {
            List<string> InstanceIds = new List<string> ();

            // Initialize error values
            ErrorCode    = 0;
            ErrorMessage = "";

            // Create the request object
            List<string> groups = new List<string> () { SecurityGroupId };
            var launchRequest = new RunInstancesRequest ()
            {
                ImageId          = AMI_ID,
                InstanceType     = InstanceType,
                MinCount         = InstanceCount,
                MaxCount         = InstanceCount,
                KeyName          = KeyPairName,
                SecurityGroupIds = groups,
                UserData         = Gadgets.Base64Encode (UserData)
            };

            // Launch the instances
            try
            {
                var launchResponse = EC2client.RunInstances (launchRequest);

                // Check response for errors
                if (launchResponse.HttpStatusCode != HttpStatusCode.OK)
                {
                    ErrorCode = Convert.ToInt32 (launchResponse.HttpStatusCode);
                    ErrorMessage = "Http Error [" + launchResponse.HttpStatusCode.ToString () + "]";
                }
                else
                {
                    List<Instance> createdInstances = launchResponse.Reservation.Instances;
                    foreach (Instance instance in createdInstances)
                    {
                        InstanceIds.Add (instance.InstanceId);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorCode    = -1;
                ErrorMessage = ex.Message + "::" + ex.InnerException;
            }

            return InstanceIds;
        }
        public IAmazonSimpleNotificationService GetSnsClient(RegionEndpoint region)
        {
            var innerClient = CreateMeABus.DefaultClientFactory().GetSnsClient(region);
            var client = Substitute.For<IAmazonSimpleNotificationService>();

            client.CreateTopic(Arg.Any<CreateTopicRequest>())
                .ReturnsForAnyArgs(r => innerClient.CreateTopic(r.Arg<CreateTopicRequest>()))
                .AndDoes(r => Increment("CreateTopic", r.Arg<CreateTopicRequest>().Name, r.Arg<CreateTopicRequest>()));

            client.FindTopic(Arg.Any<string>())
                .ReturnsForAnyArgs(r => innerClient.FindTopic(r.Arg<string>()))
                .AndDoes(r => Increment("FindTopic", r.Arg<string>(), r.Arg<string>()));

            return client;
        }
Example #17
0
        public static AmazonS3Client GetApi(AWSCredentials credentials, RegionEndpoint region)
        {
            if (credentials == null) throw new ArgumentNullException("credentials");
            if (region == null) throw new ArgumentNullException("region");

            var config = new AmazonS3Config
            {
                RegionEndpoint = region,
            };

            ApplyProxy(config);

            var api = new AmazonS3Client(credentials, config);
            return api;
        }
Example #18
0
        public static List<SecurityGroup> GetAllSecurityGroups(AWSEnvironment environment, RegionEndpoint region)
        {
            try
            {
                var ec2Client = AWSClientFactory.CreateAmazonEC2Client(environment.AccessKeyID, environment.SecretAccessKey, region);
                var regionSecurityGroups = ec2Client.DescribeSecurityGroups();
                return regionSecurityGroups.SecurityGroups;
            }
            catch (AmazonEC2Exception aex)
            {
                Logger.Log(LogLevel.Error, aex, $"AmazonEC2Exception in GetAllSecurityGroups() : {aex.Message}");
            }

            return null;
        }
        protected override void Given()
        {
            _topicName = "message";
            _queueName = "queue" + DateTime.Now.Ticks;
            _regionEndpoint = RegionEndpoint.SAEast1;

            EnableMockedBus();

            Configuration = new MessagingConfig();

            TestEndpoint = _regionEndpoint;

            DeleteQueueIfItAlreadyExists(_regionEndpoint, _queueName);
            DeleteTopicIfItAlreadyExists(_regionEndpoint, _topicName);
        }
        private SnsTopicByName EnsureTopicExists(RegionEndpoint region, IMessageSerialisationRegister serialisationRegister, SqsReadConfiguration queueConfig)
        {
            var snsclient = _awsClientFactory.GetAwsClientFactory().GetSnsClient(region);

            var eventTopic = _topicCache.TryGetFromCache(region.SystemName, queueConfig.PublishEndpoint);
            if (eventTopic != null)
                return eventTopic;

            eventTopic = new SnsTopicByName(queueConfig.PublishEndpoint, snsclient, serialisationRegister);
            _topicCache.AddToCache(region.SystemName, queueConfig.PublishEndpoint, eventTopic);

            if (!eventTopic.Exists())
                eventTopic.Create();

            return eventTopic;
        }
Example #21
0
        public static List<AWSInstance> GetAllRDSInstances(AWSEnvironment environment, RegionEndpoint region)
        {
            var dbInstances = new List<AWSInstance>();

            try
            {
                var rdsClient = AWSClientFactory.CreateAmazonRDSClient(environment.AccessKeyID, environment.SecretAccessKey, region);
                var rdsInstances = rdsClient.DescribeDBInstances();
                foreach (var dbInstance in rdsInstances.DBInstances)
                {
                    var rdsInstance = new AWSInstance
                    {
                        Role = dbInstance.Engine.ToUpper(),
                        Name = dbInstance.DBInstanceIdentifier,
                        InstanceType = dbInstance.DBInstanceClass,
                        PubliclyAccessible = dbInstance.PubliclyAccessible,
                        Encrypted = dbInstance.StorageEncrypted,
                        Platform = Platforms.Linux,
                        State = States.Running,
                        IsRDS = true
                    };

                    if (rdsInstance.Role == "MSSQL")
                    {
                        rdsInstance.Platform = Platforms.Windows;
                    }

                    foreach (var subnet in dbInstance.DBSubnetGroup.Subnets)
                    {
                        rdsInstance.SubnetIDs.Add(subnet.SubnetIdentifier);
                    }

                    foreach (var vpcSecurityGroup in dbInstance.VpcSecurityGroups)
                    {
                        rdsInstance.SecurityGroupIDs.Add(vpcSecurityGroup.VpcSecurityGroupId);
                    }

                    dbInstances.Add(rdsInstance);
                }
            }
            catch (AmazonEC2Exception aex)
            {
                Logger.Log(LogLevel.Error, aex, $"AmazonEC2Exception in GetAllRDSInstances() : {aex.Message}");
            }

            return dbInstances;
        }
Example #22
0
    // Called when Start game button is clicked
    void StartGame()
    {
        if (!this.gameStartRequested)
        {
            this.startGameButton.gameObject.SetActive(false);
            this.gameStartRequested = true;

            FindObjectOfType <UIManager>().SetTextBox("Setting up Client..");

            // Get the Region enum from the string value
            this.region = Amazon.RegionEndpoint.GetBySystemName(regionString);
            Debug.Log("My Region endpoint: " + this.region);

            // Check if we have stored an identity and request credentials for that existing identity
            Client.cognitoID = PlayerPrefs.GetString("CognitoID", null);
            if (Client.cognitoID != null && Client.cognitoID != "")
            {
                Debug.Log("Requesting credentials for existing identity: " + Client.cognitoID);
                var response = Task.Run(() => GetCredentialsForExistingIdentity(Client.cognitoID));
                response.Wait(5000);
                Client.cognitoID          = response.Result.IdentityId;
                Client.cognitoCredentials = new Amazon.Runtime.ImmutableCredentials(response.Result.Credentials.AccessKeyId, response.Result.Credentials.SecretKey, response.Result.Credentials.SessionToken);
            }
            // Else get a new identity
            else
            {
                Debug.Log("Requesting a new playeridentity as none stored yet.");
                CognitoAWSCredentials credentials = new CognitoAWSCredentials(
                    this.identityPoolID,
                    this.region);
                Client.cognitoCredentials = credentials.GetCredentials();
                Client.cognitoID          = credentials.GetIdentityId();
                Debug.Log("Got Cognito ID: " + credentials.GetIdentityId());

                // Store to player prefs and save for future games
                PlayerPrefs.SetString("CognitoID", Client.cognitoID);
                PlayerPrefs.Save();
            }

            // Get latencies to regions
            this.MeasureLatencies();

            // Connect to the server now that we have our identity, credendtials and latencies
            StartCoroutine(ConnectToServer());
        }
    }
Example #23
0
        public AWSS3Helper(RegionEndpoint regionendpoint, String AWSAccessKey, String AWSSecretKey)
        {
            // Set configuration info
            AmazonS3Config config = new AmazonS3Config ();
            config.Timeout = new TimeSpan (1, 0, 0);
            config.ReadWriteTimeout = new TimeSpan (1, 0, 0);
            config.RegionEndpoint = regionendpoint;

            // Create S3 client
            S3client = Amazon.AWSClientFactory.CreateAmazonS3Client
                        (Gadgets.LoadConfigurationSetting ("AWSAccessKey", AWSAccessKey),
                         Gadgets.LoadConfigurationSetting ("AWSSecretKey", AWSSecretKey),
                         config);

            // Create the file transfer utility class
            fileTransferUtility = new TransferUtility (S3client);
        }
Example #24
0
        public FDGlacier(FDUserSettings settings, FDOperationLog oplog, string optype)
        {
            this.vaultName = settings.AWSGlacierVaultName;
            log = oplog;

            switch (settings.AWSRegion) {
            case FDUserSettings.AWSRegionIndex.USWest1:
                region = RegionEndpoint.USWest1;
                break;
            case FDUserSettings.AWSRegionIndex.USWest2:
                region = RegionEndpoint.USWest2;
                break;
            case FDUserSettings.AWSRegionIndex.USEast1:
                region = RegionEndpoint.USEast1;
                break;
            case FDUserSettings.AWSRegionIndex.EUWest1:
                region = RegionEndpoint.EUWest1;
                break;
            case FDUserSettings.AWSRegionIndex.APNortheast1:
                region = RegionEndpoint.APNortheast1;
                break;
            default:
                region = RegionEndpoint.USEast1;
                break;
            }

            //Instantiate the glacier config with our settins (for future move to AmazonGlacierClient)
            glacierConfig = new AmazonGlacierConfig();
            glacierConfig.RegionEndpoint = region;

            //Instantiate AWS Credentials
            awsCredentials = new BasicAWSCredentials(settings.AWSAccessKey, settings.AWSSecretKey);

            //Instantiate the transfer manager with our settings
            //TODO: Switch to glacier client so we can abort this damn thing
            //glacierClient = new AmazonGlacierClient(appConfig["AWSAccessKey"], appConfig["AWSSecretKey"], region);
            transferManager = new ArchiveTransferManager(awsCredentials, region);

            upOptions = new UploadOptions();
            downOptions = new DownloadOptions();
            progress = 0;
            upOptions.StreamTransferProgress = downOptions.StreamTransferProgress = this.onProgress;

            OperationType = optype;
        }
Example #25
0
        public static List<AWSSubnet> GetAllAWSSubnets(AWSEnvironment environment, RegionEndpoint region)
        {
            var subnets = new List<AWSSubnet>();
            try
            {
                var ec2Client = AWSClientFactory.CreateAmazonEC2Client(environment.AccessKeyID, environment.SecretAccessKey, region);
                var regionSubnets = ec2Client.DescribeSubnets();
                foreach (var subnet in regionSubnets.Subnets)
                {
                    subnets.Add(new AWSSubnet(subnet) { Public = IsSubnetPublic(ec2Client, subnet.SubnetId) });
                }
            }
            catch (AmazonEC2Exception aex)
            {
                Logger.Log(LogLevel.Error, aex, $"AmazonEC2Exception in GetAllAWSSubnets() : {aex.Message}");
            }

            return subnets;
        }
Example #26
0
        public PvcS3(
            string accessKey = null,
            string secretKey = null,
            string bucketName = null,
            Amazon.RegionEndpoint regionEndpoint = null)
        {
            this.accessKey = accessKey != null ? accessKey : PvcS3.AccessKey;
            this.secretKey = secretKey != null ? secretKey : PvcS3.SecretKey;
            this.bucketName = bucketName != null ? bucketName : PvcS3.BucketName;
            this.regionEndpoint = regionEndpoint != null ? regionEndpoint : PvcS3.RegionEndpoint;

            // Set up the API client for S3.
            AWSCredentials creds = new BasicAWSCredentials(this.accessKey, this.secretKey);
            this.s3client = new AmazonS3Client(creds, this.regionEndpoint);

            // Initialize some private stuff that we use to track md5 sums
            this.keyEtags = new Dictionary<string, string>();
            this.keyMD5Sums = new Dictionary<string, string>();
        }
Example #27
0
        public PvcS3(
            string accessKey  = null,
            string secretKey  = null,
            string bucketName = null,
            Amazon.RegionEndpoint regionEndpoint = null)
        {
            this.accessKey      = accessKey != null ? accessKey : PvcS3.AccessKey;
            this.secretKey      = secretKey != null ? secretKey : PvcS3.SecretKey;
            this.bucketName     = bucketName != null ? bucketName : PvcS3.BucketName;
            this.regionEndpoint = regionEndpoint != null ? regionEndpoint : PvcS3.RegionEndpoint;

            // Set up the API client for S3.
            AWSCredentials creds = new BasicAWSCredentials(this.accessKey, this.secretKey);

            this.s3client = new AmazonS3Client(creds, this.regionEndpoint);

            // Initialize some private stuff that we use to track md5 sums
            this.keyEtags   = new Dictionary <string, string>();
            this.keyMD5Sums = new Dictionary <string, string>();
        }
Example #28
0
        public S3FileSystem(string virtualRoot, string awsAccessKey, string awsSecretKey, string awsBucketName, string awsSaveMediaToS3, string awsRegion)
        {
            if (virtualRoot == null)
            {
                throw new ArgumentNullException(nameof(virtualRoot));
            }

            if (!virtualRoot.StartsWith("~/"))
            {
                throw new ArgumentException("The virtualRoot argument must be a virtual path and start with '~/'");
            }

            RootPath = IOHelper.MapPath(virtualRoot);
            _rootUrl = IOHelper.ResolveUrl(virtualRoot);
            
            AwsAccessKey = awsAccessKey;
            AwsSecretKey = awsSecretKey;
            AwsBucketName = awsBucketName;
            AwsSaveMediaToS3 = bool.Parse(awsSaveMediaToS3);

            // [ML] - This isnt ideal, but is the best way i an think of making this paramaterized in the IFileSystemProviderManager

            FieldInfo field = null;

            if (!string.IsNullOrWhiteSpace(awsRegion))
            {
                field = typeof(RegionEndpoint).GetField(awsRegion, BindingFlags.Static | BindingFlags.Public);

                if (field == null)
                {
                    throw new ArgumentException($"No Field found on '{typeof(RegionEndpoint).Name}' with the name '{awsRegion}'");
                }
            }

            AwsRegion = field?.GetValue(null) as RegionEndpoint ?? RegionEndpoint.USWest1;

            if (AwsIsValid)
            {
                _amazonS3FileSystem = new AmazonS3FileSystem(AwsAccessKey, AwsSecretKey, AwsBucketName, AwsRegion);
            }
        }
        public void To_Config(
            string configStr,
            string expectedBucketName,RegionEndpoint expectedRegion, string expectedkeyPrefix, string expectedDelimiter,
            Expression<Action<IAWSCredentialsFactory>> verify
        ) {
            var configToken = JToken.Parse(configStr);

            var config = this.converter.ToConfig(configToken);

            Assert.NotNull(config);
            var s3Config = config as AmazonS3CabinetConfig;

            Assert.NotNull(s3Config);

            Assert.Equal(expectedBucketName, s3Config.BucketName);
            Assert.Equal(expectedRegion, s3Config.AmazonS3Config.RegionEndpoint);
            Assert.Equal(expectedkeyPrefix, s3Config.KeyPrefix);
            Assert.Equal(expectedDelimiter, s3Config.Delimiter);

            this.mockCredentialsFactory.Verify(verify, Times.Once);
        }
        /// <summary>
        /// Configures Rebus to use Amazon Simple Queue Service as the message transport
        /// </summary>
        public static void UseAmazonSqs(this StandardConfigurer<ITransport> configurer, string accessKeyId, string secretAccessKey, RegionEndpoint regionEndpoint, string inputQueueAddress)
        {
            configurer.Register(c =>
            {
                var rebusLoggerFactory = c.Get<IRebusLoggerFactory>();
                var asyncTaskFactory = c.Get<IAsyncTaskFactory>();

                return new AmazonSqsTransport(inputQueueAddress, accessKeyId, secretAccessKey, regionEndpoint, rebusLoggerFactory, asyncTaskFactory);
            });

            configurer
                .OtherService<IPipeline>()
                .Decorate(p =>
                {
                    var pipeline = p.Get<IPipeline>();

                    return new PipelineStepRemover(pipeline)
                        .RemoveIncomingStep(s => s.GetType() == typeof (HandleDeferredMessagesStep));
                });

            configurer.OtherService<ITimeoutManager>().Register(c => new DisabledTimeoutManager(), description: SqsTimeoutManagerText);
        }
        public IAmazonSQS GetSqsClient(RegionEndpoint region)
        {
            var innerClient = CreateMeABus.DefaultClientFactory().GetSqsClient(region);
            var client = Substitute.For<IAmazonSQS>();

            client.ListQueues(Arg.Any<ListQueuesRequest>())
                .ReturnsForAnyArgs(r => innerClient.ListQueues(r.Arg<ListQueuesRequest>()))
                .AndDoes(r => Increment("ListQueues", r.Arg<ListQueuesRequest>().QueueNamePrefix, r.Arg<ListQueuesRequest>()));

            client.CreateQueue(Arg.Any<CreateQueueRequest>())
                .ReturnsForAnyArgs(r => innerClient.CreateQueue(r.Arg<CreateQueueRequest>()))
                .AndDoes(r => Increment("CreateQueue", r.Arg<CreateQueueRequest>().QueueName, r.Arg<CreateQueueRequest>()));

            client.GetQueueAttributes(Arg.Any<GetQueueAttributesRequest>())
                .ReturnsForAnyArgs(r => innerClient.GetQueueAttributes(r.Arg<GetQueueAttributesRequest>()))
                .AndDoes(r => Increment("GetQueueAttributes", r.Arg<GetQueueAttributesRequest>().QueueUrl, r.Arg<GetQueueAttributesRequest>()));

            client.ReceiveMessageAsync(Arg.Any<ReceiveMessageRequest>())
                .ReturnsForAnyArgs(r => innerClient.ReceiveMessageAsync(r.Arg<ReceiveMessageRequest>()))
                .AndDoes(r => Increment("ReceiveMessageAsync", r.Arg<ReceiveMessageRequest>().QueueUrl, r.Arg<ReceiveMessageRequest>()));

            return client;
        }
Example #32
0
        private void Init()
        {
            // Input check
            if (String.IsNullOrEmpty(ConfigurationManager.AppSettings["AccessKey"]))
            {
                throw new ConfigurationException("Illegal configuration file - missing AWS AccessKey");
            }
            if (String.IsNullOrEmpty(ConfigurationManager.AppSettings["SecretKey"]))
            {
                throw new ConfigurationException("Illegal configuration file - missing AWS SecretKey");
            }
            if (String.IsNullOrEmpty(ConfigurationManager.AppSettings["BucketName"]))
            {
                throw new ConfigurationException("Illegal configuration file - missing bucket name");
            }

            _accessKeyID     = ConfigurationManager.AppSettings["AccessKey"];
            _secretAccessKey = ConfigurationManager.AppSettings["SecretKey"];
            _bucketName      = ConfigurationManager.AppSettings["BucketName"];
            _regionEndpoint  = Amazon.RegionEndpoint.EUWest1;

            _client = new AmazonS3Client(_accessKeyID, _secretAccessKey, _regionEndpoint);
        }
Example #33
0
 public SqsQueueByUrl(RegionEndpoint region, string queueUrl, IAmazonSQS client)
     : base(region, client)
 {
     Url = queueUrl;
 }
 /// <summary>
 /// Create a client for the Amazon AutoScaling Service with the specified region
 /// </summary>
 /// <param name="awsAccessKey">The AWS Access Key associated with the account</param>
 /// <param name="awsSecretAccessKey">The AWS Secret Access Key associated with the account</param>
 /// <param name="region">The region to connect to.</param>
 /// <returns>An Amazon AutoScaling client</returns>
 /// <remarks>
 /// </remarks>
 public static IAmazonAutoScaling CreateAmazonAutoScalingClient(
     string awsAccessKey,
     string awsSecretAccessKey, RegionEndpoint region
     )
 {
     return new AmazonAutoScalingClient(awsAccessKey, awsSecretAccessKey, region);
 }
 /// <summary>
 /// Create a client for the Amazon SimpleEmailService Service with AWSCredentials and region.
 /// </summary>
 /// <param name="credentials">AWS Credentials</param>
 /// <param name="region">The region to connect to.</param>
 /// <returns>An Amazon SimpleEmailService client</returns>
 /// <remarks>
 /// </remarks>
 public static IAmazonSimpleEmailService CreateAmazonSimpleEmailServiceClient(AWSCredentials credentials, RegionEndpoint region)
 {
     return new AmazonSimpleEmailServiceClient(credentials, region);
 }
 /// <summary>
 /// Create a client for the Amazon SimpleEmailService Service with the specified region
 /// </summary>
 /// <param name="awsAccessKey">The AWS Access Key associated with the account</param>
 /// <param name="awsSecretAccessKey">The AWS Secret Access Key associated with the account</param>
 /// <param name="region">The region to connect to.</param>
 /// <returns>An Amazon SimpleEmailService client</returns>
 /// <remarks>
 /// </remarks>
 public static IAmazonSimpleEmailService CreateAmazonSimpleEmailServiceClient(
     string awsAccessKey,
     string awsSecretAccessKey, RegionEndpoint region
     )
 {
     return new AmazonSimpleEmailServiceClient(awsAccessKey, awsSecretAccessKey, region);
 }
 /// <summary>
 /// Create a client for the Amazon SecurityTokenService Service with AWSCredentials and region.
 /// </summary>
 /// <param name="credentials">AWS Credentials</param>
 /// <param name="region">The region to connect to.</param>
 /// <returns>An Amazon SecurityTokenService client</returns>
 /// <remarks>
 /// </remarks>
 public static IAmazonSecurityTokenService CreateAmazonSecurityTokenServiceClient(AWSCredentials credentials, RegionEndpoint region)
 {
     return new AmazonSecurityTokenServiceClient(credentials, region);
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="credentials"></param>
 /// <param name="region"></param>
 public AWSLambdaClient(AWSCredentials credentials, Amazon.RegionEndpoint region)
 {
     _client = new AmazonLambdaClient(credentials, region);
 }
 /// <summary>
 /// Bind the S3Region and S3Bucket
 /// </summary>
 /// <param name="s3Region"></param>
 /// <param name="s3BucketName"></param>
 public FileRepository(Amazon.RegionEndpoint s3Region, string s3BucketName)
 {
     _s3Region     = s3Region;
     _s3BucketName = s3BucketName;
 }
Example #40
0
        /// <summary>
        /// 生成签名(带自定义查询字符串)
        /// @wHaibo
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="fileKey"></param>
        /// <param name="qs"></param>
        /// <param name="expires"></param>
        /// <returns></returns>
        static string SignURL(string bucketName, string fileKey, NameValueCollection qs, TimeSpan expires)
        {
            Amazon.Runtime.BasicAWSCredentials cred = new Amazon.Runtime.BasicAWSCredentials(strAccessKey, strSecretKey);
            Amazon.RegionEndpoint endpoint          = Amazon.RegionEndpoint.CNNorth1;
            //Amazon.RegionEndpoint endpoint = Amazon.RegionEndpoint.APNortheast1;
            string host = endpoint.GetEndpointForService(bucketName + ".s3").Hostname;

            string method = "GET";
            string fk     = fileKey.Replace("/", "_XXXX_");
            string uri    = "/" + System.Uri.EscapeDataString(fk);

            uri = uri.Replace("_XXXX_", "/");
            Console.WriteLine(uri);

            DateTime dt               = DateTime.Now.ToUniversalTime();
            string   algorithm        = "AWS4-HMAC-SHA256";
            string   amz_date         = dt.ToString("yyyyMMddTHHmmss") + "Z";
            string   datestamp        = dt.ToString("yyyyMMdd");
            string   credential_scope = datestamp + "/" + endpoint.SystemName + "/s3/" + "aws4_request";

            NameValueCollection queryString = new NameValueCollection();

            if (qs != null && qs.Count > 0)
            {
                queryString.Add(qs);
            }
            queryString.Add("X-Amz-Algorithm", algorithm);
            queryString.Add("X-Amz-Credential", String.Format("{0}/{1}",
                                                              strAccessKey, credential_scope));
            queryString.Add("X-Amz-Date", amz_date);
            queryString.Add("X-Amz-Expires", Math.Round(expires.TotalSeconds, 0).ToString());
            queryString.Add("X-Amz-SignedHeaders", "host");

            List <String> kl = queryString.AllKeys.OrderBy(x => new StringAsciiCompare(WebUtility.UrlEncode(x))).ToList();
            StringBuilder sb = new StringBuilder();

            foreach (string k in kl)
            {
                if (sb.Length > 0)
                {
                    sb.Append("&");
                }
                sb.Append(WebUtility.UrlEncode(k)).Append("=");
                if (!String.IsNullOrEmpty(queryString[k]))
                {
                    sb.Append(WebUtility.UrlEncode(queryString[k]));
                }
            }


            string canonical_headers = "host:" + host + '\n';
            string signed_headers    = "host";
            string canonical_request = method + '\n' + uri + '\n' + sb.ToString() + '\n' + canonical_headers + '\n' + signed_headers + '\n' + "UNSIGNED-PAYLOAD";

            Console.WriteLine(canonical_request);
            string string_to_sign = algorithm + '\n' + amz_date + '\n' + credential_scope + '\n' + sha256(canonical_request);



            byte[] signing_key = getSignatureKey(strSecretKey, datestamp, endpoint.SystemName, "s3");

            string signature = BitConverter.ToString(HmacSHA256(string_to_sign, signing_key)).Replace("-", "").ToLower();


            string url = "http://" + host + uri + "?" + sb.ToString() + "&X-Amz-Signature=" + signature;

            return(url);
        }