Esempio n. 1
0
        public void PresignedURLSamples()
        {
            {
                #region GetPreSignedURL Sample 1

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a CopyObject request
                GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
                {
                    BucketName = "SampleBucket",
                    Key        = "Item1",
                    Expires    = DateTime.UtcNow.AddMinutes(5)
                };

                // Get path for request
                string path = client.GetPreSignedURL(request);

                // Test by getting contents
                string contents = GetContents(path);

                #endregion
            }

            {
                #region GetPreSignedURL Sample 2

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a CopyObject request
                GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
                {
                    BucketName = "SampleBucket",
                    Key        = "Item1",
                    Expires    = DateTime.UtcNow.AddMinutes(5)
                };
                request.ResponseHeaderOverrides.ContentType        = "text/xml+zip";
                request.ResponseHeaderOverrides.ContentDisposition = "attachment; filename=dispName.pdf";
                request.ResponseHeaderOverrides.CacheControl       = "No-cache";
                request.ResponseHeaderOverrides.ContentLanguage    = "mi, en";
                request.ResponseHeaderOverrides.Expires            = "Thu, 01 Dec 1994 16:00:00 GMT";
                request.ResponseHeaderOverrides.ContentEncoding    = "x-gzip";

                // Get path for request
                string path = client.GetPreSignedURL(request);

                // Test by getting contents
                string contents = GetContents(path);

                #endregion
            }

            {
                #region GetPreSignedURL Sample 3

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a CopyObject request
                GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
                {
                    BucketName = "SampleBucket",
                    Expires    = DateTime.UtcNow.AddMinutes(5)
                };

                // Get path for request
                string path = client.GetPreSignedURL(request);

                // Retrieve objects
                string allObjects = GetContents(path);

                #endregion
            }

            {
                #region GetPreSignedURL Sample 4

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a CopyObject request
                GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
                {
                    Expires = DateTime.UtcNow.AddMinutes(5)
                };

                // Get path for request
                string path = client.GetPreSignedURL(request);

                // Retrieve buckets
                string allBuckets = GetContents(path);

                #endregion
            }

            {
                #region GetPreSignedURL Sample 5

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a CopyObject request
                GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
                {
                    BucketName = "SampleBucket",
                    Key        = "Item1",
                    Verb       = HttpVerb.PUT,
                    Expires    = DateTime.UtcNow.AddDays(10)
                };

                // Get path for request
                string path = client.GetPreSignedURL(request);

                // Prepare data
                byte[] data = UTF8Encoding.UTF8.GetBytes("Sample text.");

                // Configure request
                HttpWebRequest httpRequest = WebRequest.Create(path) as HttpWebRequest;
                httpRequest.Method        = "PUT";
                httpRequest.ContentLength = data.Length;

                // Write data to stream
                Stream requestStream = httpRequest.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();

                // Issue request
                HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;

                #endregion
            }
        }
Esempio n. 2
0
        public async Task <string> FunctionHandler(S3EventNotification evnt, ILambdaContext context)
        {
            // S3 Client and the associated Call Variables
            IAmazonS3 S3Client = new AmazonS3Client();

            S3EventNotification.S3Entity s3Event = new S3EventNotification.S3Entity();
            GetObjectRequest             ParameterFileRequest  = new GetObjectRequest();
            GetObjectResponse            ParameterFileResponse = new GetObjectResponse();
            GetPreSignedUrlRequest       PresignedKeyRequest   = new GetPreSignedUrlRequest();
            GetObjectResponse            CommitInfoResponse    = new GetObjectResponse();

            // Cloudformation Client and associated Variables
            AmazonCloudFormationClient CloudformationClient  = new AmazonCloudFormationClient();
            List <Parameter>           Parameters            = new List <Parameter>();
            DescribeStacksRequest      CurrentStacksRequest  = new DescribeStacksRequest();
            DescribeStacksResponse     CurrentStacksResponse = new DescribeStacksResponse();

            // A flag to determine if I want to create a new stack or update an existing one
            bool newstack = new bool();

            // The name of the stack which will be generated by the deployment process.
            string TargetStackName;

            // The fixed file name values the deployment process will require. If all three files are not present no deployment will take place.
            string DeploymentFileName = "master.template";
            string ParameterFileName  = "Parameters.txt";
            string CommitInfoFileName = "commitinfo.txt";


            // Write details of the s3 event to my logger
            s3Event = evnt.Records?[0].S3;
            context.Logger.Log("S3 Cloudformation Template Upload Event Recieved. Processing potential deployment.");
            context.Logger.Log("Bucket: " + s3Event.Bucket.Name);
            context.Logger.Log("Key: " + s3Event.Object.Key);

            // Preform a check to make sure the filename matches the standard required.
            if (s3Event.Object.Key.EndsWith(DeploymentFileName))
            {
                context.Logger.Log("S3 Event corresponds to a properly formatted master.template CloudFormation document. Commencing deployment.");
            }
            else
            {
                context.Logger.Log("S3 Event does not match deployment requirements. Candidates for deployment must contain the primary CloudFormation template in a master.template file.");
                return("Impropper filename. No deployment processed");
            }

            // Display the commitinfo from the deployment
            string CommitInfoKeyName = s3Event.Object.Key.Replace(DeploymentFileName, CommitInfoFileName);

            context.Logger.Log($"Looking for accompanying commitinfo file: {CommitInfoKeyName}");
            try
            {
                CommitInfoResponse = await S3Client.GetObjectAsync(s3Event.Bucket.Name, CommitInfoKeyName);

                using (StreamReader reader = new StreamReader(CommitInfoResponse.ResponseStream))
                {
                    string contents = reader.ReadToEnd();
                    context.Logger.Log(contents);
                }
            }
            catch (Exception e)
            {
                context.Logger.Log(e.Message);
                context.Logger.Log("No commitinfo.txt file detected. Aborting Deployment");
                return("No accompanying commitinfo.txt. No deployment Processed");
            }

            // Get and set associated parameters
            string ParameterKeyName = s3Event.Object.Key.Replace(DeploymentFileName, ParameterFileName);

            context.Logger.Log($"Looking for accompanying parameter file: {ParameterKeyName}");
            try
            {
                ParameterFileResponse = await S3Client.GetObjectAsync(s3Event.Bucket.Name, ParameterKeyName);

                StreamReader reader    = new StreamReader(ParameterFileResponse.ResponseStream);
                string       paramline = reader.ReadLine();
                context.Logger.Log("Parameter file line being processed: " + paramline);
                while (!string.IsNullOrWhiteSpace(paramline))
                {
                    string[] paramstrings = paramline.Split(':');
                    if (paramstrings.Length == 2)
                    {
                        Parameters.Add(new Parameter()
                        {
                            ParameterKey   = paramstrings[0],
                            ParameterValue = paramstrings[1]
                        });
                    }
                    paramline = reader.ReadLine();
                    context.Logger.Log("Parameter file line being processed: " + paramline);
                }
            }
            catch (Exception e)
            {
                context.Logger.Log(e.Message);
                context.Logger.Log("No parameter file detected. Aborting Deployment.");
                return("No accompanying commitinfo.txt.No deployment Processed");
            }

            // The name of the stack will be based on the folder structure containing the master.template document.
            // As an example, a template deployed to the S3 key Knect/RCC/master.template would generate the stack Knect-RCC
            TargetStackName = s3Event.Object.Key.Replace("/", "-");
            TargetStackName = TargetStackName.Replace("-" + DeploymentFileName, "");
            context.Logger.Log("Cloudformation Stack Name: " + TargetStackName);

            // Gets a presigned url for the cloudformation client so it can access the master.template document.
            PresignedKeyRequest.BucketName = s3Event.Bucket.Name;
            PresignedKeyRequest.Key        = s3Event.Object.Key;
            PresignedKeyRequest.Expires    = DateTime.Now.AddMinutes(5);
            string PresignedS3Key = S3Client.GetPreSignedURL(PresignedKeyRequest);

            // If a stack with the target name already exists I want to update it. Otherwise I want to create a new stack.
            try
            {
                CurrentStacksRequest.StackName = TargetStackName;
                CurrentStacksResponse          = await CloudformationClient.DescribeStacksAsync(CurrentStacksRequest);

                context.Logger.Log("A stack for the target name already exists. The existing stack will be updated.");

                newstack = false;
            }
            catch
            {
                context.Logger.Log("No stack with the target name exists. A new stack will be created.");

                newstack = true;
            }

            foreach (Parameter param in Parameters)
            {
                context.Logger.Log($"Parameter is set Key: {param.ParameterKey} with value {param.ParameterValue}");
            }

            // If there is an existing stack I will update it. Otherwise I will create a new stack
            if (newstack == true)
            {
                // Create a new stack
                CreateStackRequest CreateStack = new CreateStackRequest();
                CreateStack.StackName   = TargetStackName;
                CreateStack.TemplateURL = PresignedS3Key;
                CreateStack.Parameters  = Parameters;
                CreateStack.Capabilities.Add("CAPABILITY_NAMED_IAM");

                await CloudformationClient.CreateStackAsync(CreateStack);

                return("A stack creation request was successfully generated");
            }
            else
            {
                UpdateStackRequest updatereq = new UpdateStackRequest();
                updatereq.StackName   = TargetStackName;
                updatereq.TemplateURL = PresignedS3Key;
                updatereq.Parameters  = Parameters;
                updatereq.Capabilities.Add("CAPABILITY_NAMED_IAM");

                await CloudformationClient.UpdateStackAsync(updatereq);

                return("A stack update request was successfully generated");
            }
        }
Esempio n. 3
0
        public ActionResult AttachImage(string id, IFormFile file)
        {
            var fileTransferUtility =
                new TransferUtility(s3Client);


            string fileName = (rand.ToString() + file.FileName);

            rand++;
            // var uniqueFileName = GetUniqueFileName(file.FileName);
            var filePath = Path.Combine(file.FileName);
            var uploads  = Path.Combine(Directory.GetCurrentDirectory(), fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                // fileTransferUtility.UploadAsync(stream,bucketName, keyName);
                file.CopyToAsync(stream);
            }

            fileTransferUtility.UploadAsync(uploads, bucketName, fileName);
            GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();

            request.BucketName = bucketName;
            request.Key        = fileName;
            request.Expires    = DateTime.Now.AddYears((2));
            request.Protocol   = Protocol.HTTP;
            string url      = fileTransferUtility.S3Client.GetPreSignedURL(request);
            string username = getUsername();

            // Console.WriteLine(arguments[1]);
            Console.WriteLine("Upload 1 completed");
            if (file.Length > 0)
            {
            }

            NOTES note = _context.notes.Find(id);

            var Attachment = new Attachments {
                url = url, FileName = fileName, length = file.Length, noteID = note.noteID
            };

            _context.Add(Attachment);
            _context.SaveChanges();

            IEnumerable <Attachments> a1 = _context.attachments.AsEnumerable();
            List <mAttachments>       am = new List <mAttachments>();
            string key = "";

            foreach (Attachments at in a1)
            {
                if (at.noteID == id)
                {
                    key = at.FileName;
                    mAttachments mA = new mAttachments();
                    mA.AID = at.AID;
                    mA.url = at.url;
                    am.Add(mA);
                }
            }

            //  fileTransferUtility.S3Client.DeleteObjectAsync(new Amazon.S3.Model.DeleteObjectRequest() { BucketName = bucketname, Key =  key });

            IEnumerable <mAttachments> newA = am;

            if (ModelState.IsValid)
            {
                var a11 = new mAttachments {
                    AID = Attachment.AID, url = Attachment.url
                };
                //  string username = us.getUsername();

                return(StatusCode(201, new{ a11 }));
            }
            else
            {
                var conflict = "Bad Request";
                return(StatusCode(409, new{ result = conflict }));
            }
        }
