Beispiel #1
0
        public FileService()
        {
            Amazon.S3.AmazonS3Config s3Config = new Amazon.S3.AmazonS3Config();
            s3Config.ServiceURL = FileService.serviceUrl;

            this.S3Client = new Amazon.S3.AmazonS3Client(ConfigService.amazonAccessKey, ConfigService.amazonSecretAccessKey, s3Config);
        }
        public async Task <String> SaveFileToAws(FileDto file)
        {
            var keyId     = _configuration.GetSection("AWS").GetValue <String>("AWSAccessKeyId");
            var keySecret = _configuration.GetSection("AWS").GetValue <String>("AWSSecretAccessKey");

            var config = new Amazon.S3.AmazonS3Config();

            config.RegionEndpoint = Amazon.RegionEndpoint.EUWest3;

            var client = new Amazon.S3.AmazonS3Client(keyId, keySecret, config);

            var fileName = $"users-uploads/{Guid.NewGuid().ToString()}-{file.FileName}";

            var request = new Amazon.S3.Model.PutObjectRequest
            {
                BucketName   = "teller-uploads",
                ContentType  = file.ContentType,
                InputStream  = file.ReadStream,
                Key          = fileName,
                StorageClass = Amazon.S3.S3StorageClass.Standard,
                CannedACL    = Amazon.S3.S3CannedACL.PublicRead,
            };

            var response = await client.PutObjectAsync(request);

            if (response.HttpStatusCode == HttpStatusCode.OK)
            {
                return("https://teller-uploads.s3.amazonaws.com/" + fileName);
            }
            else
            {
                return(null);
            }
        }
Beispiel #3
0
        public void UploadFile(Uri fileUrl, Stream s)
        {
            var filePath = fileUrl.AbsolutePath;

            // We need to strip off any leading '/' in the path or
            // else it creates a path with an empty leading segment
            if (filePath.StartsWith("/"))
            {
                filePath = filePath.Substring(1);
            }

            var s3 = new Amazon.S3.AmazonS3Client(
                AccessKeyId, SecretAccessKey, RegionEndpoint);

            var s3Requ = new Amazon.S3.Model.PutObjectRequest
            {
                BucketName      = BucketName,
                Key             = filePath,
                InputStream     = s,
                AutoCloseStream = false,
            };
            var s3Resp = s3.PutObject(s3Requ);

            if (DnsProvider != null)
            {
                var hostname = fileUrl.Host;
                DnsProvider.EditCnameRecord(hostname, DnsCnameTarget);
            }
            else
            {
                // TODO:  do nothing for now
                // Throw Exception???
            }
        }
Beispiel #4
0
        private void PushLogToS3(string key, string log)
        {
            if (string.IsNullOrEmpty(Settings.ResultsBucket))
            {
                return;
            }

            try
            {
                WriteInfo("Pushing log to S3...");
                using (var s3 = new Amazon.S3.AmazonS3Client(Credentials, RegionEndpoint))
                {
                    s3.PutObjectAsync(new PutObjectRequest
                    {
                        BucketName  = Settings.ResultsBucket,
                        Key         = key,
                        ContentBody = log
                    }).Wait();
                }
            }
            catch (Exception e)
            {
                WriteError("Error pushing logs to S3: {0}", e);
            }
        }
        public void UploadFile(Uri fileUrl, Stream s)
        {
            var filePath = fileUrl.AbsolutePath;
            // We need to strip off any leading '/' in the path or
            // else it creates a path with an empty leading segment
            if (filePath.StartsWith("/"))
                filePath = filePath.Substring(1);

            var s3 = new Amazon.S3.AmazonS3Client(
                    AccessKeyId, SecretAccessKey, RegionEndpoint);

            var s3Requ = new Amazon.S3.Model.PutObjectRequest
            {
                BucketName = BucketName,
                Key = filePath,
                InputStream = s,
                AutoCloseStream = false,
            };
            var s3Resp = s3.PutObject(s3Requ);

            if (DnsProvider != null)
            {
                var hostname = fileUrl.Host;
                DnsProvider.EditCnameRecord(hostname, DnsCnameTarget);
            }
            else
            {
                // TODO:  do nothing for now
                // Throw Exception???
            }
        }
