Basic set of credentials consisting of an AccessKey and SecretKey
Inheritance: AWSCredentials
        public async Task<IList<S3Object>> GetListofFiles()
        {
           
               
                BasicAWSCredentials credentials = new BasicAWSCredentials("AKIAJTADDHY7T7GZXX5Q", "n4xV5B25mt7e6br84H2G9SXBx8eDYTQJgCxoaF49");
                AmazonS3Client s3Client = new AmazonS3Client(credentials, RegionEndpoint.USEast1);
                string token = null;
                var result = new List<S3Object>();
                do
                {
                    ListObjectsRequest request = new ListObjectsRequest()
                    {
                        BucketName = ExistingBucketName    
                    };
                    ListObjectsResponse response = await s3Client.ListObjectsAsync(request).ConfigureAwait(false);
                    result.AddRange(response.S3Objects);
                    token = response.NextMarker;

                } while (token != null);              
                
             return result;
            
            
        }
	static void Main()
	{
		// Connect to Amazon S3 service with authentication
		BasicAWSCredentials basicCredentials =
			new BasicAWSCredentials("AKIAIIYG27E27PLQ6EWQ", 
			"hr9+5JrS95zA5U9C6OmNji+ZOTR+w3vIXbWr3/td");
		AmazonS3Client s3Client = new AmazonS3Client(basicCredentials);

		// Display all S3 buckets
		ListBucketsResponse buckets = s3Client.ListBuckets();
		foreach (var bucket in buckets.Buckets)
		{
			Console.WriteLine(bucket.BucketName);
		}

		// Display and download the files in the first S3 bucket
		string bucketName = buckets.Buckets[0].BucketName;
		Console.WriteLine("Objects in bucket '{0}':", bucketName);
		ListObjectsResponse objects =
			s3Client.ListObjects(new ListObjectsRequest() { BucketName = bucketName });
		foreach (var s3Object in objects.S3Objects)
		{
			Console.WriteLine("\t{0} ({1})", s3Object.Key, s3Object.Size);
			if (s3Object.Size > 0)
			{
				// We have a file (not a directory) --> download it
				GetObjectResponse objData = s3Client.GetObject(
					new GetObjectRequest() { BucketName = bucketName, Key = s3Object.Key });
				string s3FileName = new FileInfo(s3Object.Key).Name;
				SaveStreamToFile(objData.ResponseStream, s3FileName);
			}
		}

		// Create a new directory and upload a file in it
		string path = "uploads/new_folder_" + DateTime.Now.Ticks;
		string newFileName = "example.txt";
		string fullFileName = path + "/" + newFileName;
		string fileContents = "This is an example file created through the Amazon S3 API.";
		s3Client.PutObject(new PutObjectRequest() { 
			BucketName = bucketName, 
			Key = fullFileName,
			ContentBody = fileContents}
		);
		Console.WriteLine("Created a file in Amazon S3: {0}", fullFileName);

		// Share the uploaded file and get a download URL
		string uploadedFileUrl = s3Client.GetPreSignedURL(new GetPreSignedUrlRequest()
		{ 
			BucketName = bucketName,
			Key = fullFileName,
			Expires = DateTime.Now.AddYears(5)
		});
		Console.WriteLine("File download URL: {0}", uploadedFileUrl);
		System.Diagnostics.Process.Start(uploadedFileUrl);
	}
Ejemplo n.º 3
0
 public string GetVideoUrl(string Key)
 {
     try
     {
         var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("AKIAY5IW6ZYW3VA4Q4WF", "CHwZ7C04mjFqDXAuStZ2nu2Fnjz2IG5oALrGnd+c");
         using (var client = new AmazonS3Client(awsCredentials, RegionEndpoint.EUWest1))
         {
             var request = new GetPreSignedUrlRequest()
             {
                 BucketName = "zgapara",
                 Key        = Key,
                 Expires    = DateTime.UtcNow.AddMinutes(120),
                 Verb       = HttpVerb.GET,
                 Protocol   = Protocol.HTTP
             };
             var url = client.GetPreSignedURL(request);
             return(url);
         }
     }
     catch (Exception ex)
     {
         ex.GetType();
         return("");
     }
 }
Ejemplo n.º 4
0
        public void CanWriteManyItems()
        {
            var awsCreds = new BasicAWSCredentials(accessKey, secretKey);
            var client = new Dynamo(awsCreds, RegionEndpoint.USWest2);

            var firstItem = new Dictionary<string, object>
            {
                {"last_modified", DateTime.UtcNow.ToString("O")},
                {"keyword", "test+item"},
                {"ppc", 123.33}
            };

            var secondItem = new Dictionary<string, object>
            {
                {"last_modified", DateTime.UtcNow.AddDays(-2).ToString("O")},
                {"keyword", "test+item+number+2"},
                {"ppc", 13.43}
            };

            var list = new List<Dictionary<string, object>>
            {
                firstItem,
                secondItem
            };
            client.WriteMany("dev_yahoo_ppc",list, 10);
        }