Esempio n. 4
0
        private void ValidateS3Connection(S3StorageProviderSettingsPart part, IUpdateModel updater)
        {
            var provider = _storageProvider as S3StorageProvider;

            if (provider == null)
            {
                return;
            }

            IAmazonS3 client = null;

            try
            {
                if (part.UseCustomCredentials)
                {
                    bool valid = true;
                    if (string.IsNullOrWhiteSpace(part.AWSAccessKey))
                    {
                        updater.AddModelError("AWSAccessKey", T("Specify a value for AWS Access Key"));
                        valid = false;
                    }
                    if (string.IsNullOrWhiteSpace(part.AWSSecretKey))
                    {
                        updater.AddModelError("AWSAccessKey", T("Specify a value for AWS Secret Key"));
                        valid = false;
                    }
                    if (string.IsNullOrWhiteSpace(part.RegionEndpoint))
                    {
                        updater.AddModelError("AWSAccessKey", T("Specify a value for S3 Region Endpoint"));
                        valid = false;
                    }

                    if (!valid)
                    {
                        return;
                    }

                    client = provider.CreateClientFromCustomCredentials(part.AWSAccessKey, part.AWSSecretKey, Amazon.RegionEndpoint.GetBySystemName(part.RegionEndpoint));
                    if (client != null)
                    {
                        _notifier.Information(T("Connecting using custom credentials: OK"));
                    }
                }
                else
                {
                    var iamCredentials = provider.GetIAMCredentials();
                    client = provider.CreateClientFromIAMCredentials(iamCredentials);
                    if (client != null)
                    {
                        _notifier.Information(T("Connecting using IAM role: OK"));
                    }
                }

                // Check AWS credentials, bucket name and bucket permissions
                string bucketName = part.Record.BucketName;
                GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
                request.BucketName = bucketName;
                if (AWSConfigs.S3Config.UseSignatureVersion4)
                {
                    request.Expires = DateTime.Now.AddDays(6);
                }
                else
                {
                    request.Expires = new DateTime(2019, 12, 31);
                }
                request.Verb     = HttpVerb.HEAD;
                request.Protocol = Protocol.HTTP;
                string url = client.GetPreSignedURL(request);
                Uri    uri = new Uri(url);


                if (!AmazonS3Util.DoesS3BucketExist(client, bucketName))
                {
                    updater.AddModelError("Settings", T("Invalid bucket name. No bucket by the name {0} exists.", part.Record.BucketName));
                }
                else
                {
                    // Check for read/write permissions
                    var acl = client.GetACL(new GetACLRequest()
                    {
                        BucketName = bucketName
                    });

                    var grants = acl.AccessControlList.Grants;

                    if (!grants.Any(x => x.Permission == S3Permission.FULL_CONTROL))
                    {
                        if (!grants.Any(x => x.Permission == S3Permission.WRITE))
                        {
                            updater.AddModelError("Settings", T("You don't have write access to this bucket"));
                        }
                        if (!grants.Any(x => x.Permission == S3Permission.READ))
                        {
                            updater.AddModelError("Settings", T("You don't have read access to this bucket"));
                        }
                    }
                }

                _notifier.Information(T("All settings look okay"));
            }
            catch (AmazonS3Exception ex) {
                if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || ex.ErrorCode.Equals("InvalidSecurity")))
                {
                    updater.AddModelError("Settings", T("Invalid AWS credentials"));
                }
                else if (ex.ErrorCode != null && ex.ErrorCode.Equals("AccessDenied"))
                {
                    updater.AddModelError("Settings", T("Access denied. You don't have permission to access the bucket '{0}'", part.Record.BucketName));
                }
                else
                {
                    updater.AddModelError("Settings", T("Unknown error: {0}", ex.Message));
                }
            }
            finally {
                if (client != null)
                {
                    client.Dispose();
                }
            }
        }
Esempio n. 5
0
        private static long GetSecondsUntilExpiration(IClientConfig config, GetPreSignedUrlRequest request, bool aws4Signing)
        {
            var baselineTime = aws4Signing ? config.CorrectedUtcNow : new DateTime(1970, 1, 1);

            return(Convert.ToInt64((request.Expires.ToUniversalTime() - baselineTime).TotalSeconds));
        }
Esempio n. 6
0
        private static string GenerateUpdateJsonParquet(string oldReportName, string newReportName)
        {
            PutReportDefinitionRequest OldReportRequest = new PutReportDefinitionRequest()
            {
                ReportDefinition = new ReportDefinition()
                {
                    Compression         = CompressionFormat.Parquet,
                    Format              = ReportFormat.Parquet,
                    S3Bucket            = OutputBucket,
                    S3Prefix            = $"{AccountNumber}/",
                    S3Region            = Region,
                    TimeUnit            = TimeUnit.DAILY,
                    AdditionalArtifacts = new List <string>()
                    {
                        "ATHENA"
                    },
                    AdditionalSchemaElements = new List <string>()
                    {
                        "RESOURCES"
                    }
                }
            };

            PutReportDefinitionRequest NewReportRequest = new PutReportDefinitionRequest()
            {
                ReportDefinition = new ReportDefinition()
                {
                    Compression         = CompressionFormat.Parquet,
                    Format              = ReportFormat.Parquet,
                    S3Bucket            = OutputBucket,
                    S3Prefix            = $"{AccountNumber}/",
                    S3Region            = Region,
                    TimeUnit            = TimeUnit.DAILY,
                    AdditionalArtifacts = new List <string>()
                    {
                        "ATHENA"
                    },
                    AdditionalSchemaElements = new List <string>()
                    {
                        "RESOURCES"
                    },
                    ReportName = newReportName
                }
            };

            GetPreSignedUrlRequest Req = new GetPreSignedUrlRequest()
            {
                BucketName = PresignedUrlBucket,
                Key        = "result.txt",
                Expires    = DateTime.Now.AddMinutes(2),
                Protocol   = Protocol.HTTPS,
                Verb       = HttpVerb.PUT
            };

            string PreSignedUrl = S3Client.GetPreSignedURL(Req);
            string Json         = $@"
{{
""requestType"":""update"",
""responseUrl"":""{PreSignedUrl}"",
""stackId"":""arn:aws:cloudformation:{Region}:{AccountNumber}:stack/stack-name/{Guid.NewGuid().ToString()}"",
""requestId"":""12345678"",
""resourceType"":""Custom::TestResource"",
""logicalResourceId"":""{oldReportName}"",
""physicalResourceId"":""{oldReportName}"",
""resourceProperties"":{JsonConvert.SerializeObject(NewReportRequest)},
""oldResourceProperties"":{JsonConvert.SerializeObject(OldReportRequest)}
}}";

            Json = Json.Trim().Replace("\r", "").Replace("\n", "").Replace("\t", "");

            return(Json);
        }
Esempio n. 7
0
        /// <summary>
        /// Determines whether an S3 bucket exists or not.
        /// This is done by:
        /// 1. Creating a PreSigned Url for the bucket. To work with Signature V4 only regions, as
        /// well as Signature V4-optional regions, we keep the expiry to within the maximum for V4
        /// (which is one week).
        /// 2. Making a HEAD request to the Url
        /// </summary>
        /// <param name="bucketName">The name of the bucket to check.</param>
        /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param>
        /// <returns></returns>
        public static bool DoesS3BucketExist(IAmazonS3 s3Client, string bucketName)
        {
            if (s3Client == null)
            {
                throw new ArgumentNullException("s3Client", "The s3Client cannot be null!");
            }

            if (String.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException("bucketName", "The bucketName cannot be null or the empty string!");
            }

            var request = new GetPreSignedUrlRequest
            {
                BucketName = bucketName,
                Expires    = DateTime.Now.AddDays(1),
                Verb       = HttpVerb.HEAD,
                Protocol   = Protocol.HTTP
            };

            var url = s3Client.GetPreSignedURL(request);
            var uri = new Uri(url);

            var httpRequest = WebRequest.Create(uri) as HttpWebRequest;

            httpRequest.Method = "HEAD";
            var concreteClient = s3Client as AmazonS3Client;

            if (concreteClient != null)
            {
                concreteClient.ConfigureProxy(httpRequest);
            }

            WebException we = null;

            try
            {
                using (var httpResponse = httpRequest.GetResponse() as HttpWebResponse)
                {
                    // If all went well, the bucket was found!
                    return(true);
                }
            }
            catch (WebException ex)
            {
                we = ex;
            }

            // this code is executed when WebException is thrown
            using (var errorResponse = we.Response as HttpWebResponse)
            {
                if (errorResponse != null)
                {
                    var code = errorResponse.StatusCode;
                    return(code != HttpStatusCode.NotFound &&
                           code != HttpStatusCode.BadRequest);
                }

                // The Error Response is null which is indicative of either
                // a bad request or some other problem
                return(false);
            }
        }