Beispiel #6
0
        public static void CleanBucket(string accessKey, string secretKey, string bucket, TimeSpan t)
        {
            using (var client = new Amazon.S3.AmazonS3Client(accessKey, secretKey, RegionEndpoint.USEast1)) {
                string marker = null;
                while (true)
                {
                    var loReq = new ListObjectsRequest()
                    {
                        Marker = marker, BucketName = bucket
                    };
                    var loRes = client.ListObjects(loReq);

                    foreach (var o in loRes.S3Objects)
                    {
                        if (o.LastModified <= DateTime.Now.Subtract(t))
                        {
                            client.DeleteObject(bucket, o.Key);
                            Console.WriteLine("Deleted " + o.Key);
                        }
                    }

                    if (loRes.IsTruncated)
                    {
                        marker = loRes.NextMarker;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Beispiel #7
0
        private async void View_SendLogOutput(object sender, EventArgs e)
        {
            View.Sending = true;
            try
            {
                var logId = DateTime.UtcNow.ToString("dd MMMM yyyy hh:mm:ss", CultureInfo.InvariantCulture);

                var file = Path.Combine(Resources.GameUserDir, "minidump.dmp");
                if (FileSystem.Current.FileExists(file))
                {
                    var transfer      = new TransferUtility(_game.SupportS3BucketAccessKey, _game.SupportS3BucketSecretKey, Amazon.RegionEndpoint.USEast1);
                    var uploadRequest = new TransferUtilityUploadRequest();
                    uploadRequest.FilePath   = file;
                    uploadRequest.BucketName = _game.SupportS3Bucket;
                    uploadRequest.Key        = logId + "/minidump.dmp";
                    await transfer.UploadAsync(uploadRequest);
                }

                var client     = new Amazon.S3.AmazonS3Client(_game.SupportS3BucketAccessKey, _game.SupportS3BucketSecretKey, Amazon.RegionEndpoint.USEast1);
                var putRequest = new PutObjectRequest();
                putRequest.ContentBody = SubmitCoreErrorPresenter.GetLogContent(_game, _detail);
                putRequest.BucketName  = _game.SupportS3Bucket;
                putRequest.Key         = logId + "/log.txt";
                await client.PutObjectAsync(putRequest);
            }
            catch (Exception ex)
            {
                Message.Show("Failed to send error report");
                Logger.Current.Write(ex, "Failed to send error report");
            }
            View.Sending = false;
            CloseView();
        }
Beispiel #8
0
        public override Result Execute(ConDepSettings settings, CancellationToken token)
        {
            var dynamicAwsConfig = settings.Config.OperationsConfig.Aws;

            var client = new Amazon.S3.AmazonS3Client(GetAwsCredentials(dynamicAwsConfig), RegionEndpoint.GetBySystemName((string)dynamicAwsConfig.Region));

            var response = client.GetObject(_bucket, _key);

            response.WriteResponseStreamToFile(_dstFile);

            var result = Result.SuccessUnChanged();

            result.Data.BucketName     = response.BucketName;
            result.Data.ContentLength  = response.ContentLength;
            result.Data.Expiration     = response.Expiration;
            result.Data.Expires        = response.Expires;
            result.Data.HttpStatusCode = response.HttpStatusCode;
            result.Data.Key            = response.Key;
            result.Data.LastModified   = response.LastModified;
            result.Data.ServerSideEncryptionCustomerMethod = response.ServerSideEncryptionCustomerMethod;
            result.Data.ServerSideEncryptionMethod         = response.ServerSideEncryptionMethod;
            result.Data.StorageClass            = response.StorageClass;
            result.Data.VersionId               = response.VersionId;
            result.Data.WebsiteRedirectLocation = response.WebsiteRedirectLocation;
            return(result);
        }
Beispiel #9
0
 static void WipeBucket(ControllerConfiguration context)
 {
     using (var client = new Amazon.S3.AmazonS3Client(context.AwsAccessKeyId, context.AwsSecretAccessKey))
     {
         int batchSize = 100;
         int count = batchSize;
         while (count == batchSize)
         {
             using (var listResponse = client.ListObjects(new Amazon.S3.Model.ListObjectsRequest()
             {
                 BucketName = context.BucketName,
                 MaxKeys = batchSize,
             }))
             {
                 count = listResponse.S3Objects.Count;
                 Parallel.ForEach(listResponse.S3Objects, s3obj =>
                     {
                         using (var delResponse = client.DeleteObject(new Amazon.S3.Model.DeleteObjectRequest()
                         {
                             BucketName = context.BucketName,
                             Key = s3obj.Key,
                         })) { }
                     });
             }
         }
     }
 }
Beispiel #10
0
        public FileUploader(string accessKeyId, string secretAccessKey, string serviceUrl)
        {
            Amazon.S3.AmazonS3Config s3Config = new Amazon.S3.AmazonS3Config();
            s3Config.ServiceURL = serviceUrl;

            this.S3Client = new Amazon.S3.AmazonS3Client(accessKeyId, secretAccessKey, s3Config);
        }
Beispiel #11
0
        public override Result Execute(ConDepSettings settings, CancellationToken token)
        {
            var dynamicAwsConfig = settings.Config.OperationsConfig.Aws;

            using (var client = new Amazon.S3.AmazonS3Client(GetAwsCredentials(dynamicAwsConfig), RegionEndpoint.GetBySystemName((string)dynamicAwsConfig.Region)))
            {
                Logger.Verbose("Trying to get object metadata from S3");
                try
                {
                    var obj = client.GetObjectMetadata(_bucket, _key);
                    if (obj.HttpStatusCode != HttpStatusCode.OK)
                    {
                        Logger.Verbose("Failed to get Amazon S3 Object metadata. Http status code was {0}", obj.HttpStatusCode);
                        Logger.Info("Could not find Amazon S3 Object {0} in bucket {1}- assuming allready deleted.", _key, _bucket);
                        return(Result.SuccessUnChanged());
                    }
                }
                catch (Exception ex)
                {
                    Logger.Verbose("Failed to get Amazon S3 Object metadata. Exception was:");
                    Logger.Verbose(ex.Message);
                    Logger.Warn("Exception during Amazon S3 Object lookup. Assuming object allready deleted.");
                    return(Result.Failed());
                }

                Logger.Info("Deleting Amazon S3 Object with key {0} in Bucket {1}", _key, _bucket);
                client.DeleteObject(_bucket, _key);
                return(Result.SuccessChanged());
            }
        }
Beispiel #12
0
 public static void TestCredentials(AWSCredentials credentials)
 {
     using (var client = new Amazon.S3.AmazonS3Client(credentials))
     {
         // Retries to handle credentials not being full propagated yet.
         UtilityMethods.WaitUntilSuccess(() => client.ListBuckets());
     }
 }
Beispiel #13
0
        public S3Handler()
        {
            Amazon.S3.AmazonS3Config s3Config = new Amazon.S3.AmazonS3Config();
            // Build Url.
            s3Config.ServiceURL = ServiceUrl;

            //Amazon.S3.AmazonS3Client S3Client = null;
            this.S3Client = new Amazon.S3.AmazonS3Client(AccessKey, SecretKey, s3Config);
        }
        public void Execute(JobExecutionContext context)
        {
            //Get Current Batch
            //Close Current Batch
            //And Open New One
            //Create a new BatchFile Record
            logger.Log(LogLevel.Info, String.Format("Creating Nacha ACH File at {0}", System.DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")));

            logger.Log(LogLevel.Info, String.Format("Batching Transactions"));
            var transactions = transactionBatchService.BatchTransactions();

            FileGenerator fileGeneratorService = new FileGenerator();

            fileGeneratorService.CompanyIdentificationNumber = ConfigurationManager.AppSettings["CompanyIdentificationNumber"];
            fileGeneratorService.CompanyName                     = ConfigurationManager.AppSettings["CompanyName"];
            fileGeneratorService.ImmediateDestinationId          = ConfigurationManager.AppSettings["ImmediateDestinationId"];
            fileGeneratorService.ImmediateDestinationName        = ConfigurationManager.AppSettings["ImmediateDestinationName"];
            fileGeneratorService.ImmediateOriginId               = ConfigurationManager.AppSettings["ImmediateOriginId"];
            fileGeneratorService.ImmediateOriginName             = ConfigurationManager.AppSettings["ImmediateOriginName"];
            fileGeneratorService.OriginatingTransitRoutingNumber = ConfigurationManager.AppSettings["OriginatingTransitRoutingNumber"];
            fileGeneratorService.CompanyDiscretionaryData        = ConfigurationManager.AppSettings["CompanyDiscretionaryData"];

            logger.Log(LogLevel.Info, String.Format("Processing Transactions"));

            var results = fileGeneratorService.ProcessFile(transactions);

            logger.Log(LogLevel.Info, String.Format("Creating Batch File"));

            StringBuilder sb = new StringBuilder();

            results.ForEach(s => sb.AppendLine(s));
            string fileContext = sb.ToString();

            logger.Log(LogLevel.Info, String.Format("Uploading Batch File to S3 Server"));

            string bucketName = ConfigurationManager.AppSettings["NachaFileBucketName"];

            if (String.IsNullOrEmpty(bucketName))
            {
                throw new Exception("S3 bucket name for NachaFileBucketName not configured");
            }

            Amazon.S3.AmazonS3Client s3Client   = new Amazon.S3.AmazonS3Client();
            PutObjectRequest         putRequest = new PutObjectRequest()
            {
                ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256,
                BucketName  = bucketName,
                ContentBody = fileContext,
                Key         = "NACHA_FILE_" + System.DateTime.Now.ToString("MMddyy_Hmmss")
            };

            s3Client.PutObject(putRequest);

            //Move all payments to paid
            //Update Batch File
            //Start BatchFile Workflow
        }
        public String GetS3ContextAsText(string bucket, string key)
        {
            var s3client = new Amazon.S3.AmazonS3Client();
            var response = s3client.GetObjectAsync(new GetObjectRequest {
                BucketName = bucket, Key = key
            });

            response.Wait();
            return(new StreamReader(response.Result.ResponseStream).ReadToEnd());
        }
Beispiel #16
0
        private static TransferUtility CreateTransferUtility(Dictionary <string, string> auth)
        {
            string accessKeyId     = (string)auth["AccessKeyId"];
            string secretAccessKey = (string)auth["SecretAccessKey"];
            string sessionToken    = (string)auth["SessionToken"];

            var s3Client = new Amazon.S3.AmazonS3Client(accessKeyId, secretAccessKey, sessionToken, Amazon.RegionEndpoint.USEast1);

            return(new TransferUtility(s3Client));
        }
        public List <string> ListItems(string bucketName, string serverFolder, int?maxItems = null)
        {
            //ref: http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingNetSDK.html

            List <string> listRest = new List <string>();
            int           count    = 0;

            var region = Amazon.RegionEndpoint.GetBySystemName(this.Credential.Region);

            using (var client = new Amazon.S3.AmazonS3Client(this.Credential.AcesssKey, this.Credential.SecretKey, region))
            {
                var request = new Amazon.S3.Model.ListObjectsRequest
                {
                    BucketName = bucketName,
                    MaxKeys    = 10,
                    Prefix     = serverFolder
                };

                do
                {
                    var response = client.ListObjects(request);

                    // Process response
                    foreach (Amazon.S3.Model.S3Object entry in response.S3Objects)
                    {
                        if (entry.Key == serverFolder || entry.Key == string.Format("{0}/", serverFolder) || entry.Key == string.Format("/{0}", serverFolder))
                        {
                            continue; //Folder
                        }
                        count++;

                        System.Diagnostics.Debug.WriteLine("AwsS3 -- key = {0} size = {1} / {2} items read", entry.Key, entry.Size.ToString("#,##0"), count.ToString("#,##0"));
                        listRest.Add(entry.Key);
                    }

                    // If response is truncated, set the marker to get the next
                    // set of keys.
                    if (response.IsTruncated)
                    {
                        request.Marker = response.NextMarker;
                    }
                    else
                    {
                        request = null;
                    }

                    if (maxItems.HasValue && count >= maxItems.Value)
                    {
                        break;
                    }
                } while (request != null);
            }

            return(listRest);
        }
Beispiel #18
0
        public static void PutObject(string Key, Stream InputStream)
        {
            Amazon.S3.AmazonS3Client s3Client = new Amazon.S3.AmazonS3Client(Amazon.RegionEndpoint.GetBySystemName(S3Region));

            var result = s3Client.PutObjectAsync(new PutObjectRequest {
                InputStream = InputStream, BucketName = S3BucketName, Key = Key
            }).GetAwaiter().GetResult();

            if (result.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new Exception($"Error putting object to S3: {result.HttpStatusCode.ToString()}");
            }
        }
Beispiel #19
0
        public override void OnAfterInitialize()
        {
            base.OnAfterInitialize();

            if (s3Settings == null)
            {
                s3Settings = FWUtils.ConfigUtils.GetAppSettings().AmazonCloud.S3;
            }
            if (s3Client == null)
            {
                s3Client = new Amazon.S3.AmazonS3Client(s3Settings.AccessKeyID, s3Settings.SecretAccessKey, Amazon.RegionEndpoint.GetBySystemName(s3Settings.RegionEndpoint));
            }
        }
        public void Execute(JobExecutionContext context)
        {
            //Get Current Batch
            //Close Current Batch
            //And Open New One
            //Create a new BatchFile Record
            logger.Log(LogLevel.Info, String.Format("Creating Nacha ACH File at {0}", System.DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")));

            logger.Log(LogLevel.Info, String.Format("Batching Transactions"));
            var transactions = transactionBatchService.BatchTransactions();

            FileGenerator fileGeneratorService = new FileGenerator();
            fileGeneratorService.CompanyIdentificationNumber = ConfigurationManager.AppSettings["CompanyIdentificationNumber"];
            fileGeneratorService.CompanyName = ConfigurationManager.AppSettings["CompanyName"];
            fileGeneratorService.ImmediateDestinationId = ConfigurationManager.AppSettings["ImmediateDestinationId"];
            fileGeneratorService.ImmediateDestinationName = ConfigurationManager.AppSettings["ImmediateDestinationName"];
            fileGeneratorService.ImmediateOriginId = ConfigurationManager.AppSettings["ImmediateOriginId"];
            fileGeneratorService.ImmediateOriginName = ConfigurationManager.AppSettings["ImmediateOriginName"];
            fileGeneratorService.OriginatingTransitRoutingNumber = ConfigurationManager.AppSettings["OriginatingTransitRoutingNumber"];
            fileGeneratorService.CompanyDiscretionaryData = ConfigurationManager.AppSettings["CompanyDiscretionaryData"];

            logger.Log(LogLevel.Info, String.Format("Processing Transactions"));

            var results = fileGeneratorService.ProcessFile(transactions);

            logger.Log(LogLevel.Info, String.Format("Creating Batch File"));

            StringBuilder sb = new StringBuilder();
            results.ForEach(s => sb.AppendLine(s));
            string fileContext = sb.ToString();

            logger.Log(LogLevel.Info, String.Format("Uploading Batch File to S3 Server"));

            string bucketName = ConfigurationManager.AppSettings["NachaFileBucketName"];
            if (String.IsNullOrEmpty(bucketName))
                throw new Exception("S3 bucket name for NachaFileBucketName not configured");

            Amazon.S3.AmazonS3Client s3Client = new Amazon.S3.AmazonS3Client();
            PutObjectRequest putRequest = new PutObjectRequest()
            {
                ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256,
                BucketName = bucketName,
                ContentBody = fileContext,
                Key = "NACHA_FILE_" + System.DateTime.Now.ToString("MMddyy_Hmmss")
            };
            s3Client.PutObject(putRequest);

            //Move all payments to paid
            //Update Batch File
            //Start BatchFile Workflow
        }
 /// <summary>
 /// Upload content to S3 bucket
 /// </summary>
 /// <param name="content">Content.</param>
 /// <param name="bucket">Bucket.</param>
 /// <param name="key">Key.</param>
 private void uploadContent(String content, String bucket, String key)
 {
     using (var s3client = new Amazon.S3.AmazonS3Client())
     {
         var uploadPromise = s3client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
         {
             BucketName  = bucket,
             Key         = key,
             ContentBody = content
         });
         uploadPromise.Wait();
         context.Logger.LogLine($"Uploaded s3://{bucket}/{key} ETAG {uploadPromise.Result.ETag}");
     }
 }
Beispiel #22
0
 private byte[] GetBytes(string dataStorage, string fileName)
 {
     using (var client = new Amazon.S3.AmazonS3Client(RegionEndpoint.USWest2))
     {
         using (var response = client.GetObject(dataStorage, fileName))
         {
             using (var memotyStream = new MemoryStream())
             {
                 response.ResponseStream.CopyTo(memotyStream);
                 return(memotyStream.ToArray());
             }
         }
     }
 }
        public void TestSessionCredentials()
        {
            using (var sts = new Amazon.SecurityToken.AmazonSecurityTokenServiceClient())
            {
                AWSCredentials credentials = sts.GetSessionToken().Credentials;

                var originalEC2Signature = AWSConfigs.EC2Config.UseSignatureVersion4;
                var originalS3Signature  = AWSConfigs.S3Config.UseSignatureVersion4;
                AWSConfigs.EC2Config.UseSignatureVersion4 = true;
                AWSConfigs.S3Config.UseSignatureVersion4  = true;
                try
                {
                    using (var ec2 = new Amazon.EC2.AmazonEC2Client(credentials))
                    {
                        var regions = ec2.DescribeRegions().Regions;
                        Console.WriteLine(regions.Count);
                    }

                    using (var s3 = new Amazon.S3.AmazonS3Client(credentials))
                    {
                        var buckets = s3.ListBuckets().Buckets;
                        Console.WriteLine(buckets.Count);
                    }

                    using (var swf = new Amazon.SimpleWorkflow.AmazonSimpleWorkflowClient(credentials))
                    {
                        var domains = swf.ListDomains(new Amazon.SimpleWorkflow.Model.ListDomainsRequest {
                            RegistrationStatus = "REGISTERED"
                        }).DomainInfos;
                        Console.WriteLine(domains.Infos.Count);
                    }

                    using (var swf = new Amazon.SimpleWorkflow.AmazonSimpleWorkflowClient(credentials, new Amazon.SimpleWorkflow.AmazonSimpleWorkflowConfig {
                        SignatureVersion = "4"
                    }))
                    {
                        var domains = swf.ListDomains(new Amazon.SimpleWorkflow.Model.ListDomainsRequest {
                            RegistrationStatus = "REGISTERED"
                        }).DomainInfos;
                        Console.WriteLine(domains.Infos.Count);
                    }
                }
                finally
                {
                    AWSConfigs.EC2Config.UseSignatureVersion4 = originalEC2Signature;
                    AWSConfigs.S3Config.UseSignatureVersion4  = originalS3Signature;
                }
            }
        }
Beispiel #24
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Amazon.S3.Model.GetObjectResponse Get(string key)
        {
            try
            {
                Amazon.S3.AmazonS3Client         client = new Amazon.S3.AmazonS3Client(AWAPI_File_AmazonS3_AccessKey, AWAPI_File_AmazonS3_SecretKey);
                Amazon.S3.Model.GetObjectRequest req    = new Amazon.S3.Model.GetObjectRequest();
                req.BucketName = AWAPI_File_AmazonS3_BucketName;
                req.Key        = key;
                Amazon.S3.Model.GetObjectResponse res = client.GetObject(req);

                return(res);
            }
            catch (Exception ex)
            {
            }
            return(null);
        }
Beispiel #25
0
        public void CrudCalls()
        {
            var originalBuilds = GetAllBuilds().ToList();

            var timestamp = DateTime.Now.ToFileTime().ToString();
            var newBuild  = Client.CreateBuild(new CreateBuildRequest
            {
                Name    = "TestBuild-" + timestamp,
                Version = timestamp
            }).Build;

            createdBuilds.Add(newBuild.BuildId);

            var builds = GetAllBuilds().ToList();

            Assert.AreNotEqual(originalBuilds.Count, builds.Count);

            Client.UpdateBuild(new UpdateBuildRequest
            {
                BuildId = newBuild.BuildId,
                Name    = newBuild.Name + "_2",
                Version = newBuild.Version + "_2"
            });

            var uploadCreds     = Client.RequestUploadCredentials(newBuild.BuildId);
            var storageLocation = uploadCreds.StorageLocation;
            var credentials     = uploadCreds.UploadCredentials;

            using (var s3client = new Amazon.S3.AmazonS3Client(credentials))
            {
                var putResponse = s3client.PutObject(new Amazon.S3.Model.PutObjectRequest
                {
                    BucketName  = storageLocation.Bucket,
                    Key         = storageLocation.Key,
                    ContentBody = "test content"
                });
                Console.WriteLine(putResponse.ContentLength);
            }

            Client.DeleteBuild(newBuild.BuildId);
            createdBuilds.Remove(newBuild.BuildId);

            builds = GetAllBuilds().ToList();
            Assert.AreEqual(originalBuilds.Count, builds.Count);
        }
        public override async Task <string> HandleEvent(
            APIGatewayProxyRequest request,
            ILambdaContext context,
            string deliveryId,
            BaseEvent eventPayload)
        {
            var client           = new Amazon.S3.AmazonS3Client();
            var putObjectRequest = new Amazon.S3.Model.PutObjectRequest
            {
                BucketName  = bucketName,
                Key         = deliveryId,
                ContentBody = request.Body
            };

            await client.PutObjectAsync(putObjectRequest);

            return($"Event payload uploaded to {bucketName}/{deliveryId}");
        }
Beispiel #27
0
        /// <summary>
        /// Check if the file exists or not
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Amazon.S3.Model.GetObjectResponse Get(string fileName)
        {
            try
            {
                Amazon.S3.AmazonS3Client client = new Amazon.S3.AmazonS3Client(ConfigurationLibrary.Config.fileAmazonS3AccessKey,
                                                                               ConfigurationLibrary.Config.fileAmazonS3SecreyKey);
                Amazon.S3.Model.GetObjectRequest req = new Amazon.S3.Model.GetObjectRequest();
                req.BucketName = GetBucketNameFromUrl(fileName); // ConfigurationLibrary.Config.fileAmazonS3BucketName;
                req.Key        = fileName;
                Amazon.S3.Model.GetObjectResponse res = client.GetObject(req);

                return(res);
            }
            catch (Exception ex)
            {
            }
            return(null);
        }
Beispiel #28
0
        //public void SetACL(string fileKey, bool anonymouseReadAccess)
        //{
        //    SetACLRequest aclRequest = new SetACLRequest();
        //    aclRequest.Key = fileKey;
        //    aclRequest.BucketName = AWAPI_File_AmazonS3_BucketName;

        //    S3AccessControlList aclList = new S3AccessControlList();

        //    Owner owner = new Owner();
        //    owner.Id = "oyesil";
        //    owner.DisplayName = "";
        //    aclList.Owner = owner;

        //    if (anonymouseReadAccess)
        //    {
        //        S3Grantee grantPublicRead = new S3Grantee();
        //        grantPublicRead.URI = " http://acs.amazonaws.com/groups/global/AllUsers";
        //        aclList.AddGrant(grantPublicRead, S3Permission.READ);
        //    }

        //    //Authenticated user read access
        //    S3Grantee grantAuthenticatedRead = new S3Grantee();
        //    grantAuthenticatedRead.URI = " http://acs.amazonaws.com/groups/global/AuthenticatedUsers";
        //    aclList.AddGrant(grantAuthenticatedRead, S3Permission.READ);

        //    aclRequest.ACL = aclList;


        //    Amazon.S3.AmazonS3Client client = new Amazon.S3.AmazonS3Client(AWAPI_File_AmazonS3_AccessKey, AWAPI_File_AmazonS3_SecretKey);
        //    SetACLResponse aclResponse = client.SetACL(aclRequest);

        //    client.Dispose();
        //}

        /// <summary>
        /// Deletes file from s3
        /// </summary>
        /// <returns></returns>
        public bool Delete(string fileUrl)
        {
            try
            {
                string key = GetKeyNameFromUrl(fileUrl);
                Amazon.S3.AmazonS3Client            client = new Amazon.S3.AmazonS3Client(AWAPI_File_AmazonS3_AccessKey, AWAPI_File_AmazonS3_SecretKey);
                Amazon.S3.Model.DeleteObjectRequest req    = new Amazon.S3.Model.DeleteObjectRequest();
                req.BucketName = AWAPI_File_AmazonS3_BucketName;
                req.Key        = key;

                client.DeleteObject(req);
                return(true);
            }
            catch (Exception)
            {
            }
            return(false);
        }
Beispiel #29
0
        static IDistributor GetAwsDistributor(string distributionPath)
        {
            IDistributor distributor = null;

            if (string.IsNullOrEmpty(distributionPath))
            {
                "Amazon bucket has to be provided".WriteErrorToConsole();
                Environment.Exit(ResultCodeEnum.ERROR_INPUT.Value());
                return(distributor);
            }

            string region = Environment.GetEnvironmentVariable(AmazonDistributor.AWS_REGION);

            if (string.IsNullOrWhiteSpace(region))
            {
                $"{AmazonDistributor.AWS_REGION} enviroment variable has to set".WriteErrorToConsole();
                Environment.Exit(ResultCodeEnum.ERROR_INPUT.Value());
                return(distributor);
            }

            string awsKey = Environment.GetEnvironmentVariable(Amazon.Runtime.EnvironmentVariablesAWSCredentials.ENVIRONMENT_VARIABLE_ACCESSKEY);

            if (string.IsNullOrWhiteSpace(awsKey))
            {
                $"{Amazon.Runtime.EnvironmentVariablesAWSCredentials.ENVIRONMENT_VARIABLE_ACCESSKEY} enviroment variable has to set".WriteErrorToConsole();
                Environment.Exit(ResultCodeEnum.ERROR_INPUT.Value());
                return(distributor);
            }

            string awsSecret = Environment.GetEnvironmentVariable(Amazon.Runtime.EnvironmentVariablesAWSCredentials.ENVIRONMENT_VARIABLE_SECRETKEY);

            if (string.IsNullOrWhiteSpace(awsSecret))
            {
                $"{Amazon.Runtime.EnvironmentVariablesAWSCredentials.ENVIRONMENT_VARIABLE_SECRETKEY} enviroment variable has to set".WriteErrorToConsole();
                Environment.Exit(ResultCodeEnum.ERROR_INPUT.Value());
                return(distributor);
            }


            Amazon.S3.AmazonS3Client client = new Amazon.S3.AmazonS3Client(awsKey, awsSecret, Amazon.RegionEndpoint.GetBySystemName(region));

            distributor = new AmazonDistributor(distributionPath, client);
            return(distributor);
        }
        public void TestSessionCredentials()
        {
            using (var sts = new Amazon.SecurityToken.AmazonSecurityTokenServiceClient())
            {
                AWSCredentials credentials = sts.GetSessionToken().Credentials;

                var originalEC2Signature = AWSConfigs.EC2Config.UseSignatureVersion4;
                var originalS3Signature = AWSConfigs.S3Config.UseSignatureVersion4;
                AWSConfigs.EC2Config.UseSignatureVersion4 = true;
                AWSConfigs.S3Config.UseSignatureVersion4 = true;
                try
                {

                    using (var ec2 = new Amazon.EC2.AmazonEC2Client(credentials))
                    {
                        var regions = ec2.DescribeRegions().Regions;
                        Console.WriteLine(regions.Count);
                    }

                    using (var s3 = new Amazon.S3.AmazonS3Client(credentials))
                    {
                        var buckets = s3.ListBuckets().Buckets;
                        Console.WriteLine(buckets.Count);
                    }

                    using (var swf = new Amazon.SimpleWorkflow.AmazonSimpleWorkflowClient(credentials))
                    {
                        var domains = swf.ListDomains(new Amazon.SimpleWorkflow.Model.ListDomainsRequest { RegistrationStatus = "REGISTERED" }).DomainInfos;
                        Console.WriteLine(domains.Infos.Count);
                    }

                    using (var swf = new Amazon.SimpleWorkflow.AmazonSimpleWorkflowClient(credentials, new Amazon.SimpleWorkflow.AmazonSimpleWorkflowConfig { SignatureVersion = "4" }))
                    {
                        var domains = swf.ListDomains(new Amazon.SimpleWorkflow.Model.ListDomainsRequest { RegistrationStatus = "REGISTERED" }).DomainInfos;
                        Console.WriteLine(domains.Infos.Count);
                    }
                }
                finally
                {
                    AWSConfigs.EC2Config.UseSignatureVersion4 = originalEC2Signature;
                    AWSConfigs.S3Config.UseSignatureVersion4 = originalS3Signature;
                }
            }
        }
        public void DeleteItem(string bucketName, string keyName)
        {
            //ref: http://docs.aws.amazon.com/AmazonS3/latest/dev/DeletingOneObjectUsingNetSDK.html

            var region = Amazon.RegionEndpoint.GetBySystemName(this.Credential.Region);

            using (var client = new Amazon.S3.AmazonS3Client(this.Credential.AcesssKey, this.Credential.SecretKey, region))
            {
                var deleteObjectRequest = new Amazon.S3.Model.DeleteObjectRequest
                {
                    BucketName = bucketName,
                    Key        = keyName
                };

                client.DeleteObject(deleteObjectRequest);

                Log(string.Format("AwsS3 -- Deleted {0}", keyName));
            }
        }
Beispiel #32
0
        static void Main(string[] args)
        {
            // Instantiate AWS Service Client
            Amazon.S3.AmazonS3Client Cli = new Amazon.S3.AmazonS3Client();

            // Configure Request
            var blr = new ListBucketsRequest();

            // Issue Request
            var bucketResponse = Cli.ListBuckets(blr);

            //Process Response
            foreach (var b in bucketResponse.Buckets)
            {
                Console.WriteLine($"Bucket:{b.BucketName}, Created: {b.CreationDate.ToLongDateString()}");
            }

            Console.ReadKey();
        }
Beispiel #33
0
        /// <summary>
        //
        /// </summary>
        /// <param name="siteId"></param>
        /// <param name="fileName">should be unique id: 234234546.jpg or
        ///     /images/34234234234.pjg
        /// </param>
        /// <param name="content"></param>
        /// <param name="contentType"></param>
        /// <returns>Returns the URL address of the file...</returns>
        public string Upload(string filePath, System.IO.Stream fileStream, string contentType)
        {
            Amazon.S3.AmazonS3Config config = new Amazon.S3.AmazonS3Config().
                                              WithCommunicationProtocol(Protocol.HTTP);

            string key = filePath.Replace(BaseURL, "");

            Amazon.S3.AmazonS3Client client = new Amazon.S3.AmazonS3Client(ConfigurationLibrary.Config.fileAmazonS3AccessKey,
                                                                           ConfigurationLibrary.Config.fileAmazonS3SecreyKey, config);
            //System.IO.MemoryStream stream = new System.IO.MemoryStream(content);

            try
            {
                PutObjectRequest request = new PutObjectRequest();
                request.WithInputStream(fileStream);
                request.WithBucketName(BucketName);
                request.WithKey(key);
                request.WithCannedACL(S3CannedACL.PublicRead);

                S3Response response = client.PutObject(request);
                //response.Dispose();
            }
            catch (Amazon.S3.AmazonS3Exception ex)
            {
                if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || ex.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Please check the provided AWS Credentials.");
                    Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
                }
                else
                {
                    Console.WriteLine("An error occurred with the message '{0}' when writing an object", ex.Message);
                }
            }
            client.Dispose();

            //SetACL(fileName, true);

            string fileUrl = BaseURL + key;

            return(fileUrl);
        }
Beispiel #34
0
        public override Result Execute(ConDepSettings settings, CancellationToken token)
        {
            var dynamicAwsConfig = settings.Config.OperationsConfig.Aws;
            var fileName         = Path.GetFileName(_srcFile);

            var client     = new Amazon.S3.AmazonS3Client(GetAwsCredentials(dynamicAwsConfig), RegionEndpoint.GetBySystemName((string)dynamicAwsConfig.Region));
            var objRequest = new PutObjectRequest
            {
                BucketName = _bucket,
                Key        =
                    string.IsNullOrWhiteSpace(_options.Values.TargetFolderPath)
                        ? fileName
                        : Path.Combine(_options.Values.TargetFolderPath, fileName).Replace('\\', '/'),
                FilePath = _srcFile
            };

            var response = client.PutObject(objRequest);

            return(Result(response));
        }
Beispiel #35
0
        private void PushLogToS3(string key, string log)
        {
            if (string.IsNullOrEmpty(Settings.ResultsBucket))
                return;

            try
            {
                WriteInfo("Pushing log to S3...");
                using (var s3 = new Amazon.S3.AmazonS3Client(Credentials, RegionEndpoint))
                {
                    s3.PutObjectAsync(new PutObjectRequest
                    {
                        BucketName = Settings.ResultsBucket,
                        Key = key,
                        ContentBody = log
                    }).Wait();
                }
            }
            catch(Exception e)
            {
                WriteError("Error pushing logs to S3: {0}", e);
            }
        }
Beispiel #36
0
		private void UploadToS3(string backupPath, PeriodicBackupSetup localBackupConfigs)
		{
			var awsRegion = RegionEndpoint.GetBySystemName(localBackupConfigs.AwsRegionEndpoint) ?? RegionEndpoint.USEast1;

			using (var client = new Amazon.S3.AmazonS3Client(awsAccessKey, awsSecretKey, awsRegion))
			using (var fileStream = File.OpenRead(backupPath))
			{
				var key = Path.GetFileName(backupPath);
				var request = new PutObjectRequest();
				request.WithMetaData("Description", GetArchiveDescription());
				request.WithInputStream(fileStream);
				request.WithBucketName(localBackupConfigs.S3BucketName);
				request.WithKey(key);

				using (client.PutObject(request))
				{
					logger.Info(string.Format("Successfully uploaded backup {0} to S3 bucket {1}, with key {2}",
											  Path.GetFileName(backupPath), localBackupConfigs.S3BucketName, key));
				}
			}
		}
        /// <summary>
        /// Uploads the drawing to Amazon S3
        /// </summary>
        /// <param name="dwgFilePath"></param>
        /// <returns>Presigned Url of the uploaded drawing file in Amazon S3</returns>
        public static String UploadDrawingToS3(String dwgFilePath)
        {
            String s3URL = String.Empty;

            try
            {
                if (!System.IO.File.Exists(dwgFilePath))
                    return s3URL;

                String keyName = System.IO.Path.GetFileName(dwgFilePath);

                using (Amazon.S3.IAmazonS3 client = new Amazon.S3.AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1))
                {
                    Amazon.S3.Model.PutObjectRequest putRequest1 = new Amazon.S3.Model.PutObjectRequest
                    {
                        BucketName = S3BucketName,
                        Key = keyName,
                        ContentBody = "sample text"
                    };

                    Amazon.S3.Model.PutObjectResponse response1 = client.PutObject(putRequest1);

                    Amazon.S3.Model.PutObjectRequest putRequest2 = new Amazon.S3.Model.PutObjectRequest
                    {
                        BucketName = S3BucketName,
                        Key = keyName,
                        FilePath = dwgFilePath,
                        ContentType = "application/acad"
                    };
                    putRequest2.Metadata.Add("x-amz-meta-title", keyName);

                    Amazon.S3.Model.PutObjectResponse response2 = client.PutObject(putRequest2);

                    Amazon.S3.Model.GetPreSignedUrlRequest request1 = new Amazon.S3.Model.GetPreSignedUrlRequest
                    {
                        BucketName = S3BucketName,
                        Key = keyName,
                        Expires = DateTime.Now.AddMinutes(5)
                    };

                    s3URL = client.GetPreSignedURL(request1);

                    Console.WriteLine(s3URL);
                }
            }
            catch (Amazon.S3.AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
                    amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Check the provided AWS Credentials.");
                    Console.WriteLine("For service sign up go to http://aws.amazon.com/s3");
                }
                else
                {
                    Console.WriteLine("Error occurred. Message:'{0}' when writing an object", amazonS3Exception.Message);
                }
            }
            return s3URL;
        }
		private void DoUpload(string backupPath, PeriodicBackupSetup backupConfigs)
		{
			var AWSRegion = RegionEndpoint.GetBySystemName(backupConfigs.AwsRegionEndpoint) ?? RegionEndpoint.USEast1;

			var desc = string.Format("Raven.Database.Backup {0} {1}", Database.Name,
			                     DateTimeOffset.UtcNow.ToString("u"));

			if (!string.IsNullOrWhiteSpace(backupConfigs.GlacierVaultName))
			{
				var manager = new ArchiveTransferManager(awsAccessKey, awsSecretKey, AWSRegion);
				var archiveId = manager.Upload(backupConfigs.GlacierVaultName, desc, backupPath).ArchiveId;
				logger.Info(string.Format("Successfully uploaded backup {0} to Glacier, archive ID: {1}", Path.GetFileName(backupPath),
										  archiveId));
				return;
			}

			if (!string.IsNullOrWhiteSpace(backupConfigs.S3BucketName))
			{
				var client = new Amazon.S3.AmazonS3Client(awsAccessKey, awsSecretKey, AWSRegion);

				using (var fileStream = File.OpenRead(backupPath))
				{
					var key = Path.GetFileName(backupPath);
					var request = new PutObjectRequest();
					request.WithMetaData("Description", desc);
					request.WithInputStream(fileStream);
					request.WithBucketName(backupConfigs.S3BucketName);
					request.WithKey(key);

					using (S3Response _ = client.PutObject(request))
					{
						logger.Info(string.Format("Successfully uploaded backup {0} to S3 bucket {1}, with key {2}",
							Path.GetFileName(backupPath), backupConfigs.S3BucketName, key));
						return;
					}
				}
			}
		}
        public ActionResult Edit(TimelineEvent timelineEvent)
        {
            try
            {
                var file = Request.Files["file"];

                if (file != null && file.FileName.EndsWith(".png") || file.FileName.EndsWith(".jpg"))
                {
                    var fullPath = Server.MapPath("/" + file.FileName);
                    file.SaveAs(fullPath);

                    var config = new Amazon.S3.AmazonS3Config {
                        MaxErrorRetry = 0
                    };
                    var client = new Amazon.S3.AmazonS3Client("AKIAJTSDXO36LAHQEOJA", "+HW/vJzJ+XApMYhBzVtAYElxiEZIVw24NXTYBtiG", config);

                    var req = new Amazon.S3.Model.PutObjectRequest {
                        BucketName = "10years-dawitisaak",
                        FilePath = fullPath
                    };
                    var res = client.PutObject(req);

                    timelineEvent.Image = res.AmazonId2;
                }

                // TODO: Add update logic here
                timelineEvent = timelineService.Save(timelineEvent);

                return RedirectToAction("Details", new { id = timelineEvent.Id });
            }
            catch(Exception ex)
            {
                throw ex;
                ModelState.AddModelError("Could not Edit", ex);
                return View(timelineEvent);
            }
        }