Ejemplo n.º 1
0
        private void putWithRetry(PutObjectRequest request)
        {
            // Retry put request up to five times
            for (int i = 0; i < properties.S3MaxErrorRetry; i++)
            {
                try
                {
                    // Attempt to put the object
                    s3.PutObject(request);
                    log.Info("Successfully uploaded file to S3: " + request.ToString());
                    return;
                }
                catch (AmazonS3Exception exception)
                {
                    log.Info("Failed to upload file to S3: " + exception.ToString());
                    Thread.Sleep(properties.S3RetryDelayInterval);
                }
                catch (Exception e) {
                    log.Info("Failed to upload file to S3: " + e.ToString());
                    Thread.Sleep(properties.S3RetryDelayInterval);
                }
            }

            throw new AmazonS3Exception("Failed to upload file to S3");
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> UploadPatientTestReport(string patientId, [FromForm] IFormFile file, string testName)
        {
            try
            {
                if (file == null || file.Length == 0)
                {
                    return(Content("File Not Selected"));
                }
                string fileName = file.FileName;
                Console.WriteLine(fileName);
                string extension = Path.GetExtension(fileName);
                Console.WriteLine(extension);
                var key = string.Format("{0}/{1}-" + DateTime.Now.ToString("MM-dd-yy") + extension, patientId, testName);
                Console.WriteLine(key);

                _client = new AmazonS3Client(accessKey, secretKey, RegionEndpoint.APSouth1);

                var request = new PutObjectRequest {
                    BucketName      = "cgi-wave7-dotnet",
                    Key             = key,
                    AutoCloseStream = false,
                    InputStream     = file.OpenReadStream()
                };

                Console.WriteLine(request.ToString());

                await _client.PutObjectAsync(request);

                TestReport tr = new TestReport();
                tr.PatientId           = patientId;
                tr.NameOfTestConducted = testName;
                tr.FileLocation        = key;
                tr.FileName            = testName + "-" + DateTime.Now.ToString("MM-dd-yy");

                return(Ok(_service.AddTestReportToPatient(patientId, tr)));
            }
            catch (Exception exe)
            {
                return(BadRequest(exe.Message));
            }
        }