Esempio n. 8
0
        public void ServerSideEncryptionBYOKPutAndGet()
        {
            var bucketName = S3TestUtils.CreateBucket(Client);

            try
            {
                Aes aesEncryption = Aes.Create();
                aesEncryption.KeySize = 256;
                aesEncryption.GenerateKey();
                string base64Key    = Convert.ToBase64String(aesEncryption.Key);
                string base64KeyMd5 = ComputeEncodedMD5FromEncodedString(base64Key);

                PutObjectRequest putRequest = new PutObjectRequest
                {
                    BucketName  = bucketName,
                    Key         = key,
                    ContentBody = "The Data To Encrypt in S3",

                    ServerSideEncryptionCustomerMethod         = ServerSideEncryptionCustomerMethod.AES256,
                    ServerSideEncryptionCustomerProvidedKey    = base64Key,
                    ServerSideEncryptionCustomerProvidedKeyMD5 = base64KeyMd5
                };

                Client.PutObject(putRequest);

                GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest
                {
                    BucketName = bucketName,
                    Key        = key,

                    ServerSideEncryptionCustomerMethod         = ServerSideEncryptionCustomerMethod.AES256,
                    ServerSideEncryptionCustomerProvidedKey    = base64Key,
                    ServerSideEncryptionCustomerProvidedKeyMD5 = base64KeyMd5
                };

                GetObjectMetadataResponse getObjectMetadataResponse = Client.GetObjectMetadata(getObjectMetadataRequest);
                Assert.AreEqual(ServerSideEncryptionCustomerMethod.AES256, getObjectMetadataResponse.ServerSideEncryptionCustomerMethod);

                GetObjectRequest getObjectRequest = new GetObjectRequest
                {
                    BucketName = bucketName,
                    Key        = key,

                    ServerSideEncryptionCustomerMethod         = ServerSideEncryptionCustomerMethod.AES256,
                    ServerSideEncryptionCustomerProvidedKey    = base64Key,
                    ServerSideEncryptionCustomerProvidedKeyMD5 = base64KeyMd5
                };

                using (GetObjectResponse getResponse = Client.GetObject(getObjectRequest))
                    using (StreamReader reader = new StreamReader(getResponse.ResponseStream))
                    {
                        string content = reader.ReadToEnd();
                        Assert.AreEqual(putRequest.ContentBody, content);
                        Assert.AreEqual(ServerSideEncryptionCustomerMethod.AES256, getResponse.ServerSideEncryptionCustomerMethod);
                    }

                GetPreSignedUrlRequest getPresignedUrlRequest = new GetPreSignedUrlRequest
                {
                    BucketName = bucketName,
                    Key        = key,
                    ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,
                    Expires = DateTime.Now.AddMinutes(5)
                };
                var url        = Client.GetPreSignedURL(getPresignedUrlRequest);
                var webRequest = HttpWebRequest.Create(url);
                webRequest.Headers.Add("x-amz-server-side-encryption-customer-algorithm", "AES256");
                webRequest.Headers.Add("x-amz-server-side-encryption-customer-key", base64Key);
                webRequest.Headers.Add("x-amz-server-side-encryption-customer-key-MD5", base64KeyMd5);

                using (var response = webRequest.GetResponse())
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        var contents = reader.ReadToEnd();
                        Assert.AreEqual(putRequest.ContentBody, contents);
                    }

                aesEncryption.GenerateKey();
                string copyBase64Key = Convert.ToBase64String(aesEncryption.Key);

                CopyObjectRequest copyRequest = new CopyObjectRequest
                {
                    SourceBucket      = bucketName,
                    SourceKey         = key,
                    DestinationBucket = bucketName,
                    DestinationKey    = "EncryptedObject_Copy",

                    CopySourceServerSideEncryptionCustomerMethod      = ServerSideEncryptionCustomerMethod.AES256,
                    CopySourceServerSideEncryptionCustomerProvidedKey = base64Key,
                    ServerSideEncryptionCustomerMethod      = ServerSideEncryptionCustomerMethod.AES256,
                    ServerSideEncryptionCustomerProvidedKey = copyBase64Key
                };
                Client.CopyObject(copyRequest);

                getObjectMetadataRequest = new GetObjectMetadataRequest
                {
                    BucketName = bucketName,
                    Key        = "EncryptedObject_Copy",

                    ServerSideEncryptionCustomerMethod      = ServerSideEncryptionCustomerMethod.AES256,
                    ServerSideEncryptionCustomerProvidedKey = copyBase64Key
                };

                getObjectMetadataResponse = Client.GetObjectMetadata(getObjectMetadataRequest);
                Assert.AreEqual(ServerSideEncryptionCustomerMethod.AES256, getObjectMetadataResponse.ServerSideEncryptionCustomerMethod);
            }
            finally
            {
                AmazonS3Util.DeleteS3BucketWithObjects(Client, bucketName);
            }
        }
 public string GetPreSignedUrl(GetPreSignedUrlRequest request) => _s3Client.GetPreSignedURL(request);
Esempio n. 10
0
        public override Uri GetInternalUri(string domain, string path, TimeSpan expire, IEnumerable <string> headers)
        {
            if (expire == TimeSpan.Zero || expire == TimeSpan.MinValue || expire == TimeSpan.MaxValue)
            {
                expire = GetExpire(domain);
            }
            if (expire == TimeSpan.Zero || expire == TimeSpan.MinValue || expire == TimeSpan.MaxValue)
            {
                return(GetUriShared(domain, path));
            }

            var pUrlRequest = new GetPreSignedUrlRequest
            {
                BucketName = _bucket,
                Expires    = DateTime.UtcNow.Add(expire),
                Key        = MakePath(domain, path),
                Protocol   = SecureHelper.IsSecure() ? Protocol.HTTPS : Protocol.HTTP,
                Verb       = HttpVerb.GET
            };

            if (headers != null && headers.Any())
            {
                var headersOverrides = new ResponseHeaderOverrides();

                foreach (var h in headers)
                {
                    if (h.StartsWith("Content-Disposition"))
                    {
                        headersOverrides.ContentDisposition = (h.Substring("Content-Disposition".Length + 1));
                    }
                    else if (h.StartsWith("Cache-Control"))
                    {
                        headersOverrides.CacheControl = (h.Substring("Cache-Control".Length + 1));
                    }
                    else if (h.StartsWith("Content-Encoding"))
                    {
                        headersOverrides.ContentEncoding = (h.Substring("Content-Encoding".Length + 1));
                    }
                    else if (h.StartsWith("Content-Language"))
                    {
                        headersOverrides.ContentLanguage = (h.Substring("Content-Language".Length + 1));
                    }
                    else if (h.StartsWith("Content-Type"))
                    {
                        headersOverrides.ContentType = (h.Substring("Content-Type".Length + 1));
                    }
                    else if (h.StartsWith("Expires"))
                    {
                        headersOverrides.Expires = (h.Substring("Expires".Length + 1));
                    }
                    else
                    {
                        throw new FormatException(string.Format("Invalid header: {0}", h));
                    }
                }
                pUrlRequest.ResponseHeaderOverrides = headersOverrides;
            }
            using (var client = GetClient())
            {
                return(MakeUri(client.GetPreSignedURL(pUrlRequest)));
            }
        }
Esempio n. 11
0
        public override UploadResult Upload(Stream stream, string fileName)
        {
            if (string.IsNullOrEmpty(s3Settings.AccessKeyID))
            {
                throw new Exception("'Access Key' must not be empty.");
            }
            if (string.IsNullOrEmpty(s3Settings.SecretAccessKey))
            {
                throw new Exception("'Secret Access Key' must not be empty.");
            }
            if (string.IsNullOrEmpty(s3Settings.Bucket))
            {
                throw new Exception("'Bucket' must not be empty.");
            }
            if (GetCurrentRegion(s3Settings) == UnknownEndpoint)
            {
                throw new Exception("Please select an endpoint.");
            }

            var region = GetCurrentRegion(s3Settings);

            var s3ClientConfig = new AmazonS3Config();

            if (region.AmazonRegion == null)
            {
                s3ClientConfig.ServiceURL = "https://" + region.Hostname;
            }
            else
            {
                s3ClientConfig.RegionEndpoint = region.AmazonRegion;
            }

            using (var client = new AmazonS3Client(GetCurrentCredentials(), s3ClientConfig))
            {
                var putRequest = new GetPreSignedUrlRequest
                {
                    BucketName  = s3Settings.Bucket,
                    Key         = GetObjectKey(fileName),
                    Verb        = HttpVerb.PUT,
                    Expires     = DateTime.UtcNow.AddMinutes(5),
                    ContentType = Helpers.GetMimeType(fileName)
                };

                var requestHeaders = new NameValueCollection();
                requestHeaders["x-amz-acl"]           = "public-read";
                requestHeaders["x-amz-storage-class"] = GetObjectStorageClass();

                putRequest.Headers["x-amz-acl"]           = "public-read";
                putRequest.Headers["x-amz-storage-class"] = GetObjectStorageClass();

                var responseHeaders = SendRequestStreamGetHeaders(client.GetPreSignedURL(putRequest), stream, Helpers.GetMimeType(fileName), requestHeaders, method: HttpMethod.PUT);
                var eTag            = responseHeaders["ETag"].Replace("\"", "");

                var uploadResult = new UploadResult();

                if (GetMd5Hash(stream) == eTag)
                {
                    uploadResult.IsSuccess = true;
                    uploadResult.URL       = GetObjectURL(putRequest.Key);
                }
                else
                {
                    uploadResult.Errors = new List <string> {
                        "Uploaded file is different."
                    };
                }

                return(uploadResult);
            }
        }
Esempio n. 12
0
        public async Task <string> UploadAttachment(IFormFile File)
        {
            try
            {
                if (await AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName))
                {
                    var transfacility = new TransferUtility(_client);

                    var folderName = Path.Combine("StaticFiles", "Images");
                    var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
                    var fileName   = File.FileName;

                    if (File.Length > 0)
                    {
                        var fullPath = Path.Combine(pathToSave, fileName);
                        var dbPath   = Path.Combine(folderName, fileName);



                        using (var stream = new FileStream(fullPath, FileMode.Create))
                        {
                            await File.CopyToAsync(stream);

                            await transfacility.UploadAsync(stream,
                                                            bucketName, fileName);
                        }



                        //  using (var fileToUpload =
                        //new FileStream(fullPath, FileMode.Open, FileAccess.Read))
                        //  {
                        //      await transfacility.UploadAsync(fileToUpload,
                        //                                 "newfeatureimages", fileName);
                        //  }

                        //GetObjectRequest request = new GetObjectRequest();
                        //request.BucketName = "newfeatureattachments";
                        //request.Key = fileName;
                        //var getresponse = await _client.GetObjectAsync(request);

                        string urlString = "";

                        GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest
                        {
                            BucketName = bucketName,
                            Key        = fileName,
                            Expires    = DateTime.UtcNow.AddHours(168)
                        };
                        urlString = _client.GetPreSignedURL(request1);



                        return(urlString);
                        // return Ok(new { dbPath });
                    }
                    else
                    {
                        return("");
                    }
                }
                else
                {
                    return("");
                }
            }
            catch (Exception ex)
            {
                return("");
            }
        }
        /// <summary>
        /// Get Presigned URL from amazon to download attachments.
        /// The presigned URL generated can be accessed maximum up to 7 days, currently it the access limit is kept for 1 day.
        /// </summary>
        /// <param name="filename">filename as string</param>
        /// <param name="contentType">content type as integer</param>
        /// <param name="bucketName">bucket name as string</param>
        /// <returns>string - presigned url to download attachment from amazon</returns>
        public static string GeneratePreSignedURL(string filename, int contentType, string bucketName)
        {
            string urlString = "";
            string fileType  = string.Empty;

            // Attachment type according to content Type sent in the request.
            switch (contentType)
            {
            case 1:
                fileType = "File";
                break;

            case 2:
                fileType = "Audio";
                break;

            case 3:
                fileType = "Video";
                break;

            case 4:
                fileType = "Link";
                break;

            default:
                fileType = "File";
                break;
            }

            //// Key name consists of the path of the folder where we want to save the attachments on amazon storage.
            string objectKey = "/messagefiles/" + fileType + "/" + filename;

            GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest
            {
                BucketName = bucketName,
                Key        = objectKey,
                Expires    = DateTime.Now.AddDays(1) // NOTE : The presigned URL generated can be accessed maximum up to 7 days, currently it the access limit is kept for 1 day.
            };

            try
            {
                urlString = client.GetPreSignedURL(request1);
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                     ||
                     amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Check the provided AWS Credentials.");
                    Console.WriteLine(
                        "To sign up for service, go to http://aws.amazon.com/s3");
                }
                else
                {
                    Console.WriteLine(
                        "Error occurred. Message:'{0}' when listing objects",
                        amazonS3Exception.Message);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return(urlString);
        }
Esempio n. 14
0
        public string GenerateURL(string objectName)
        {
            AmazonS3 s3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSKey, AWSSecretKey);


            string url = "";

            try
            {
                using (s3Client)
                {
                    GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
                    request.WithBucketName(bucketName);
                    request.WithKey(prefix + objectName);
                    if (expires == 0)
                    {
                        request.WithExpires(DateTime.Now.AddYears(20));
                    }
                    else
                    {
                        request.WithExpires(DateTime.Now.AddDays(expires));
                    }


                    url = s3Client.GetPreSignedURL(request);
                }
            }
            catch (AmazonS3Exception)
            {
            }

            if (!isPrivate)
            {
                url = url.Substring(0, url.IndexOf("?")); //remove QS
            }

            if (!https)
            {
                url = url.Replace(@"https://", @"http://");
            }

            if (shorten)
            {
                //create is.gd url
                string resp = "";
                try
                {
                    string isgd = @"http://is.gd/create.php?format=simple&url=" + System.Uri.EscapeDataString(url);
                    resp = new System.Net.WebClient().DownloadString(isgd);
                }
                catch (Exception) { }
                if (resp != "")
                {
                    url = resp;
                    if (appendExt)
                    {
                        if (System.IO.Path.GetExtension(objectName).ToLower() == "png")
                        {
                            url += "?" + System.IO.Path.GetExtension(objectName).ToLower();
                        }
                        else
                        {
                            url += "?" + System.IO.Path.GetFileName(objectName).ToLower();
                        }
                    }
                }
            }


            return(url);
        }