Ejemplo n.º 5
0
        protected void btnSend_Click(object sender, EventArgs e)
        {
            string fromAddress = "*****@*****.**";  // Replace with your "From" address. This address must be verified.
            string recipientAddress = "*****@*****.**"; // Replace with a "To" address. If your account is still in the
            string subject = "Test sending email using AWS SDK with C#";
            string body = "This is the email content!";

            AWSCredentials credentials = new BasicAWSCredentials("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY");

            using (var client = AWSClientFactory.CreateAmazonSimpleEmailServiceClient(credentials, RegionEndpoint.USEast1))
            {
                var request = new SendEmailRequest
                {
                    Source = fromAddress,
                    Destination = new Destination { ToAddresses = new List<string> { recipientAddress } },
                    Message = new Message
                    {
                        Subject = new Amazon.SimpleEmail.Model.Content(subject),
                        Body = new Body { Text = new Amazon.SimpleEmail.Model.Content(body) }
                    }
                };

                try
                {
                    // Send the email.
                    var response = client.SendEmail(request);
                    Response.Write("Email sent!");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
Ejemplo n.º 6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContextPool <RequestStatDbContext>(options => options.UseNpgsql(Configuration.GetConnectionString("RequestStatDb")));

            services.AddMvc(options => options.EnableEndpointRouting = false);
            services.AddHttpClient();

            services.AddTransient <IConfiguration>(_ =>
                                                   Configuration
                                                   );

            #region AmazonS3Client

            AmazonS3Config config = new AmazonS3Config();
            config.ServiceURL = Configuration.GetSection("S3")["ServiceURL"];
            AWSCredentials credentials = new Amazon.Runtime.BasicAWSCredentials(
                Configuration.GetSection("S3")["accessKey"],
                Configuration.GetSection("S3")["secretKey"]);
            services.AddTransient <IAmazonS3>(_ =>
                                              new AmazonS3Client(credentials, config));
            #endregion

            #region Swagger config

            services.AddOpenApiDocument(document =>
            {
                document.Title   = "S3 load API";
                document.Version = "v1";
            });
            #endregion
        }
    public void CreateGameLiftClient()
    {
        var config = new AmazonGameLiftConfig();

        config.RegionEndpoint = Amazon.RegionEndpoint.USEast1;
        Debug.Log("GL372");
        try
        {
            CredentialProfile profile = null;
            var nscf = new SharedCredentialsFile();
            nscf.TryGetProfile(profileName, out profile);
            var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("my accessKey", "my secretKey");
            // AWSCredentials credentials = profile.GetAWSCredentials(null);
            Debug.Log("demo-gamelift-unity profile GL376");
            aglc = new AmazonGameLiftClient(awsCredentials, config);
            Debug.Log("GL378");
        }
        catch (AmazonServiceException)
        {
            Debug.Log("regular profile search GL382");
            try
            {
                aglc = new AmazonGameLiftClient(config);
            }
            catch (AmazonServiceException e)
            {
                Debug.Log("AWS Credentials not found. Cannot connect to GameLift. Start application with -credentials <file> flag where credentials are the credentials.csv or accessKeys.csv file containing the access and secret key. GL390");
                Debug.Log(e.Message);
            }
        }
    }
		public Attachment GetStream(FileInfo file)
		{
			var bucketInfo = CreateBucketInfo(file.URL);
			var awsCredentials = new BasicAWSCredentials(McmModuleConfiguration.Aws.AccessKey, McmModuleConfiguration.Aws.SecretKey);
			var s3Config = new AmazonS3Config
			{
				ServiceURL = bucketInfo.ServiceURL
			};

			using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(awsCredentials, s3Config))
			{
				try
				{
					var request = new Amazon.S3.Model.GetObjectRequest
					{
						BucketName = bucketInfo.Bucketname,
						Key = bucketInfo.Key,
					};

					var response = client.GetObject(request);

					return new Attachment
					{
						FileName = file.OriginalFilename,
						ContentType = file.MimeType,
						Disposable = response,
						Stream = response.ResponseStream
					};
				}
				catch (System.Exception e)
				{
					throw new UnhandledException(string.Format("bucket: {0}, key: {1}, service_url: {2}", bucketInfo.Bucketname, bucketInfo.Key, bucketInfo.ServiceURL), e);
				}
			}
		}
 public IdentityManagementService(IIdentityManagementServiceConfigurationProvider configurationProvider,
     ILoggerProvider loggerProvider)
 {
     this.loggerProvider = loggerProvider;
     this.configurationProvider = configurationProvider;
     credentials = new BasicAWSCredentials(configurationProvider.AccessKey, configurationProvider.SecretKey);
 }
Ejemplo n.º 10
0
 public SQSHandeler(BasicAWSCredentials amazonCredentials, AmazonSQSClient sqsClient)
 {
     if (amazonCredentials != null && sqsClient != null) {
         this.amazonCredentials = amazonCredentials;
         this.sqsClient = sqsClient;
     }
 }
Ejemplo n.º 11
0
 private void InitAmazonData() {
     sqsHandeler = new SQSHandeler(amazonCredentials, sqsClient);
     if (sqsHandeler.AmazonCredentials != null
         && sqsHandeler.SQSClient != null) {
         amazonCredentials = sqsHandeler.AmazonCredentials;
         sqsClient = sqsHandeler.SQSClient;
     }
 }
 public AmazonS3StorageProvider(IAmazonS3StorageConfiguration amazonS3StorageConfiguration)
 {
     _amazonS3StorageConfiguration = amazonS3StorageConfiguration;
     var cred = new BasicAWSCredentials(_amazonS3StorageConfiguration.AWSAccessKey, _amazonS3StorageConfiguration.AWSSecretKey);
     //TODO: aws region to config
     _client = new AmazonS3Client(cred, RegionEndpoint.USEast1);
     var config = new TransferUtilityConfig();
     _transferUtility = new TransferUtility(_client, config);
 }
Ejemplo n.º 13
0
        public void CanGetNumberANumber()
        {
            var awsCreds = new BasicAWSCredentials(accessKey, secretKey);
            var client = new Dynamo(awsCreds, RegionEndpoint.USWest2);

            var json = client.Get("dev_yahoo_ppc", "keyword", "test+keyword");

            Assert.NotNull(json);
        }
Ejemplo n.º 14
0
        public void CanGetJsonFromSecondaryIndex()
        {
            var awsCreds = new BasicAWSCredentials(accessKey,secretKey);
            var client = new Dynamo(awsCreds,RegionEndpoint.USWest2);

            var json = client.Get("dev_conversions", "clickId-index", "clickId",
                new Guid("0886fc2b-9080-463b-801e-9fe17c33539f").ToString());

            Assert.NotNull(json);
        }
        public ElasticBeanstalkService(IElasticBeanstalkServiceConfigurationProvider configurationProvider, ILoggerProvider loggerProvider)
        {
            this.loggerProvider = loggerProvider;
            this.configurationProvider = configurationProvider;
            creds = new BasicAWSCredentials(configurationProvider.AccessKey, configurationProvider.SecretKey);

            currentCNamePrefix = $"{configurationProvider.CNamePrefix}-{configurationProvider.DeploymentEnvironmentName}";
            loggerProvider.GetLogger().Debug("Current CNamePrefix: {currentCNamePrefix}", currentCNamePrefix);

            nextCNamePrefix = $"{configurationProvider.CNamePrefix}Next-{configurationProvider.DeploymentEnvironmentName}";
            loggerProvider.GetLogger().Debug("Next CNamePrefix: {nextCNamePrefix}", nextCNamePrefix);
        }
Ejemplo n.º 16
0
        private void LoadConfiguration(string source)
        {
            var section = configuration.GetSection("API-KEYS");
            var key     = section.GetSection(source)["KEY"];
            var secret  = section.GetSection(source)["SECRET"];

            BucketName   = section.GetSection(source)["BUCKET"];
            BucketRegion = RegionEndpoint.GetBySystemName(section.GetSection(source)["REGION"]);
            var creds = new Amazon.Runtime.BasicAWSCredentials(key, secret);

            s3Client = new AmazonS3Client(creds, BucketRegion);
        }
Ejemplo n.º 17
0
        public TestRunner()
        {
            if (!Loaded)
            {
                var resource = Resources.Load(@"settings") as TextAsset;
                var settings = JsonMapper.ToObject(resource.text);

                TestAccountId = settings["AccountId"] == null ? null : settings["AccountId"].ToString();
                Credentials = new BasicAWSCredentials(settings["AccessKeyId"].ToString(), settings["SecretAccessKey"].ToString());
                RegionEndpoint = RegionEndpoint.GetBySystemName(settings["RegionEndpoint"].ToString());
                Loaded = true;
                LogWriter = new StringWriter();
            }
        }
Ejemplo n.º 18
0
        public void CanWriteDictionary()
        {
            var awsCreds = new BasicAWSCredentials(accessKey,secretKey);
            var client = new Dynamo(awsCreds,RegionEndpoint.USWest2);

            var dict = new Dictionary<string, object>
            {
                {"keyword", "test+keyword"},
                {"ppc", 1.75},
                {"last_modified", DateTime.UtcNow.ToString("O")}
            };

            client.WriteSingle("dev_yahoo_ppc", dict);
        }
Ejemplo n.º 19
0
        public static AmazonS3Client GetApi(AccountConfiguration account)
        {
            var credentials = new BasicAWSCredentials(account.Id, account.Secret);

            var region = RegionEndpoint.USWest1;
            if (account.AdditionalSettings != null && account.AdditionalSettings.ContainsKey("AWSRegion"))
            {
                var regionName = account.AdditionalSettings["AWSRegion"];
                region = RegionEndpoint.GetBySystemName(regionName);
            }

            var api = GetApi(credentials, region);

            return api;
        }
Ejemplo n.º 20
0
        public DomainChecker(ITldProviderClient tldProviderClient)
        {
            _tldProviderClient = tldProviderClient;

            ACCESS_KEY = ConfigurationManager.AppSettings["Aws.Route53.AccessKey"];
            SECRET_KEY = ConfigurationManager.AppSettings["Aws.Route53.SecretKey"];

            var credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
            var config = new AmazonRoute53DomainsConfig()
            {
                RegionEndpoint = RegionEndpoint.USEast1
            };

            _amazonRoute53DomainsClient = new AmazonRoute53DomainsClient(credentials, config);
        }
Ejemplo n.º 21
0
 // TODO: Get/set these values from namespace Common program.cs file instead
 public void Main(string[] args) {
     InitUserData();
     InitAmazonData();
     InitPeasantTasks();
     queueURL = sqsHandeler.QueueURL;
     amazonCredentials = new BasicAWSCredentials(userID, userKey);
     sqsClient = new AmazonSQSClient(amazonCredentials, RegionEndpoint.USEast1);
     Console.Write("Greetings, overlord!\n\n> How many tasks must"
         + " be given to these lowly peasants? ");
     var userMsg = Console.ReadLine();
     var numPeasantTasks = ParseUserMsg(userMsg);
     
     for (var i = 0; i < numPeasantTasks; i++) {
         var peasantTask = AssignTaskToPeasant();
         peasantTask.Wait();
     }
 }
Ejemplo n.º 22
0
        /// <summary>
        /// The method opens the queue
        /// </summary>
        public bool OpenQueue(string queuename, int maxnumberofmessages, String AWSAccessKey, String AWSSecretKey)
        {
            ClearErrorInfo();

            IsValid = false;

            if (!string.IsNullOrWhiteSpace(queuename))
            {
                // Checking for the need to use provided credentials instead of reading from app.Config
                if (!String.IsNullOrWhiteSpace(AWSSecretKey) && !String.IsNullOrWhiteSpace(AWSSecretKey))
                {
                    AWSCredentials awsCredentials = new BasicAWSCredentials(AWSAccessKey, AWSSecretKey);
                    queue                         = AWSClientFactory.CreateAmazonSQSClient (awsCredentials, RegionEndpoint.USEast1);
                }
                else
                {
                    queue = AWSClientFactory.CreateAmazonSQSClient (RegionEndpoint.USEast1);
                }

                try
                {
                    // Get queue url
                    GetQueueUrlRequest sqsRequest = new GetQueueUrlRequest();
                    sqsRequest.QueueName          = queuename;
                    queueurl                      = queue.GetQueueUrl(sqsRequest);

                    // Format receive messages request
                    rcvMessageRequest                     = new ReceiveMessageRequest();
                    rcvMessageRequest.QueueUrl            = queueurl.QueueUrl;
                    rcvMessageRequest.MaxNumberOfMessages = maxnumberofmessages;

                    // Format the delete messages request
                    delMessageRequest          = new DeleteMessageRequest();
                    delMessageRequest.QueueUrl = queueurl.QueueUrl;

                    IsValid = true;
                }
                catch (Exception ex)
                {
                    ErrorCode = e_Exception;
                    ErrorMessage = ex.Message;
                }
            }

            return IsValid;
        }
Ejemplo n.º 23
0
        public static void Main(string[] args)
        {
            LabVariables labVariables = null;
            var program = new Program();
            try
            {
                // Start the "prep" mode operations to make sure that the resources are all in the expected state.
                Console.WriteLine("Starting up in \"prep\" mode.");
                labVariables = program.PrepMode_Run();

                Console.WriteLine("\nPrep complete. Transitioning to \"app\" mode.");
                program.AppMode_Run(labVariables);
            }
            catch (Exception ex)
            {
                LabUtility.DumpError(ex);
            }
            finally
            {
                try
                {

                    if (labVariables != null)
                    {
                        Console.Write("\nLab run completed. Cleaning up buckets.");

                        AWSCredentials credentials =
                            new BasicAWSCredentials(ConfigurationManager.AppSettings["prepModeAWSAccessKey"],
                                ConfigurationManager.AppSettings["prepModeAWSSecretKey"]);

                        var s3Client = new AmazonS3Client(credentials, RegionEndpoint);

                        OptionalLabCode.RemoveLabBuckets(s3Client, labVariables.BucketNames);
                        Console.WriteLine(" Done.");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\nAttempt to clean up buckets failed. {0}", ex.Message);
                }

                Console.WriteLine("\nPress <enter> to end.");
                Console.ReadLine();
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Создает новый экземпляр класса S3Uploader
        /// </summary>
        /// <param name="credntialsProvider">Провайдер реквизитов для подключения</param>
        public S3Uploader(ICredentialsProvider credntialsProvider)
        {
            AmazonS3Config config = new
            AmazonS3Config()
            {
                CommunicationProtocol = Amazon.S3.Model.Protocol.HTTPS,
                BufferSize = 1024*1024,
            };
            var _credentials = new BasicAWSCredentials(credntialsProvider.AccessKey, credntialsProvider.SecretKey);
            _bucketName = ConfigurationManager.AppSettings["bucketName"];
            _client = new AmazonS3Client(_credentials,config);

            var strTimeout = ConfigurationManager.AppSettings["amazonRequestTimeout"];
            int minutesTimeout;
            if (int.TryParse(strTimeout, out minutesTimeout))
            {
                _timeout = minutesTimeout * 60 * 1000;
            }
        }
Ejemplo n.º 25
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>();
        }
Ejemplo n.º 26
0
        private static AmazonS3Client CreateClient(AmazonS3Config config = null)
        {
            if (config == null) config = new AmazonS3Config();

            // load the ViPR S3 endpoint information and credentials from app.config
            Uri endpoint = new Uri(ConfigurationManager.AppSettings["AWSServiceURI"]);
            AWSCredentials creds = new BasicAWSCredentials(
                ConfigurationManager.AppSettings["AWSAccessKey"],
                ConfigurationManager.AppSettings["AWSSecretKey"]);

            // establish the configuration, for either a host-based endpoint or an IP-based endpoint
            if (endpoint.HostNameType == UriHostNameType.IPv4 || endpoint.HostNameType == UriHostNameType.IPv6)
            {
                // By default, the AWS .NET SDK does not support
                // path-style buckets and nonstandard ports in the service
                // URL.  Therefore, we need to configure the ViPR endpoint
                // as a proxy instead of the ServiceURL since most test
                // configurations will use IP addresses and the internal
                // 9020/9021 ports.

                config.ProxyHost = endpoint.Host;
                config.ProxyPort = endpoint.Port;
                if (endpoint.Scheme == "https")
                {
                    throw new ArgumentException("ViPR S3 via HTTPS is not supported through .NET with the AWS SDK: " + endpoint);
                }
                else
                {
                    config.CommunicationProtocol = Protocol.HTTP;
                }
            }
            else
            {
                config.CommunicationProtocol =
                        string.Equals("http", endpoint.Scheme, StringComparison.InvariantCultureIgnoreCase) ?
                        Protocol.HTTP : Protocol.HTTPS;
                config.ServiceURL = endpoint.GetComponents(UriComponents.HostAndPort, UriFormat.UriEscaped);
            }

            var client = new AmazonS3Client(creds, config);
            return client;
        }
Ejemplo n.º 27
0
        public void DoWork()
        {
            var credentials = new BasicAWSCredentials(ConfigurationManager.AppSettings["AccessKey"], ConfigurationManager.AppSettings["SecretKey"]);
            _sqsClient = new AmazonSQSClient(credentials, RegionEndpoint.EUWest1);
            var running = true;
            while (running)
            {
                Console.WriteLine("Message to send: ");
                var message = Console.ReadLine();

                if (message == "end")
                {
                    running = false;
                }
                else
                {
                    AddToQueue(message);
                }
            }
        }
Ejemplo n.º 28
0
        public static void Main(string[] args)
        {
            string accessKey = "put your access key here!";
            string secretKey = "put your secret key here!";

            AmazonS3Config config = new AmazonS3Config();

            config.ServiceURL = "https://storage.yandexcloud.net";

            AWSCredentials credentials = new Amazon.Runtime.BasicAWSCredentials(accessKey, secretKey);

            using (client = new AmazonS3Client(credentials, config))
            {
                Console.WriteLine("Listing buckets");
                ListingBuckets();
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
       // private const string bucketurl = "http://s3.amazonaws.com/";

       // private const string bucketurl = "com.mf.carl-prototype.s3.amazonaws.com";
        #region Upload
        public async Task UploadFile(IReadOnlyList<StorageFile> files)
        {
            try
            {
               

                basicAwsCredentials = new BasicAWSCredentials(accesskey, secretkey);
                List<BackgroundTransferContentPart> parts = new List<BackgroundTransferContentPart>();
                for (int i = 0; i < files.Count; i++)
                {
                    BackgroundTransferContentPart part = new BackgroundTransferContentPart(files[i].Name, files[i].Name);
                    this.Filename = files[i].Name;
                    part.SetFile(files[i]);
                    parts.Add(part);
                }
                //Uri uri = new Uri(bucketurl + ExistingBucketName + "/");
                //Uri uri = new Uri("https://com.mf.carl-prototype.s3-us-west-2.amazonaws.com/");
                Uri uri = new Uri("https://s3.amazonaws.com/" + ExistingBucketName +"/");
              //  Uri uri = new Uri("https://"+ExistingBucketName+".s3-us-west-2.amazonaws.com/" );
            
                BackgroundUploader uploader = new BackgroundUploader();

                PasswordCredential pwdCredential = new PasswordCredential();
                pwdCredential.UserName = accesskey;
                pwdCredential.Password = secretkey;
                uploader.ServerCredential = pwdCredential;
               // uploader.Method = "POST";
       
               // uploader.SetRequestHeader("Content-Type", "multipart/form-data;boundary=34");
                  uploader.ServerCredential = new PasswordCredential{ UserName=accesskey, Password= secretkey};
                UploadOperation upload = await uploader.CreateUploadAsync(uri, parts);
                // Attach progress and completion handlers.
                await HandleUploadAsync(upload, true);
            }
            catch(Exception ex)
            {

            }
        }
Ejemplo n.º 30
0
 public AWSStorageProvider(string connectionString)
 {
     // Parse the connection string to the access key and secret key
     if (string.IsNullOrEmpty(connectionString))
         throw new ArgumentException("Connection string cannot be empty");
     else
     {
         // Split the connection string parts (make keys lower-cased for comparison)
         var parts = connectionString.ToDictionary(KeyValueSeparator, ConnectionStringPartSeparator, makeKeysLowercased: true);
         // Check if the connection string contains all required parts
         if (parts == null || connectionStringParts.Except(parts.Keys).Count() > 0)
         {
             throw new ArgumentException("Connection string must include the access key, secret key, and bucket of your AWS account");
         }
         else
         {
             var credentials = new BasicAWSCredentials(parts["accesskey"], parts["secretkey"]);
             _client = Amazon.AWSClientFactory.CreateAmazonS3Client(credentials);
             _rootDirectory = new S3DirectoryInfo(_client, parts["bucket"]);
             //_client = Amazon.AWSClientFactory.CreateAmazonS3Client(Amazon.RegionEndpoint.USWest2);
         }
     }
 }
Ejemplo n.º 31
0
        /// <summary>
        /// Resolves the set of <see cref="AWSCredentials">AWS Credentials</see> based on the
        /// combination of credential-related parameters that are specified.
        /// </summary>
        /// <remarks>
        /// The order of resolution is as follows:
        /// <list>
        /// <item>
        /// 1.  If AccessKeyId is found
        ///     <item>a.  If Session Token is found, returns Session AWS Credential</item>
        ///     <item>b.  If no Session Token, returns a Base AWS Credential</item>
        /// </item>
        /// <item>
        /// 2.  If Profile Name is found, return a Stored Profile AWS Credential, with
        ///     an optional, overridden Profile Location
        /// </item>
        /// <item>
        /// 3.  If an IAM Role Name is specified, get the credentials from the local
        ///     EC2 instance IAM Role environment; if the special name '*' is used,
        ///     it uses the first IAM Role found in the current EC2 environment
        /// </item>
        /// <item>
        /// 4.  Otherwise, assume credentials are specified in environment variables
        ///     accessible to the hosting process and retrieve them from the following
        ///     variables:
        ///     <item><code>AWS_ACCESS_KEY_ID</code></item>
        ///     <item><code>AWS_SECRET_ACCESS_KEY</code></item>
        ///     <item><code></code>AWS_SESSION_TOKEN</code> (optional)</code></item>
        /// </item>
        /// </list>
        /// </remarks>
        public AWSCredentials ResolveCredentials()
        {
            AWSCredentials cr;

            if (!string.IsNullOrEmpty(AwsAccessKeyId))
            {
                if (!string.IsNullOrEmpty(AwsSessionToken))
                {
                    cr = new SessionAWSCredentials(AwsAccessKeyId, AwsSecretAccessKey, AwsSessionToken);
                }
                else
                {
                    cr = new Amazon.Runtime.BasicAWSCredentials(AwsAccessKeyId, AwsSecretAccessKey);
                }
            }
            else if (!string.IsNullOrEmpty(AwsProfileName))
            {
                cr = new StoredProfileAWSCredentials(AwsProfileName, AwsProfileLocation);
            }
            else if (!string.IsNullOrEmpty(AwsIamRole))
            {
                if (AwsIamRole == IAM_ROLE_ANY)
                {
                    cr = new InstanceProfileAWSCredentials();
                }
                else
                {
                    cr = new InstanceProfileAWSCredentials(AwsIamRole);
                }
            }
            else
            {
                cr = new EnvironmentVariablesAWSCredentials();
            }

            return(cr);
        }
Ejemplo n.º 32
0
        private async Task TestConnection()
        {
            if (this.TestResult != null) return;

            m_lblTestResult.Visible = true;

            if (string.IsNullOrEmpty(this.AccessKey) ||string.IsNullOrEmpty(this.SecretKey))
            {
                m_lblTestResult.Text = "Please enter Access Key and Secret Key";
                return;
            }

            this.Enabled = false;
            m_lblTestResult.Text = "Testing Connection...";

            this.TestResult = null;

            var credentials = new BasicAWSCredentials(this.AccessKey, this.SecretKey);
            try
            {
                using (var api = AmazonS3Helper.GetApi(credentials, this.AWSRegion))
                {
                    var buckets = await api.ListBucketsAsync();
                    m_lblTestResult.Text = "Connection succeeded.";
                    this.TestResult = true;
                }
            }
            catch (Exception ex)
            {
                m_lblTestResult.Text = "Connection failed!";
                this.TestResult = false;
            }
            finally
            {
                this.Enabled = true;
            }
        }
        /// <summary>
        /// Initializes the provider by pulling the config info from the web.config and validate/create the DynamoDB table.
        /// If the table is being created this method will block until the table is active.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="config"></param>
        public override void Initialize(string name, NameValueCollection config)
        {
            _logger.InfoFormat("Initialize : Initializing Session provider {0}", name);

            if (config == null)
                throw new ArgumentNullException("config");

            base.Initialize(name, config);

            GetConfigSettings(config);

            RegionEndpoint region = null;
            if(!string.IsNullOrEmpty(this._regionName))
                region = RegionEndpoint.GetBySystemName(this._regionName);

            AWSCredentials credentials = null;
            if (!string.IsNullOrEmpty(this._accessKey))
            {
                credentials = new BasicAWSCredentials(this._accessKey, this._secretKey);
            }
            else if (!string.IsNullOrEmpty(this._profileName))
            {
                if (string.IsNullOrEmpty(this._profilesLocation))
                    credentials = new StoredProfileAWSCredentials(this._profileName);
                else
                    credentials = new StoredProfileAWSCredentials(this._profileName, this._profilesLocation);
            }

            AmazonDynamoDBConfig ddbConfig = new AmazonDynamoDBConfig();

            if (region != null)
                ddbConfig.RegionEndpoint = region;
            if (!string.IsNullOrEmpty(this._serviceURL))
                ddbConfig.ServiceURL = this._serviceURL;

            if (credentials != null)
            {
                this._ddbClient = new AmazonDynamoDBClient(credentials, ddbConfig);
            }
            else
            {
                this._ddbClient = new AmazonDynamoDBClient(ddbConfig);
            }

            ((AmazonDynamoDBClient)this._ddbClient).BeforeRequestEvent += DynamoDBSessionStateStore_BeforeRequestEvent;

            SetupTable();
        }
Ejemplo n.º 34
0
        //Construtor
        public EnvioS3()
        {
            var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("xxx", "yyy");

            s3Client = new AmazonS3Client(awsCredentials, bucketRegion);
        }
 public async Task AbortUploadFile(BasicAWSCredentials credentials, string bucketname, string key, string uploadid)
 {
     var s3Client = new AmazonS3Client(credentials, RegionEndpoint.USEast1);
     await s3Client.AbortMultipartUploadAsync(new AbortMultipartUploadRequest
     {
         BucketName = bucketname,
         Key = key,
         UploadId = uploadid
     });
 }
 private IAmazonRDS CreateClient()
 {
     var credentials = new BasicAWSCredentials(_context.Settings.Amazon.AccessKey, _context.Settings.Amazon.SecretKey);
     return new AmazonRDSClient(credentials, _context.Settings.Amazon.Region);
 }
Ejemplo n.º 37
0
    public static async Task <IActionResult> ListingObjectsAsync(HttpRequest req, TraceWriter log)
    {
        // Reference: https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingNetSDK.html
        var bucketName   = Environment.GetEnvironmentVariable("S3BucketName", EnvironmentVariableTarget.Process);
        var bucketRegion = RegionEndpoint.GetBySystemName(Environment.GetEnvironmentVariable("S3BucketRegion", EnvironmentVariableTarget.Process));

        if (awsSecretKey == null)
        {
            log.Info($"Fetching AWS secret key for the first time from KeyVault...");
            var azureServiceTokenProvider = new AzureServiceTokenProvider();
            var keyVaultClient            = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
            var secretName       = Environment.GetEnvironmentVariable("AmazonS3SecretAccessKeySecretName", EnvironmentVariableTarget.Process);
            var azureKeyVaultUrl = Environment.GetEnvironmentVariable("AzureKeyVaultUrl", EnvironmentVariableTarget.Process);
            var secret           = await keyVaultClient.GetSecretAsync($"{azureKeyVaultUrl}secrets/{secretName}").ConfigureAwait(false);

            awsSecretKey = secret.Value;
            log.Info("[Setting]: Successfully fetched AWS secret key from KeyVault.");
        }

        var credentials = new Amazon.Runtime.BasicAWSCredentials(Environment.GetEnvironmentVariable("AwsAccessKey", EnvironmentVariableTarget.Process), awsSecretKey);

        string  s3BucketLastProcessedDateTimeUtcAsString = req.Query["s3BucketLastProcessedDateTimeUtc"]; // GET
        string  requestBody = new StreamReader(req.Body).ReadToEnd();                                     // POST
        dynamic data        = JsonConvert.DeserializeObject(requestBody);

        s3BucketLastProcessedDateTimeUtcAsString = s3BucketLastProcessedDateTimeUtcAsString ?? data?.s3BucketLastProcessedDateTimeUtc;
        if (string.IsNullOrWhiteSpace(s3BucketLastProcessedDateTimeUtcAsString))
        {
            string errorMessage = "A 's3BucketLastProcessedDateTimeUtc' querystring parameter or a request body containing a JSON object with a 's3BucketLastProcessedDateTimeUtc' property was expected but not found.";
            log.Info(errorMessage);
            return(new BadRequestObjectResult(errorMessage));
        }
        var s3BucketLastProcessedDateTimeUtc = DateTime.Parse(s3BucketLastProcessedDateTimeUtcAsString);

        log.Info($"Bucket Name: {bucketName}.");
        log.Info($"Bucket Region: {bucketRegion}.");
        log.Info($"S3 Bucket Last Processed DateTimeUtc: {s3BucketLastProcessedDateTimeUtcAsString}.");

        List <S3Object> filteredObjects             = new List <S3Object>();
        int             totalUnfilteredCount        = 0;
        int             currentUnfilteredCount      = 0;
        DateTime        newLastProcessedDateTimeUtc = DateTime.UtcNow;
        IAmazonS3       client = new AmazonS3Client(credentials, bucketRegion);

        try
        {
            ListObjectsV2Request request = new ListObjectsV2Request
            {
                BucketName = bucketName
            };

            ListObjectsV2Response response;
            do
            {
                response = await client.ListObjectsV2Async(request);

                currentUnfilteredCount = response.S3Objects.Count;
                totalUnfilteredCount  += currentUnfilteredCount;
                log.Info($"Results Count (pre-filtering): {currentUnfilteredCount}.");
                var currentFilteredObjects = response.S3Objects.FindAll((s3Object) =>
                {
                    // Return objects updated after the last process date and that are not folder records (end with _$folder$ and have 0 size).
                    return(DateTime.Compare(s3Object.LastModified.ToUniversalTime(), s3BucketLastProcessedDateTimeUtc) > 0
                           & !(s3Object.Key.EndsWith("_$folder$", StringComparison.InvariantCulture) && s3Object.Size == 0));
                });
                log.Info($"Results Count (post-filtering): {currentFilteredObjects.Count}.");
                filteredObjects.AddRange(currentFilteredObjects);

                log.Info($"Next Continuation Token: {response.NextContinuationToken}.");
                request.ContinuationToken = response.NextContinuationToken;
            } while (response.IsTruncated);

            log.Info($"Results Count (total-unfiltered): {totalUnfilteredCount}.");
            log.Info($"Results Count (total-filtered): {filteredObjects.Count}.");
            dynamic payload = new System.Dynamic.ExpandoObject();
            payload.s3Objects = filteredObjects;
            payload.newLastProcessedDateTimeUtc = newLastProcessedDateTimeUtc.ToString();
            return(new OkObjectResult(JsonConvert.SerializeObject(payload, Formatting.Indented)));
        }
        catch (AmazonS3Exception amazonS3Exception)
        {
            log.Info($"AmazonS3Exception [ListingObjectsAsync]: {amazonS3Exception.ToString()}.");
            return(new BadRequestObjectResult("Operation failed (AmazonS3Exception). Check function's log for details."));
        }
        catch (Exception exception)
        {
            log.Info($"Exception [ListingObjectsAsync]: {exception.ToString()}.");
            return(new BadRequestObjectResult("Operation failed. Check function's log for details."));
        }
    }
Ejemplo n.º 38
0
        private void create_security_group_rule(string key, string secret, Amazon.RegionEndpoint region, string security_group, string serverports, string ipEnpoint = "")
        {
            HttpStatusCode ret        = HttpStatusCode.OK;
            string         ip         = "0.0.0.0";
            MailAddress    fromMailer = null;
            MailAddress    toMailer   = null;
            string         mailhost   = "";
            int            mailport   = 0;
            bool           mailssl    = false;
            string         mailuser   = "";
            string         mailpass   = "";

            bool loadConfig = true;

            string partialmsg = "";

            if (ipEnpoint != string.Empty)
            {
                ip = get_external_ip(ipEnpoint);
            }

            if (loadConfig)
            {
                try
                {
                    fromMailer = new MailAddress(ConfigurationManager.AppSettings["MAIL-FROM"].ToString());
                    toMailer   = new MailAddress(ConfigurationManager.AppSettings["MAIL-TO"].ToString());
                    mailhost   = ConfigurationManager.AppSettings["MAIL-HOST"].ToString();
                    mailport   = int.Parse(ConfigurationManager.AppSettings["MAIL-PORT"].ToString());
                    mailssl    = bool.Parse(ConfigurationManager.AppSettings["MAIL-SSL"].ToString());
                    mailuser   = ConfigurationManager.AppSettings["MAIL-USER"].ToString();
                    mailpass   = ConfigurationManager.AppSettings["MAIL-PASS"].ToString();
                    ipEnpoint  = ConfigurationManager.AppSettings["IP-ENDPOINT"].ToString();
                    ip         = get_external_ip(ipEnpoint);
                }
                catch { loadConfig = false; }
            }

            if (ip != "")
            {
                try
                {
                    AmazonEC2Client ec2Client = null;

                    AWSCredentials credentials = new Amazon.Runtime.BasicAWSCredentials(key, secret);

                    ec2Client = new AmazonEC2Client(credentials, region);

                    IpRange ipRange = new IpRange
                    {
                        CidrIp      = ip + "/32",
                        Description = "Rule created by aws-security-group-access-rule at " + DateTime.Now.ToString()
                    };

                    var ingressRequest = new AuthorizeSecurityGroupIngressRequest
                    {
                        GroupId = security_group
                    };

                    var listaPortas = serverports.Split(',');
                    foreach (var item in listaPortas)
                    {
                        var ipPermission = new IpPermission
                        {
                            IpProtocol = "tcp",
                            FromPort   = Int16.Parse(item),
                            ToPort     = Int16.Parse(item)
                        };
                        ipPermission.Ipv4Ranges.Add(ipRange);

                        ingressRequest.IpPermissions.Add(ipPermission);
                    }

                    var ingressResponse = ec2Client.AuthorizeSecurityGroupIngress(ingressRequest);
                    partialmsg += "[AWS Response :: " + ingressResponse.HttpStatusCode.ToString() + "]";
                    ret         = ingressResponse.HttpStatusCode;

                    #region Debug Section

                    /*
                     * switch (ingressResponse.HttpStatusCode)
                     * {
                     *  case HttpStatusCode.Continue: partialmsg += ""; break;
                     *  case HttpStatusCode.SwitchingProtocols: partialmsg += ""; break;
                     *  case HttpStatusCode.OK: partialmsg += ""; break;
                     *  case HttpStatusCode.Created: partialmsg += ""; break;
                     *  case HttpStatusCode.Accepted: partialmsg += ""; break;
                     *  case HttpStatusCode.NonAuthoritativeInformation: partialmsg += ""; break;
                     *  case HttpStatusCode.NoContent: partialmsg += ""; break;
                     *  case HttpStatusCode.ResetContent: partialmsg += ""; break;
                     *  case HttpStatusCode.PartialContent: partialmsg += ""; break;
                     *  case HttpStatusCode.MultipleChoices: partialmsg += ""; break;
                     *  case HttpStatusCode.MovedPermanently: partialmsg += ""; break;
                     *  case HttpStatusCode.Found: partialmsg += ""; break;
                     *  case HttpStatusCode.SeeOther: partialmsg += ""; break;
                     *  case HttpStatusCode.NotModified: partialmsg += ""; break;
                     *  case HttpStatusCode.UseProxy: partialmsg += ""; break;
                     *  case HttpStatusCode.Unused: partialmsg += ""; break;
                     *  case HttpStatusCode.TemporaryRedirect: partialmsg += ""; break;
                     *  case HttpStatusCode.BadRequest: partialmsg += ""; break;
                     *  case HttpStatusCode.Unauthorized: partialmsg += ""; break;
                     *  case HttpStatusCode.PaymentRequired: partialmsg += ""; break;
                     *  case HttpStatusCode.Forbidden: partialmsg += ""; break;
                     *  case HttpStatusCode.NotFound: partialmsg += ""; break;
                     *  case HttpStatusCode.MethodNotAllowed: partialmsg += ""; break;
                     *  case HttpStatusCode.NotAcceptable: partialmsg += ""; break;
                     *  case HttpStatusCode.ProxyAuthenticationRequired: partialmsg += ""; break;
                     *  case HttpStatusCode.RequestTimeout: partialmsg += ""; break;
                     *  case HttpStatusCode.Conflict: partialmsg += ""; break;
                     *  case HttpStatusCode.Gone: partialmsg += ""; break;
                     *  case HttpStatusCode.LengthRequired: partialmsg += ""; break;
                     *  case HttpStatusCode.PreconditionFailed: partialmsg += ""; break;
                     *  case HttpStatusCode.RequestEntityTooLarge: partialmsg += ""; break;
                     *  case HttpStatusCode.RequestUriTooLong: partialmsg += ""; break;
                     *  case HttpStatusCode.UnsupportedMediaType: partialmsg += ""; break;
                     *  case HttpStatusCode.RequestedRangeNotSatisfiable: partialmsg += ""; break;
                     *  case HttpStatusCode.ExpectationFailed: partialmsg += ""; break;
                     *  case HttpStatusCode.UpgradeRequired: partialmsg += ""; break;
                     *  case HttpStatusCode.InternalServerError: partialmsg += ""; break;
                     *  case HttpStatusCode.NotImplemented: partialmsg += ""; break;
                     *  case HttpStatusCode.BadGateway: partialmsg += ""; break;
                     *  case HttpStatusCode.ServiceUnavailable: partialmsg += ""; break;
                     *  case HttpStatusCode.GatewayTimeout: partialmsg += ""; break;
                     *  case HttpStatusCode.HttpVersionNotSupported: partialmsg += ""; break;
                     * }
                     */
                    #endregion
                }
                catch (AmazonEC2Exception ex)
                {
                    ret = ex.StatusCode;

                    partialmsg += "[ERROR-CODE " + ex.ErrorCode + "] " + ex.Message + "<BR>";
                }
                finally
                {
                    if (loadConfig)
                    {
                        sendEmail(mailhost, mailport, mailssl, mailuser, mailpass, fromMailer, toMailer,
                                  "[aws-security-group-access-rule]" + ret.ToString(),
                                  string.Format("Access granted to ports {1} by IP {0}",
                                                ip, serverports)
                                  );
                    }
                }
            }

            void sendEmail(string hostname, int port, bool ssl, string user, string pass, MailAddress fromMail, MailAddress toMail, string subject, string message, bool ishtml = false)
            {
                MailMessage msg = new MailMessage
                {
                    Subject    = subject,
                    Body       = message,
                    IsBodyHtml = ishtml,
                    Priority   = MailPriority.High,
                    From       = fromMail
                };

                msg.To.Add(toMail);

                SmtpClient mailClient = new SmtpClient
                {
                    Host = hostname,
                    Port = port,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(user, pass),
                    EnableSsl             = ssl
                };

                mailClient.Send(msg);
            }
        }
Ejemplo n.º 39
-1
 public void DoWork()
 {
     var credentials = new BasicAWSCredentials(ConfigurationManager.AppSettings["AccessKey"], ConfigurationManager.AppSettings["SecretKey"]);
     _sqsClient = new AmazonSQSClient(credentials, RegionEndpoint.EUWest1);
     Console.WriteLine("Number of messages at once");
     int numberOfMessages;
     int.TryParse(Console.ReadLine(), out numberOfMessages);
     var running = true;
     while (running)
     {
         var messageList = GetMessageFromQueue(numberOfMessages);
         Console.WriteLine("Number of messages: " + messageList.Count);
         if (messageList.Count == 0)
         {
             Thread.Sleep(5000);
         }
         Console.WriteLine();
         foreach (var message in messageList)
         {
             Console.WriteLine("Message: " + message.Body);
             Console.WriteLine("Receipt Handle: " + message.ReceiptHandle);
             Console.WriteLine("Delete?");
             var readLine = Console.ReadLine();
             var delete = readLine != null ? readLine.ToLower() : "n";
             if (delete == "y")
             {
                 DeleteMessageFromQueue(message.ReceiptHandle);
             }
         }
     }
 }