Esempio n. 15
0
        /// <summary>
        /// Create a signed URL allowing access to a resource that would
        /// usually require authentication.
        /// </summary>
        /// <remarks>
        /// <para>
        /// When using query string authentication you create a query,
        /// specify an expiration time for the query, sign it with your
        /// signature, place the data in an HTTP request, and distribute
        /// the request to a user or embed the request in a web page.
        /// </para>
        /// <para>
        /// A PreSigned URL can be generated for GET, PUT, DELETE and HEAD
        /// operations on your bucketName, keys, and versions.
        /// </para>
        /// </remarks>
        /// <param name="request">The GetPreSignedUrlRequest that defines the
        /// parameters of the operation.</param>
        /// <returns>A string that is the signed http request.</returns>
        /// <exception cref="T:System.ArgumentException" />
        /// <exception cref="T:System.ArgumentNullException" />
        public string GetPreSignedURL(GetPreSignedUrlRequest request)
        {
            if (Credentials == null)
            {
                throw new AmazonS3Exception("Credentials must be specified, cannot call method anonymously");
            }

            if (request == null)
            {
                throw new ArgumentNullException("request", "The PreSignedUrlRequest specified is null!");
            }

            if (!request.IsSetExpires())
            {
                throw new InvalidOperationException("The Expires specified is null!");
            }

            var aws4Signing = AWSConfigs.S3UseSignatureVersion4;
            var region      = AWS4Signer.DetermineRegion(Config);

            if (aws4Signing && string.IsNullOrEmpty(region))
            {
                throw new InvalidOperationException("To use AWS4 signing, a region must be specified in the client configuration using the AuthenticationRegion or Region properties, or be determinable from the service URL.");
            }

            if (region.Equals(RegionEndpoint.CNNorth1.SystemName, StringComparison.OrdinalIgnoreCase))
            {
                aws4Signing = true;
            }

            var immutableCredentials = Credentials.GetCredentials();
            var irequest             = Marshall(request, immutableCredentials.AccessKey, immutableCredentials.Token, aws4Signing);

            irequest.Endpoint = DetermineEndpoint(irequest);
            ProcessRequestHandlers(irequest);
            var metrics = new RequestMetrics();

            string authorization;

            if (aws4Signing)
            {
                var aws4Signer    = new AWS4PreSignedUrlSigner();
                var signingResult = aws4Signer.SignRequest(irequest,
                                                           this.Config,
                                                           metrics,
                                                           immutableCredentials.AccessKey,
                                                           immutableCredentials.SecretKey);
                authorization = "&" + signingResult.ForQueryParameters;
            }
            else
            {
                signer.SignRequest(irequest, metrics, immutableCredentials.AccessKey, immutableCredentials.SecretKey);
                authorization = irequest.Headers[S3QueryParameter.Authorization.ToString()];
                authorization = authorization.Substring(authorization.IndexOf(":", StringComparison.Ordinal) + 1);
                authorization = "&Signature=" + AmazonS3Util.UrlEncode(authorization, false);
            }

            Uri      url      = ComposeUrl(irequest, irequest.Endpoint);
            string   result   = url.AbsoluteUri + authorization;
            Protocol protocol = DetermineProtocol();

            if (request.Protocol != protocol)
            {
                switch (protocol)
                {
                case Protocol.HTTP:
                    result = result.Replace("http://", "https://");
                    break;

                case Protocol.HTTPS:
                    result = result.Replace("https://", "http://");
                    break;
                }
            }
            return(result);
        }
        public async Task <S3UploadResponse> UploadFileAsync(string bucketName, string filebase64)
        {
            try
            {
                var credentials = new Amazon.Runtime.BasicAWSCredentials(Constants.access_key, Constants.secret_key);

                var S3Client = new AmazonS3Client(credentials, Constants.regionEndpoint);

                var fileTransferUtility = new TransferUtility(S3Client);
                var guid = Guid.NewGuid().ToString("N").Substring(0, 6);
                try
                {
                    byte[] bytes = Convert.FromBase64String(filebase64);

                    using (S3Client)
                    {
                        var request = new PutObjectRequest
                        {
                            BucketName = bucketName,
                            CannedACL  = S3CannedACL.PublicRead,
                            Key        = string.Format("userImages/{0}", guid + ".jpg")
                        };
                        using (var ms = new MemoryStream(bytes))
                        {
                            request.InputStream = ms;
                            await S3Client.PutObjectAsync(request);
                        }
                        GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest();
                        request1.BucketName = bucketName;
                        request1.Key        = string.Format("userImages/{0}", guid + ".jpg");
                        request1.Expires    = DateTime.Now.AddHours(2);
                        request1.Protocol   = Protocol.HTTP;
                        string url = S3Client.GetPreSignedURL(request1);

                        Console.WriteLine(url);

                        return(new S3UploadResponse
                        {
                            Message = "Success",
                            Status = HttpStatusCode.OK,
                            url = url.Split('?')[0].ToString()
                        });
                    }
                }
                catch (Exception ex)
                {
                    return(new S3UploadResponse
                    {
                        Message = ex.Message,
                        Status = HttpStatusCode.BadRequest,
                        url = null
                    });
                }
            }
            catch (AmazonS3Exception ex)
            {
                return(new S3UploadResponse
                {
                    Message = "Error encountered on server. Message : '{0}' when writting an object" + ex.Message,
                    Status = HttpStatusCode.BadRequest,
                    url = null
                });
            }
            catch (Exception ex)
            {
                return(new S3UploadResponse
                {
                    Message = "Error encountered on server. Message : '{0}' when writting an object" + ex.Message,
                    Status = HttpStatusCode.BadRequest,
                    url = null
                });
            }
        }
Esempio n. 17
0
        public static async System.Threading.Tasks.Task <bool> DoesS3BucketExistAsync(IAmazonS3 s3Client, string bucketName)
        {
            if (s3Client == null)
            {
                throw new ArgumentNullException("s3Client", "The s3Client cannot be null!");
            }

            if (String.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException("bucketName", "The bucketName cannot be null or the empty string!");
            }

            var request = new GetPreSignedUrlRequest
            {
                BucketName = bucketName,
                Expires    = s3Client.Config.CorrectedUtcNow.ToLocalTime().AddDays(1),
                Verb       = HttpVerb.HEAD,
                Protocol   = Protocol.HTTP
            };

            var url = s3Client.GetPreSignedURL(request);
            var uri = new Uri(url);

            var httpRequest = WebRequest.Create(uri) as HttpWebRequest;

            httpRequest.Method = "HEAD";
            var concreteClient = s3Client as AmazonS3Client;

            if (concreteClient != null)
            {
                concreteClient.ConfigureProxy(httpRequest);
            }

            try
            {
#if PCL
                var result = httpRequest.BeginGetResponse(null, null);
                using (var httpResponse = httpRequest.EndGetResponse(result) as HttpWebResponse)
#else
                using (var httpResponse = await httpRequest.GetResponseAsync().ConfigureAwait(false) as HttpWebResponse)
#endif
                {
                    // If all went well, the bucket was found!
                    return(true);
                }
            }
            catch (WebException we)
            {
                using (var errorResponse = we.Response as HttpWebResponse)
                {
                    if (errorResponse != null)
                    {
                        var code = errorResponse.StatusCode;
                        return(code != HttpStatusCode.NotFound &&
                               code != HttpStatusCode.BadRequest);
                    }

                    // The Error Response is null which is indicative of either
                    // a bad request or some other problem
                    return(false);
                }
            }
        }
Esempio n. 18
0
        public string GetUrl(IAmazonS3 client)
        {
            var urlRequest = new GetPreSignedUrlRequest();

            return(client.GetPreSignedURL(urlRequest));
        }
Esempio n. 19
0
        public override UploadResult Upload(Stream stream, string fileName)
        {
            if (string.IsNullOrEmpty(s3Settings.AccessKeyID))
            {
                Errors.Add("'Access Key' must not be empty.");
            }
            if (string.IsNullOrEmpty(s3Settings.SecretAccessKey))
            {
                Errors.Add("'Secret Access Key' must not be empty.");
            }
            if (string.IsNullOrEmpty(s3Settings.Bucket))
            {
                Errors.Add("'Bucket' must not be empty.");
            }
            if (GetCurrentRegion(s3Settings) == UnknownEndpoint)
            {
                Errors.Add("Please select an endpoint.");
            }

            if (IsError)
            {
                return(null);
            }

            var region = GetCurrentRegion(s3Settings);

            var s3ClientConfig = new AmazonS3Config();

            if (region.AmazonRegion == null)
            {
                s3ClientConfig.ServiceURL = URLHelpers.ForcePrefix(region.Hostname);
            }
            else
            {
                s3ClientConfig.RegionEndpoint = region.AmazonRegion;
            }

            using (var client = new AmazonS3Client(GetCurrentCredentials(), s3ClientConfig))
            {
                var putRequest = new GetPreSignedUrlRequest
                {
                    BucketName  = s3Settings.Bucket,
                    Key         = GetObjectKey(fileName),
                    Verb        = HttpVerb.PUT,
                    Expires     = DateTime.UtcNow.AddMinutes(5),
                    ContentType = Helpers.GetMimeType(fileName)
                };

                var requestHeaders = new NameValueCollection();
                requestHeaders["x-amz-acl"]           = "public-read";
                requestHeaders["x-amz-storage-class"] = GetObjectStorageClass();

                putRequest.Headers["x-amz-acl"]           = "public-read";
                putRequest.Headers["x-amz-storage-class"] = GetObjectStorageClass();

                var responseHeaders = SendRequestStreamGetHeaders(client.GetPreSignedURL(putRequest), stream, Helpers.GetMimeType(fileName), requestHeaders, method: HttpMethod.PUT);
                if (responseHeaders.Count == 0)
                {
                    Errors.Add("Upload to Amazon S3 failed. Check your access credentials.");
                    return(null);
                }

                var eTag = responseHeaders.Get("ETag");
                if (eTag == null)
                {
                    Errors.Add("Upload to Amazon S3 failed.");
                    return(null);
                }

                if (GetMd5Hash(stream) == eTag.Replace("\"", ""))
                {
                    return(new UploadResult {
                        IsSuccess = true, URL = GetObjectURL(putRequest.Key)
                    });
                }

                Errors.Add("Upload to Amazon S3 failed, uploaded data did not match.");
                return(null);
            }
        }
 /// <summary>
 /// Create a signed URL allowing access to a resource that would
 /// usually require authentication.
 /// </summary>
 /// <remarks>
 /// <para>
 /// When using query string authentication you create a query,
 /// specify an expiration time for the query, sign it with your
 /// signature, place the data in an HTTP request, and distribute
 /// the request to a user or embed the request in a web page.
 /// </para>
 /// <para>
 /// A PreSigned URL can be generated for GET, PUT, DELETE and HEAD
 /// operations on your bucketName, keys, and versions.
 /// </para>
 /// </remarks>
 /// <param name="request">The GetPreSignedUrlRequest that defines the
 /// parameters of the operation.</param>
 /// <returns>A string that is the signed http request.</returns>
 /// <exception cref="T:System.ArgumentException" />
 /// <exception cref="T:System.ArgumentNullException" />
 public string GetPreSignedURL(GetPreSignedUrlRequest request)
 {
     return(GetPreSignedURLInternal(request));
 }
 public string GetPreSignedURL(GetPreSignedUrlRequest request)
 {
     throw new NotImplementedException();
 }
Esempio n. 22
0
        static void Main(string[] args)
        {
            // Create a reference of Windows Azure Storage Account
            StorageCredentials  azureCloudStorageCredentials = new StorageCredentials(azureStorageAccountName, azureStorageAccountKey);
            CloudStorageAccount azureCloudStorageAccount     = new CloudStorageAccount(azureCloudStorageCredentials, true);
            // Get a reference of Blob Container where the objects will be copied
            var blobContainer = azureCloudStorageAccount.CreateCloudBlobClient().GetContainerReference(azureBlobContainerName);

            // Create blob container if needed
            Console.WriteLine("Searching for the \"" + azureBlobContainerName + "\" blob container...");
            if (blobContainer.CreateIfNotExists())
            {
                Console.WriteLine("The \"" + azureBlobContainerName + "\" blob container was not found.\nThe container has now been created.");
            }
            else
            {
                Console.WriteLine("The \"" + azureBlobContainerName + "\" blob container was found.");
            }
            Console.WriteLine();
            // Create a reference of Amazon Client
            AmazonS3Client amazonClient = new AmazonS3Client(amazonAccessKey, amazonSecretKey, Amazon.RegionEndpoint.USEast1);

            // Initialize dictionary
            CopyBlobProgress = new Dictionary <string, CopyBlobProgress>();

            string[] months      = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            DateTime today       = DateTime.Now;
            string   todayString = today.ToString("d");

            string[] dateParsed = todayString.Split('/');
            int      day        = Int32.Parse(dateParsed[1]);
            int      month;

            if (Int32.Parse(dateParsed[0]) >= 2)
            {
                month = Int32.Parse(dateParsed[0]) - 2;
            }
            else
            {
                month = Int32.Parse(dateParsed[0]) + 10;
            }
            int year;

            if (day == 1 && Int32.Parse(dateParsed[0]) == 1)
            {
                year = Int32.Parse(dateParsed[2]) - 1;
            }
            else
            {
                year = Int32.Parse(dateParsed[2]);
            }

            string continuationToken   = null;
            bool   continueListObjects = true;

            // Since ListObjects returns a maximum of 1000 objects in a single call,
            // the function will have to be called repeatedly until all objects are returned.
            while (continueListObjects)
            {
                ListObjectsRequest requestToListObjects = new ListObjectsRequest()
                {
                    BucketName = amazonBucketName,
                    Marker     = continuationToken,
                    MaxKeys    = 100,
                };
                Console.WriteLine("Attempting to list objects from the \"" + amazonBucketName + "\" S3 AWS bucket...");
                // List objects from Amazon S3
                var listObjectsResponse = amazonClient.ListObjects(requestToListObjects);
                // Get the list of objects
                var objectsFetchedSoFar = listObjectsResponse.S3Objects;
                Console.WriteLine("Object listing complete. Now beginning the copy process...");
                Console.WriteLine();
                // See if more objects are available. We'll first process the objects fetched
                // and continue the loop to get next set of objects.
                continuationToken = listObjectsResponse.NextMarker;
                foreach (var s3Object in objectsFetchedSoFar)
                {
                    string objectKey = s3Object.Key;
                    //if (day == 6 && objectKey.Contains("AWS-") && objectKey.Contains(months[month]) && objectKey.Contains(dateParsed[2])) { }
                    //else if (objectKey.Contains("AWS-") && objectKey.Length == 35) continue;
                    if (day == 1 && !objectKey.Contains("CloudTrail-" + months[month] + "-" + dateParsed[2]))
                    {
                        continue;
                    }
                    else if (day != 1)
                    {
                        if (!objectKey.Contains("CloudTrail-" + months[month + 1] + "-" + dateParsed[2]))
                        {
                            continue;
                        }
                    }
                    // Since ListObjects returns both files and folders, for now we'll skip folders
                    // The way we'll check this is by checking the value of S3 Object Key. If it
                    // end with "/" we'll assume it's a folder.
                    if (objectKey.Contains("CloudTrail-Logs-Final"))
                    {
                        // Construct the URL to copy
                        string urlToCopy = string.Format(CultureInfo.InvariantCulture, objectUrlFormat, amazonBucketName, objectKey);
                        // Create an instance of CloudBlockBlob
                        string         azureObjectKey = objectKey.Replace("CloudTrail-Logs-Final/", "");
                        CloudBlockBlob blockBlob      = blobContainer.GetBlockBlobReference(azureObjectKey);
                        var            blockBlobUrl   = blockBlob.Uri.AbsoluteUri;
                        if (!CopyBlobProgress.ContainsKey(blockBlobUrl))
                        {
                            CopyBlobProgress.Add(blockBlobUrl, new CopyBlobProgress()
                            {
                                Status = CopyStatus.NotStarted,
                            });
                            // Start the copy process. We would need to put it in try/catch block
                            // as the copy from Amazon S3 will only work with publicly accessible objects.
                            try {
                                Console.WriteLine(string.Format("Attempting to copy from \"{0}\" to \"{1}\"...", urlToCopy, blockBlobUrl));
                                GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest()
                                {
                                    BucketName = amazonBucketName,
                                    Key        = objectKey,
                                    Expires    = DateTime.Now.AddMinutes(1)
                                };
                                urlToCopy = amazonClient.GetPreSignedURL(request1);
                                blockBlob.StartCopy(new Uri(urlToCopy));
                                CopyBlobProgress[blockBlobUrl].Status = CopyStatus.Started;
                            } catch (Exception exception) {
                                CopyBlobProgress[blockBlobUrl].Status = CopyStatus.Failed;
                                CopyBlobProgress[blockBlobUrl].Error  = exception;
                            }
                        }
                    }
                }
                Console.WriteLine();
                Console.WriteLine("-");
                Console.WriteLine();
                Console.WriteLine("Checking the status of copy processes...");

                Microsoft.WindowsAzure.Storage.Blob.CopyStatus[] tracker = new Microsoft.WindowsAzure.Storage.Blob.CopyStatus[1000];
                // Now track the progress
                bool checkForBlobCopyStatus = true;
                while (checkForBlobCopyStatus)
                {
                    // List blobs in the blob container
                    var  blobsList = blobContainer.ListBlobs("", true, BlobListingDetails.Copy);
                    int  x         = 0;
                    bool written   = false;
                    foreach (var blob in blobsList)
                    {
                        var tempBlockBlob = blob as CloudBlob;
                        var copyStatus    = tempBlockBlob.CopyState;
                        if (CopyBlobProgress.ContainsKey(tempBlockBlob.Uri.AbsoluteUri))
                        {
                            var copyBlobProgress = CopyBlobProgress[tempBlockBlob.Uri.AbsoluteUri];
                            if (copyStatus != null && !copyStatus.Status.Equals(tracker[x]))
                            {
                                tracker[x] = copyStatus.Status;
                                Console.Write(string.Format("Status of \"{0}\" blob copy: ", tempBlockBlob.Uri.AbsoluteUri, copyStatus.Status));
                                switch (copyStatus.Status)
                                {
                                case Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Aborted:
                                    if (copyBlobProgress != null)
                                    {
                                        copyBlobProgress.Status = CopyStatus.Aborted;
                                        Console.WriteLine("Aborted!");
                                    }
                                    break;

                                case Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Failed:
                                    if (copyBlobProgress != null)
                                    {
                                        copyBlobProgress.Status = CopyStatus.Failed;
                                        Console.WriteLine("Failed!");
                                    }
                                    break;

                                case Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Invalid:
                                    if (copyBlobProgress != null)
                                    {
                                        copyBlobProgress.Status = CopyStatus.Invalid;
                                        Console.WriteLine("Invalid!");
                                    }
                                    break;

                                case Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Pending:
                                    if (copyBlobProgress != null)
                                    {
                                        copyBlobProgress.Status = CopyStatus.Pending;
                                        Console.WriteLine("Pending...");
                                    }
                                    break;

                                case Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Success:
                                    if (copyBlobProgress != null)
                                    {
                                        copyBlobProgress.Status = CopyStatus.Success;
                                        Console.WriteLine("SUCCESS!");
                                    }
                                    break;
                                }
                                written = true;
                            }
                        }
                        ++x;
                    }
                    if (written)
                    {
                        Console.WriteLine();
                        written = false;
                    }
                    var pendingBlob = CopyBlobProgress.FirstOrDefault(c => c.Value.Status == CopyStatus.Pending);
                    if (string.IsNullOrWhiteSpace(pendingBlob.Key))
                    {
                        checkForBlobCopyStatus = false;
                    }
                }
                if (string.IsNullOrWhiteSpace(continuationToken))
                {
                    continueListObjects = false;
                }
                Console.WriteLine("-");
                Console.WriteLine("");
            }
            Console.WriteLine("Process completed!");
            Console.Write("Press any key to terminate the program...");
            Console.ReadLine();
        }
Esempio n. 23
0
        /// <summary>
        /// Marshalls the parameters for a presigned url for a preferred signing protocol.
        /// </summary>
        /// <param name="getPreSignedUrlRequest"></param>
        /// <param name="accessKey"></param>
        /// <param name="token"></param>
        /// <param name="aws4Signing">
        /// True if AWS4 signing will be used; if the expiry period in the request exceeds the
        /// maximum allowed for AWS4 (one week), an ArgumentException is thrown.
        /// </param>
        /// <returns></returns>
        private static IRequest Marshall(IClientConfig config,
                                         GetPreSignedUrlRequest getPreSignedUrlRequest,
                                         string accessKey,
                                         string token,
                                         bool aws4Signing)
        {
            IRequest request = new DefaultRequest(getPreSignedUrlRequest, "AmazonS3");

            request.HttpMethod = getPreSignedUrlRequest.Verb.ToString();

            var headers = getPreSignedUrlRequest.Headers;

            foreach (var key in headers.Keys)
            {
                request.Headers[key] = headers[key];
            }

            AmazonS3Util.SetMetadataHeaders(request, getPreSignedUrlRequest.Metadata);

            if (!string.IsNullOrEmpty(token))
            {
                request.Headers[HeaderKeys.XAmzSecurityTokenHeader] = token;
            }

            if (getPreSignedUrlRequest.ServerSideEncryptionMethod != null && getPreSignedUrlRequest.ServerSideEncryptionMethod != ServerSideEncryptionMethod.None)
            {
                request.Headers.Add(HeaderKeys.XAmzServerSideEncryptionHeader, S3Transforms.ToStringValue(getPreSignedUrlRequest.ServerSideEncryptionMethod));
            }
            if (getPreSignedUrlRequest.IsSetServerSideEncryptionCustomerMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerAlgorithmHeader, getPreSignedUrlRequest.ServerSideEncryptionCustomerMethod);
            }
            if (getPreSignedUrlRequest.IsSetServerSideEncryptionKeyManagementServiceKeyId())
            {
                request.Headers.Add(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader, getPreSignedUrlRequest.ServerSideEncryptionKeyManagementServiceKeyId);
            }

            if (getPreSignedUrlRequest.IsSetRequestPayer() && getPreSignedUrlRequest.RequestPayer == RequestPayer.Requester)
            {
                request.Parameters.Add("x-amz-request-payer", RequestPayer.Requester.Value);
            }

            var queryParameters = request.Parameters;

            var uriResourcePath = new StringBuilder("/");

            if (!string.IsNullOrEmpty(getPreSignedUrlRequest.BucketName))
            {
                uriResourcePath.Append(S3Transforms.ToStringValue(getPreSignedUrlRequest.BucketName));
            }
            if (!string.IsNullOrEmpty(getPreSignedUrlRequest.Key))
            {
                if (uriResourcePath.Length > 1)
                {
                    uriResourcePath.Append("/");
                }
                uriResourcePath.Append(S3Transforms.ToStringValue(getPreSignedUrlRequest.Key));
            }

            var expires = GetSecondsUntilExpiration(config, getPreSignedUrlRequest, aws4Signing);

            if (aws4Signing && expires > AWS4PreSignedUrlSigner.MaxAWS4PreSignedUrlExpiry)
            {
                var msg = string.Format(CultureInfo.InvariantCulture, "The maximum expiry period for a presigned url using AWS4 signing is {0} seconds",
                                        AWS4PreSignedUrlSigner.MaxAWS4PreSignedUrlExpiry);
                throw new ArgumentException(msg);
            }

            queryParameters.Add(aws4Signing ? "X-Amz-Expires" : "Expires", expires.ToString(CultureInfo.InvariantCulture));

            if (!string.IsNullOrEmpty(token))
            {
                queryParameters.Add("x-amz-security-token", token);
            }
            if (!aws4Signing)
            {
                queryParameters.Add("AWSAccessKeyId", accessKey);
            }
            if (getPreSignedUrlRequest.IsSetVersionId())
            {
                request.AddSubResource("versionId", S3Transforms.ToStringValue(getPreSignedUrlRequest.VersionId));
            }

            var responseHeaderOverrides = getPreSignedUrlRequest.ResponseHeaderOverrides;

            if (!string.IsNullOrEmpty(responseHeaderOverrides.CacheControl))
            {
                queryParameters.Add("response-cache-control", responseHeaderOverrides.CacheControl);
            }
            if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentType))
            {
                queryParameters.Add("response-content-type", responseHeaderOverrides.ContentType);
            }
            if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentLanguage))
            {
                queryParameters.Add("response-content-language", responseHeaderOverrides.ContentLanguage);
            }
            if (!string.IsNullOrEmpty(responseHeaderOverrides.Expires))
            {
                queryParameters.Add("response-expires", responseHeaderOverrides.Expires);
            }
            if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentDisposition))
            {
                queryParameters.Add("response-content-disposition", responseHeaderOverrides.ContentDisposition);
            }
            if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentEncoding))
            {
                queryParameters.Add("response-content-encoding", responseHeaderOverrides.ContentEncoding);
            }

            // Add custom parameters to be included and signed
            foreach (string k in getPreSignedUrlRequest.Parameters.Keys)
            {
                queryParameters.Add(k, getPreSignedUrlRequest.Parameters[k]);
            }

            request.ResourcePath   = uriResourcePath.ToString();
            request.UseQueryString = true;

            return(request);
        }
Esempio n. 24
0
        private static async Task Run()
        {
            var dataTransformer = new DataTransformer();
            var builder         = new ConfigurationBuilder()
                                  .SetBasePath(Path.Combine(AppContext.BaseDirectory))
                                  .AddJsonFile("appsettings.json");

            var config     = builder.Build();
            var awsOptions = config.GetAWSOptions();

            Console.WriteLine("Connecting to AWS");

            using (IAmazonS3 s3Client = awsOptions.CreateServiceClient <IAmazonS3>())
            {
                try
                {
                    var r = s3Client.ListBucketsAsync().Result;
                    Console.WriteLine($"You have {r.Buckets.Count} buckets");

                    //await s3Client.EnsureBucketExistsAsync(InputBucketName);
                    //await s3Client.EnsureBucketExistsAsync(OuputBucketName);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }


                Console.WriteLine("Buckets created");

                while (_token.IsCancellationRequested == false)
                {
                    var inputFilesResponse = await s3Client.ListObjectsAsync(InputBucketName);

                    Console.WriteLine($"Found {inputFilesResponse.S3Objects.Count} objects to transform");

                    foreach (var inputFile in inputFilesResponse.S3Objects)
                    {
                        var s3Object = await s3Client.GetObjectAsync(inputFile.BucketName, inputFile.Key);

                        var contents = await s3Object.ContentAsString();

                        var transformedContent = dataTransformer.Transform(contents);

                        await s3Client.WriteContent(OuputBucketName, inputFile.Key, transformedContent);

                        Console.WriteLine("File transformed");

                        var presignRequest = new GetPreSignedUrlRequest
                        {
                            BucketName = OuputBucketName,
                            Key        = inputFile.Key,
                            Expires    = DateTime.Now.AddMinutes(15),
                            Protocol   = Protocol.HTTP,
                            Verb       = HttpVerb.GET
                        };

                        var presignedUrl = s3Client.GetPreSignedURL(presignRequest);
                        Console.WriteLine(presignedUrl);
                    }

                    _token.WaitHandle.WaitOne(TimeSpan.FromSeconds(5));
                }
            }
        }
Esempio n. 25
0
        /// <summary>
        /// Create a signed URL allowing access to a resource that would
        /// usually require authentication.
        /// </summary>
        /// <remarks>
        /// <para>
        /// When using query string authentication you create a query,
        /// specify an expiration time for the query, sign it with your
        /// signature, place the data in an HTTP request, and distribute
        /// the request to a user or embed the request in a web page.
        /// </para>
        /// <para>
        /// A PreSigned URL can be generated for GET, PUT, DELETE and HEAD
        /// operations on your bucketName, keys, and versions.
        /// </para>
        /// </remarks>
        /// <param name="request">The GetPreSignedUrlRequest that defines the
        /// parameters of the operation.</param>
        /// <param name="useSigV2Fallback">determines if signing will fall back to SigV2 if the
        /// signing region is us-east-1</param>
        /// <returns>A string that is the signed http request.</returns>
        /// <exception cref="T:System.ArgumentException" />
        /// <exception cref="T:System.ArgumentNullException" />
        internal string GetPreSignedURLInternal(GetPreSignedUrlRequest request, bool useSigV2Fallback = true)
        {
            if (Credentials == null)
            {
                throw new AmazonS3Exception("Credentials must be specified, cannot call method anonymously");
            }

            if (request == null)
            {
                throw new ArgumentNullException("request", "The PreSignedUrlRequest specified is null!");
            }

            if (!request.IsSetExpires())
            {
                throw new InvalidOperationException("The Expires specified is null!");
            }

            var aws4Signing = AWSConfigsS3.UseSignatureVersion4;
            var region      = AWS4Signer.DetermineSigningRegion(Config, "s3", alternateEndpoint: null, request: null);

            if (aws4Signing && string.IsNullOrEmpty(region))
            {
                throw new InvalidOperationException("To use AWS4 signing, a region must be specified in the client configuration using the AuthenticationRegion or Region properties, or be determinable from the service URL.");
            }

            RegionEndpoint endpoint = RegionEndpoint.GetBySystemName(region);

            if (endpoint.GetEndpointForService("s3").SignatureVersionOverride == "4" || endpoint.GetEndpointForService("s3").SignatureVersionOverride == null)
            {
                aws4Signing = true;
            }

            var fallbackToSigV2 = useSigV2Fallback && !AWSConfigsS3.UseSigV4SetExplicitly;

            if (endpoint == RegionEndpoint.USEast1 && fallbackToSigV2)
            {
                aws4Signing = false;
            }

            // If the expiration is longer than SigV4 will allow then automatically use SigV2 instead.
            // But only if the region we're signing for allows SigV2.
            if (aws4Signing)
            {
                var secondsUntilExpiration = GetSecondsUntilExpiration(this.Config, request, aws4Signing);

                if (secondsUntilExpiration > AWS4PreSignedUrlSigner.MaxAWS4PreSignedUrlExpiry &&
                    endpoint.GetEndpointForService("s3").SignatureVersionOverride == "2")
                {
                    aws4Signing = false;
                }
            }

            var immutableCredentials = Credentials.GetCredentials();
            var irequest             = Marshall(this.Config, request, immutableCredentials.AccessKey, immutableCredentials.Token, aws4Signing);

            irequest.Endpoint = EndpointResolver.DetermineEndpoint(this.Config, irequest);

            var context = new Amazon.Runtime.Internal.ExecutionContext(new Amazon.Runtime.Internal.RequestContext(true, new NullSigner())
            {
                Request = irequest, ClientConfig = this.Config
            }, null);

            AmazonS3PostMarshallHandler.ProcessRequestHandlers(context);

            var metrics = new RequestMetrics();

            string authorization;

            if (aws4Signing)
            {
                var aws4Signer    = new AWS4PreSignedUrlSigner();
                var signingResult = aws4Signer.SignRequest(irequest,
                                                           this.Config,
                                                           metrics,
                                                           immutableCredentials.AccessKey,
                                                           immutableCredentials.SecretKey);
                authorization = "&" + signingResult.ForQueryParameters;
            }
            else
            {
                S3Signer.SignRequest(irequest, metrics, immutableCredentials.AccessKey, immutableCredentials.SecretKey);
                authorization = irequest.Headers[HeaderKeys.AuthorizationHeader];
                authorization = authorization.Substring(authorization.IndexOf(":", StringComparison.Ordinal) + 1);
                authorization = "&Signature=" + AmazonS3Util.UrlEncode(authorization, false);
            }

            Uri    url    = AmazonServiceClient.ComposeUrl(irequest);
            string result = url.AbsoluteUri + authorization;

            Protocol protocol = DetermineProtocol();

            if (request.Protocol != protocol)
            {
                switch (protocol)
                {
                case Protocol.HTTP:
                    result = result.Replace("http://", "https://");
                    break;

                case Protocol.HTTPS:
                    result = result.Replace("https://", "http://");
                    break;
                }
            }
            return(result);
        }
Esempio n. 26
0
        public async Task <IActionResult> UploadFileToS3(int id, IFormFile photo)
        {
            var information = _database.Talents.Where(a => a.Id == id).FirstOrDefault();
            var url         = "";

            using (var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1))
            {
                try
                {
                    using (var newMemoryStream = new MemoryStream())
                    {
                        photo.CopyTo(newMemoryStream);

                        var uploadRequest = new TransferUtilityUploadRequest
                        {
                            InputStream = newMemoryStream,
                            Key         = photo.FileName,
                            BucketName  = "testbucketcscnew1",
                            CannedACL   = S3CannedACL.PublicRead
                        };

                        GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest
                        {
                            BucketName = "testbucketcscnew1",
                            Key        = photo.FileName,
                            Expires    = DateTime.Now.AddMinutes(5)
                        };


                        var fileTransferUtility = new TransferUtility(client);
                        await fileTransferUtility.UploadAsync(uploadRequest);

                        url = client.GetPreSignedURL(request1);
                    }

                    information.TalentImageURL = photo.FileName;

                    //Check for NSFW image using Clarifai
                    var clientClarifai = new ClarifaiClient("");
                    var response       = await clientClarifai.PublicModels.NsfwModel
                                         .Predict(new ClarifaiURLImage("https://testbucketcscnew1.s3.amazonaws.com/" + photo.FileName))
                                         .ExecuteAsync();

                    double concepts = 0;
                    concepts = double.Parse(response.Get().Data.Select(c => $"{c.Value}").LastOrDefault());
                    //var concepts1 = response.Get().Data.Select(c => $"{c.Name}: {c.Value}");

                    if (concepts >= 0.85) //return BadRequest if image has >85% rate of being NSFW
                    {
                        return(BadRequest(new { message = "The image you uploaded is Not Safe For Work (NSFW)" }));
                    }
                    else
                    {
                        _database.Talents.Update(information);
                        _database.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    return(BadRequest(new { message = ex.Message }));
                }
            }
            return(Ok(new { message = url }));
        }
Esempio n. 27
0
        public ActionResult createNotes(NOTES n, IFormFile file)
        {
            if (ModelState.IsValid)
            {
                _log.LogInformation("NOTE is inserted");
                statsDPublisher.Increment("_NOTE_API");
                var authHeader          = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                var credentialBytes     = Convert.FromBase64String(authHeader.Parameter);
                var credentials         = Encoding.UTF8.GetString(credentialBytes).Split(':');
                var username            = credentials[0];
                var fileTransferUtility =
                    new TransferUtility(s3Client);

                string fileName = (rand.ToString() + file.FileName);
                rand++;
                var uploads = Path.Combine(Directory.GetCurrentDirectory(), file.FileName);

                var filePath = Path.Combine(uploads);
                if (file.Length > 0)
                {
                    using (var stream = new FileStream(filePath, FileMode.Create))


                        file.CopyToAsync(stream);
                }

                fileTransferUtility.UploadAsync(filePath, bucketName, fileName);
                GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
                request.BucketName = bucketName;
                request.Key        = fileName;
                request.Expires    = DateTime.Now.AddYears(2);
                request.Protocol   = Protocol.HTTP;
                string url = fileTransferUtility.S3Client.GetPreSignedURL(request);

                var Attachment = new Attachments {
                    url = url, FileName = fileName, length = file.Length, noteID = n.noteID
                };
                _context.Add(Attachment);
                mAttachments att = new mAttachments();
                att.AID = Attachment.AID;
                att.url = Attachment.url;
                _context.SaveChanges();
                var notes = new NOTES {
                    EMAIL = username, attachments = Attachment, content = n.content, created_on = DateTime.Now, title = n.title, last_updated_on = DateTime.Now
                };
                _context.Add(notes);
                // _context.Add(Attachment);
                _context.SaveChanges();


                IEnumerable <Attachments> at    = _context.attachments.AsEnumerable();
                List <mAttachments>       newat = new List <mAttachments>();
                foreach (Attachments attachment in at)
                {
                    if (attachment.noteID == n.noteID)
                    {
                        mAttachments m = new mAttachments();
                        m.AID = attachment.AID;
                        m.url = attachment.url;
                        newat.Add(m);
                    }
                }
                string Json = JsonConvert.SerializeObject(notes, Formatting.Indented);
                return(StatusCode(201, new{ noteId = notes.noteID, content = n.content, created_on = DateTime.Now, title = n.title, last_updated_on = DateTime.Now, attachments = att }));
            }
            else
            {
                var conflict = "Bad Request";
                return(StatusCode(409, new{ result = conflict }));
            }
        }
Esempio n. 28
0
        /// <summary>
        /// Marshalls the parameters for a presigned url for a preferred signing protocol.
        /// </summary>
        /// <param name="getPreSignedUrlRequest"></param>
        /// <param name="accessKey"></param>
        /// <param name="token"></param>
        /// <param name="aws4Signing">
        /// True if AWS4 signing will be used; if the expiry period in the request exceeds the
        /// maximum allowed for AWS4 (one week), an ArgumentException is thrown.
        /// </param>
        /// <returns></returns>
        private static IRequest Marshall(GetPreSignedUrlRequest getPreSignedUrlRequest,
                                         string accessKey,
                                         string token,
                                         bool aws4Signing)
        {
            IRequest request = new DefaultRequest(getPreSignedUrlRequest, "AmazonS3");

            request.HttpMethod = getPreSignedUrlRequest.Verb.ToString();
            if (!string.IsNullOrEmpty(getPreSignedUrlRequest.ContentType))
            {
                request.Parameters[S3QueryParameter.ContentType.ToString()] = getPreSignedUrlRequest.ContentType;
            }
            if (!string.IsNullOrEmpty(token))
            {
                request.Headers[S3Constants.AmzSecurityTokenHeader] = token;
            }

            if (getPreSignedUrlRequest.ServerSideEncryptionMethod != null && getPreSignedUrlRequest.ServerSideEncryptionMethod != ServerSideEncryptionMethod.None)
            {
                request.Headers.Add("x-amz-server-side-encryption", S3Transforms.ToStringValue(getPreSignedUrlRequest.ServerSideEncryptionMethod));
            }

            var queryParameters = request.Parameters;

            var uriResourcePath = new StringBuilder("/");

            if (!string.IsNullOrEmpty(getPreSignedUrlRequest.BucketName))
            {
                uriResourcePath.Append(S3Transforms.ToStringValue(getPreSignedUrlRequest.BucketName));
            }
            if (!string.IsNullOrEmpty(getPreSignedUrlRequest.Key))
            {
                if (uriResourcePath.Length > 1)
                {
                    uriResourcePath.Append("/");
                }
                uriResourcePath.Append(S3Transforms.ToStringValue(getPreSignedUrlRequest.Key));
            }

            var baselineTime = aws4Signing ? DateTime.UtcNow : new DateTime(1970, 1, 1);
            var expires      = Convert.ToInt64((getPreSignedUrlRequest.Expires.ToUniversalTime() - baselineTime).TotalSeconds);

            if (aws4Signing && expires > AWS4PreSignedUrlSigner.MaxAWS4PreSignedUrlExpiry)
            {
                var msg = string.Format(CultureInfo.InvariantCulture, "The maximum expiry period for a presigned url using AWS4 signing is {0} seconds",
                                        AWS4PreSignedUrlSigner.MaxAWS4PreSignedUrlExpiry);
                throw new ArgumentException(msg);
            }

            queryParameters.Add(aws4Signing ? "X-Amz-Expires" : "Expires", expires.ToString(CultureInfo.InvariantCulture));

            if (!string.IsNullOrEmpty(token))
            {
                queryParameters.Add("x-amz-security-token", token);
            }
            if (!aws4Signing)
            {
                queryParameters.Add("AWSAccessKeyId", accessKey);
            }
            if (getPreSignedUrlRequest.IsSetVersionId())
            {
                queryParameters.Add("versionId", S3Transforms.ToStringValue(getPreSignedUrlRequest.VersionId));
            }

            var responseHeaderOverrides = getPreSignedUrlRequest.ResponseHeaderOverrides;

            if (!string.IsNullOrEmpty(responseHeaderOverrides.CacheControl))
            {
                queryParameters.Add("response-cache-control", responseHeaderOverrides.CacheControl);
            }
            if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentType))
            {
                queryParameters.Add("response-content-type", responseHeaderOverrides.ContentType);
            }
            if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentLanguage))
            {
                queryParameters.Add("response-content-language", responseHeaderOverrides.ContentLanguage);
            }
            if (!string.IsNullOrEmpty(responseHeaderOverrides.Expires))
            {
                queryParameters.Add("response-expires", responseHeaderOverrides.Expires);
            }
            if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentDisposition))
            {
                queryParameters.Add("response-content-disposition", responseHeaderOverrides.ContentDisposition);
            }
            if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentEncoding))
            {
                queryParameters.Add("response-content-encoding", responseHeaderOverrides.ContentEncoding);
            }

            var path = uriResourcePath.ToString();

            request.CanonicalResource = S3Transforms.GetCanonicalResource(path, queryParameters, S3Constants.GetObjectExtraSubResources);
            request.ResourcePath      = S3Transforms.FormatResourcePath(path, queryParameters);
            request.UseQueryString    = true;

            return(request);
        }
Esempio n. 29
0
        /// <summary>
        /// GetAudioFileName
        /// </summary>
        /// <param name="drv"></param>
        /// <returns></returns>
        public static string GetAudioFileName(getScorecardData_Result drv)
        {
            // If this scorecard is for website, just return the website

            int Id;

            try
            {
                Id = drv.X_ID;
            }
            catch (Exception ex)
            {
                Id = drv.F_ID;
            }

            //DataTable sc_id = GetTable("select *, (select bypass from userextrainfo where username = '******') as bypass from xcc_report_new  join scorecards  on xcc_report_new.scorecard = scorecards.id join app_settings  on app_settings.appname = xcc_report_new.appname where xcc_report_new.id = " + Id);
            using (CC_ProdEntities dataContext = new CC_ProdEntities())
            {
                var sc_id = dataContext.GetAudioFileName(HttpContext.Current.User.Identity.Name, Id).FirstOrDefault();
                if (sc_id != null)
                {
                    if (sc_id.bypass.ToString() == "True" & sc_id.onAWS.ToString() == "True")
                    {
                        return("http://files.callcriteria.com" + sc_id.audio_link);
                    }
                    if (sc_id.onAWS.ToString() == "True")
                    {
                        string    audio_link = sc_id.audio_link;
                        IAmazonS3 s3Client;
                        s3Client = new AmazonS3Client(System.Configuration.ConfigurationManager.AppSettings["CCAWSAccessKey"], System.Configuration.ConfigurationManager.AppSettings["CCCAWSSecretKey"], Amazon.RegionEndpoint.APSoutheast1);

                        GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest();
                        GetPreSignedUrlRequest URL_REQ  = new GetPreSignedUrlRequest();
                        URL_REQ.BucketName = "callcriteriasingapore" + Strings.Left(audio_link, audio_link.LastIndexOf("/")).Replace("/audio2/", "/audio/");
                        URL_REQ.Key        = audio_link.Substring(audio_link.LastIndexOf("/") + 1);
                        URL_REQ.Expires    = DateTime.Now.AddHours(1);
                        return(s3Client.GetPreSignedURL(URL_REQ));
                    }

                    if (sc_id.review_type == "website")
                    {
                        return("/point1sec.mp3");
                    }
                    if (sc_id.stream_only.ToString() == "True")
                    {
                        return(sc_id.audio_link);
                    }
                }

                if (Strings.Left(drv.audio_link.ToString(), 6) == "/audio" & (Strings.Right(drv.audio_link.ToString(), 4) == ".mp3" | Strings.Right(drv.audio_link.ToString(), 4) == ".mp4" | Strings.Right(drv.audio_link.ToString(), 4) == ".gsm"))
                {
                    //try
                    //{
                    //    TimeSpan call_len = new TimeSpan(0, 0, Convert.ToInt32(GetMediaDuration("http://files.callcriteria.com" + drv.audio_link)) / 2);   // tlf.Properties.Duration
                    //    DateTime call_time = Convert.ToDateTime("12/30/1899") + call_len;
                    //    UpdateTable("update XCC_REPORT_NEW set call_time = '" + call_time.ToString() + "' where ID = (select review_id from form_score3  where ID = " + Id + ")");
                    //    UpdateTable("update form_score3 set call_length = '" + call_len.TotalSeconds + "' where ID = " + Id);
                    //    return "http://files.callcriteria.com" + drv.audio_link.ToString(); // already converted, send it back
                    //}
                    //catch (Exception ex)
                    //{
                    //    return "http://files.callcriteria.com" + drv.audio_link.ToString();
                    //}
                }

                // thinks it's downloaded, but file's not there, get the orignal file and download again
                if (Strings.Left(drv.audio_link.ToString(), 6) == "/audio" & (Strings.Right(drv.audio_link.ToString(), 4) == ".mp3" | Strings.Right(drv.audio_link.ToString(), 4) == ".mp4" | Strings.Right(drv.audio_link.ToString(), 4) == ".gsm"))
                {
                    // Email_Error("trying to reload " & drv.Item("audio_link").ToString)
                    // See if session ID has data
                    //wav_dt = GetTable("select * from wav_data  where session_id = '" + drv.SESSION_ID + "'");
                    var wav_dt = dataContext.WAV_DATA.Where(x => x.session_id == drv.SESSION_ID).FirstOrDefault();
                    if (wav_dt == null)
                    {
                        //wav_dt = GetTable("select * from wav_data  where filename like '%" + drv.SESSION_ID + "%'");
                    }
                    if (wav_dt != null)
                    {
                        drv.audio_link = wav_dt.filename;
                    }
                }

                string session_id;
                if (drv.SESSION_ID.ToString().IndexOf(" ") > 0)
                {
                    session_id = Strings.Left(drv.SESSION_ID.ToString(), drv.SESSION_ID.ToString().IndexOf(" "));
                }
                else
                {
                    session_id = drv.SESSION_ID;
                }

                string phone         = drv.phone.ToString();
                string this_filename = drv.audio_link.ToString();

                string call_date = drv.call_date.ToString().Substring(0, drv.call_date.ToString().IndexOf(" ")).Replace("/", "_");
                if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("/audio/" + drv.appname.ToString() + "/" + call_date + "/")))
                {
                    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/audio/" + drv.appname.ToString() + "/" + call_date + "/"));
                }
                if (drv.audio_link.ToString() != "")
                {
                    if (Strings.Left(drv.audio_link.ToString(), 4) == "http" | Strings.Left(drv.audio_link.ToString(), 6) == "/audio" | Strings.Left(drv.audio_link.ToString(), 3) == "ftp")
                    {
                        this_filename = drv.audio_link.ToString().Replace(" ", "%20");
                    }
                    else
                    {
                        this_filename = drv.url_prefix + (drv.audio_link.ToString().Replace(" ", "%20"));
                    }


                    string file_ending = Strings.Right(this_filename, 4).ToLower();


                    if ((this_filename.IndexOf("@") > -1 | this_filename.IndexOf("http") > -1) & Strings.Left(this_filename, 3) != "ftp" & Strings.Left(this_filename, 4) != "sftp" & Strings.Left(this_filename, 6) != "/audio")
                    {
                        // Email_Error(this_filename)
                        System.Net.WebClient WebClient_down = new System.Net.WebClient();

                        WebClient_down.Credentials = new System.Net.NetworkCredential(drv.recording_user.ToString(), drv.record_password.ToString(), "");

                        try
                        {
                            WebClient_down.DownloadFile(this_filename, HttpContext.Current.Server.MapPath("/audio/" + drv.appname.ToString() + "/" + call_date + "/" + session_id + file_ending));
                        }
                        catch (Exception ex)
                        {
                            HttpContext.Current.Response.Write("File not found, refresh page." + this_filename + " to " + HttpContext.Current.Server.MapPath("/audio/" + drv.appname.ToString() + "/" + call_date + "/" + session_id + file_ending) + ex.Message);
                            //Email_Error(HttpContext.Current.Server.MapPath("/audio/" + drv.appname.ToString() + "/" + call_date + "/" + session_id + file_ending) + ex.Message);
                            HttpContext.Current.Response.Redirect("listen.aspx");
                        }


                        while (!!WebClient_down.IsBusy)
                        {
                            System.Threading.Thread.Sleep(100);
                        }
                    }

                    if (Strings.Left(this_filename, 6) == "ftp://")
                    {
                        WebClient ftpc = new WebClient();
                        ftpc.Credentials = new System.Net.NetworkCredential(drv.audio_user.ToString(), drv.audio_password.ToString(), "");

                        try
                        {
                            System.IO.File.Delete(@"d:\wwwroot\audio\" + drv.appname.ToString() + @"\" + call_date + @"\" + session_id + file_ending);
                        }
                        catch (Exception ex)
                        {
                        }

                        try
                        {
                            ftpc.DownloadFile(this_filename, @"d:\wwwroot\audio\" + drv.appname.ToString() + @"\" + call_date + @"\" + session_id + file_ending);
                            System.Threading.Thread.Sleep(500);
                        }
                        catch (Exception ex)
                        {
                            HttpContext.Current.Response.Write(@"d:\wwwroot\audio\" + drv.appname.ToString() + @"\" + call_date + @"\" + session_id + file_ending + " " + ex.Message + "<br><br><br>");
                            HttpContext.Current.Response.End();
                        }

                        while (!!ftpc.IsBusy)
                        {
                            System.Threading.Thread.Sleep(100);
                        }
                    }

                    if (Strings.Left(this_filename, 7) == "sftp://")
                    {
                        try
                        {
                            Renci.SshNet.SftpClient sftp_new = new Renci.SshNet.SftpClient(drv.recording_user.ToString().Substring(7), Convert.ToInt32(drv.audio_link.ToString()), drv.audio_user.ToString(), drv.audio_password.ToString());
                            sftp_new.Connect();

                            System.IO.FileStream dest_file = new System.IO.FileStream(@"d:\wwwroot\audio\" + drv.appname.ToString() + @"\" + call_date + @"\" + session_id + file_ending, FileMode.OpenOrCreate);

                            sftp_new.DownloadFile(this_filename.Substring(Strings.Len(drv.recording_user)).Replace("%20", " "), dest_file);
                            sftp_new.Disconnect();
                            sftp_new.Dispose();
                            dest_file.Close();
                            dest_file.Dispose();
                        }
                        catch (Exception ex)
                        {
                            HttpContext.Current.Response.Write(ex.Message + "<br>");
                            HttpContext.Current.Response.Write(@"d:\wwwroot\audio\" + drv.appname.ToString() + @"\" + call_date + @"\" + session_id + file_ending + "<br>");
                            HttpContext.Current.Response.Write(this_filename.Substring(Strings.Len(drv.recording_user)).Replace("%20", " "));
                            HttpContext.Current.Response.End();
                        }
                        int max_wait = 0;
                        while (!System.IO.File.Exists(HttpContext.Current.Server.MapPath("/audio/" + drv.appname.ToString() + "/" + call_date + "/" + session_id + file_ending)) | max_wait == 100)
                        {
                            System.Threading.Thread.Sleep(100);
                            max_wait += 1;
                        }
                    }
                    if (file_ending == ".mp3" | file_ending == ".mp4")
                    {
                        this_filename = HttpContext.Current.Server.MapPath("/audio/" + drv.appname.ToString() + "/" + call_date + "/" + session_id + file_ending);
                    }
                    else
                    {
                        try
                        {
                            this_filename = HttpContext.Current.Server.MapPath("/audio/" + drv.appname.ToString() + "/" + call_date + "/" + session_id + file_ending);
                        }
                        catch (Exception ex)
                        {
                            return("");
                        }
                        string output           = "";
                        string out_error        = "";
                        string destination_file = HttpContext.Current.Server.MapPath("/audio/" + drv.appname.ToString() + "/" + call_date + "/" + session_id + ".mp3");
                        //RunFFMPEG("-i " + Strings.Chr(34) + this_filename + Strings.Chr(34) + " -b:a 16k -y " + destination_file, ref output, ref out_error);
                        try
                        {
                            System.IO.File.Delete(HttpContext.Current.Server.MapPath("/audio/" + drv.appname.ToString() + "/" + call_date + "/" + session_id + file_ending));
                        }
                        catch (Exception ex)
                        {
                        }
                        file_ending = ".mp3";
                    }
                    this_filename = "/audio/" + drv.appname.ToString() + "/" + call_date + "/" + session_id + file_ending;
                    if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(this_filename)))
                    {
                        //UpdateTable("update XCC_REPORT_NEW set audio_link = '" + this_filename + "' where ID = " + Id);
                        var            isExist           = dataContext.XCC_REPORT_NEW.Where(x => x.ID == Id).FirstOrDefault();
                        XCC_REPORT_NEW tblXCC_REPORT_NEW = new XCC_REPORT_NEW();
                        if (isExist != null)
                        {
                            tblXCC_REPORT_NEW = dataContext.XCC_REPORT_NEW.Find(Id);
                            dataContext.Entry(tblXCC_REPORT_NEW).State = EntityState.Modified;
                            tblXCC_REPORT_NEW.bad_call_accepted        = System.DateTime.Now;
                            tblXCC_REPORT_NEW.audio_link = this_filename;
                            int result = dataContext.SaveChanges();
                        }
                        return(this_filename);
                    }
                    else
                    {
                        return(drv.audio_link.ToString());// already an mp3, file exists though doesn't match session ID
                    }
                }
                return("");
            }
        }
Esempio n. 30
0
        /// <summary>
        /// Determines whether an S3 bucket exists or not.
        /// This is done by:
        /// 1. Creating a PreSigned Url for the bucket (with an expiry date at the end of this decade)
        /// 2. Making a HEAD request to the Url
        /// </summary>
        /// <param name="bucketName">The name of the bucket to check.</param>
        /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param>
        /// <returns></returns>
        public static bool DoesS3BucketExist(IAmazonS3 s3Client, string bucketName)
        {
            if (s3Client == null)
            {
                throw new ArgumentNullException("s3Client", "The s3Client cannot be null!");
            }

            if (String.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException("bucketName", "The bucketName cannot be null or the empty string!");
            }

            GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();

            request.BucketName = bucketName;
            if (AWSConfigs.S3Config.UseSignatureVersion4)
            {
                request.Expires = DateTime.Now.AddDays(6);
            }
            else
            {
                request.Expires = new DateTime(2019, 12, 31);
            }
            request.Verb     = HttpVerb.HEAD;
            request.Protocol = Protocol.HTTP;
            string url = s3Client.GetPreSignedURL(request);
            Uri    uri = new Uri(url);

            HttpWebRequest httpRequest = WebRequest.Create(uri) as HttpWebRequest;

            httpRequest.Method = "HEAD";
            AmazonS3Client concreteClient = s3Client as AmazonS3Client;

            if (concreteClient != null)
            {
                concreteClient.ConfigureProxy(httpRequest);
            }

            try
            {
                using (HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse)
                {
                    // If all went well, the bucket was found!
                    return(true);
                }
            }
            catch (WebException we)
            {
                using (HttpWebResponse errorResponse = we.Response as HttpWebResponse)
                {
                    if (errorResponse != null)
                    {
                        HttpStatusCode code = errorResponse.StatusCode;
                        return(code != HttpStatusCode.NotFound &&
                               code != HttpStatusCode.BadRequest);
                    }

                    // The Error Response is null which is indicative of either
                    // a bad request or some other problem
                    return(false);
                }
            }
        }