Пример #1
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, TraceWriter log)
        {
            log.Info("ListS3Objects function processed a request.");
            IActionResult response = await S3Util.ListingObjectsAsync(req, log);

            return(response);
        }
        public async Task <IActionResult> GetRankings()
        {
            string bucketName = "reinvent-gottalent";

            List <Ranking> rankingList = new List <Ranking>();
            var            topRankings = RedisUtil.GetTopRankings(0, 4);

            foreach (var item in topRankings)
            {
                Ranking ranking = new Ranking();
                ranking.total_rank = item.total_rank;

                var game = await _context.Game.FirstOrDefaultAsync(x => x.game_id == item.game_id);

                ranking.game_id = item.game_id;
                ranking.name    = game.name;

                var gameResult = await _context.GameResult.FirstOrDefaultAsync(x => x.game_id == item.game_id);

                ranking.total_score = gameResult.total_score;
                ranking.gender      = gameResult.gender_result;
                ranking.age         = gameResult.age_result;
                ranking.grade       = gameResult.grade_result;

                var stageLog = await _context.StageLog.FirstOrDefaultAsync(x => x.game_id == item.game_id && x.action_type == "Profile");

                ranking.photoURL = S3Util.GetPresignedURL(this.S3Client, bucketName, stageLog.file_loc);

                rankingList.Add(ranking);
            }

            return(Ok(rankingList));
        }
Пример #3
0
        public async Task ShouldNotUploadNewVersionOfDirectoryArtifactWhenHashesMatch()
        {
            using var templateDir = this.deepNestedStack;
            // Hash of lambda directory content before zipping
            // Zips are not idempotent - fields e.g. timestamps in central directory change with successive zips of the same content.
            var directoryHash = new DirectoryInfo(Path.Combine(templateDir, "lambdacomplex")).MD5();
            var template      = Path.Combine(templateDir, "base-stack.json");
            var projectId     = S3Util.GenerateProjectId(template);
            var logger        = new TestLogger(this.output);
            var mockSts       = TestHelpers.GetSTSMock();
            var mockS3        = TestHelpers.GetS3ClientWithBucketMock();
            var mockContext   = new Mock <IPSCloudFormationContext>();

            mockContext.Setup(c => c.Logger).Returns(logger);
            mockContext.Setup(c => c.Region).Returns(RegionEndpoint.EUWest1);
            mockS3.SetupSequence(s3 => s3.ListObjectsV2Async(It.IsAny <ListObjectsV2Request>(), default)).ReturnsAsync(
                new ListObjectsV2Response
            {
                S3Objects = new List <S3Object>
                {
                    new S3Object
                    {
                        BucketName = "test-bucket",
                        Key        = $"lambdacomplex-{projectId}-0000.zip"
                    }
                }
            }).ReturnsAsync(this.fileNotFound).ReturnsAsync(this.fileNotFound);

            mockS3.Setup(s3 => s3.GetObjectMetadataAsync(It.IsAny <GetObjectMetadataRequest>(), default)).ReturnsAsync(
                () =>
            {
                var resp = new GetObjectMetadataResponse();

                resp.Metadata.Add(S3Util.PackagerHashKey, directoryHash);
                return(resp);
            });

            var mockClientFactory = new Mock <IPSAwsClientFactory>();

            mockClientFactory.Setup(f => f.CreateS3Client()).Returns(mockS3.Object);
            mockClientFactory.Setup(f => f.CreateSTSClient()).Returns(mockSts.Object);

            using var workingDirectory = new TempDirectory();

            var packager = new PackagerUtils(
                new TestPathResolver(),
                logger,
                new S3Util(mockClientFactory.Object, mockContext.Object, template, "test-bucket", null, null),
                new OSInfo());

            var outputTemplatePath = await packager.ProcessTemplate(template, workingDirectory);

            this.output.WriteLine(string.Empty);
            this.output.WriteLine(await File.ReadAllTextAsync(outputTemplatePath));

            // Three objects should have been uploaded to S3
            mockS3.Verify(m => m.PutObjectAsync(It.IsAny <PutObjectRequest>(), default), Times.Exactly(2));
        }
Пример #4
0
    public static string DeleteProjectDocument(int id, string connstring)
    {
        try {
            Dictionary <string, object> spParams = new Dictionary <string, object>();
            spParams.Add("@Id", id);
            return(DBAccess.ExecuteSQLNonQuery(connstring, "CALL usp_RemoveDocumentFromProject(@Id)", spParams));

            S3Util s3 = new S3Util();
            s3.DeleteFile("");
        } catch (Exception) {
            return("An error occurred");
        }
    }
Пример #5
0
        public async Task ShouldReturnBucketWhenBucketExists()
        {
            var expectedBucketName = $"cf-templates-pscloudformation-{TestHelpers.RegionName}-{TestHelpers.AccountId}";

            var logger            = new TestLogger(this.output);
            var mockClientFactory = new Mock <IPSAwsClientFactory>();

            mockClientFactory.Setup(f => f.CreateSTSClient()).Returns(TestHelpers.GetSTSMock().Object);
            mockClientFactory.Setup(f => f.CreateS3Client()).Returns(TestHelpers.GetS3ClientWithBucketMock().Object);

            var ops = new S3Util(mockClientFactory.Object, TestHelpers.GetContextMock(logger).Object);

            var bucket = await ops.GetCloudFormationBucketAsync();

            bucket.BucketName.Should().Be(expectedBucketName);
            logger.InfoMessages.Any().Should().BeFalse();
        }
        public async static Task <string> PollyDemo(IAmazonPolly pollyClient, IAmazonS3 S3Client, string text)
        {
            string result = null;
            SynthesizeSpeechRequest synthesizeRequest = new SynthesizeSpeechRequest()
            {
                LanguageCode = LanguageCode.EnUS,
                OutputFormat = "mp3",
                SampleRate   = "8000",
                Text         = text,
                TextType     = "text",
                VoiceId      = "Joanna"
            };

            try
            {
                Task <SynthesizeSpeechResponse> synthesizeTask    = pollyClient.SynthesizeSpeechAsync(synthesizeRequest);
                SynthesizeSpeechResponse        syntheizeResponse = await synthesizeTask;

                Console.WriteLine(syntheizeResponse.ContentType);
                Console.WriteLine(syntheizeResponse.RequestCharacters);

                using (MemoryStream ms = new MemoryStream())
                {
                    syntheizeResponse.AudioStream.CopyTo(ms);
                    Console.WriteLine(ms.Length);

                    // Upload image to S3 bucket
                    string bucketName = "reinvent-indiamazones";
                    //string key = dto.text;
                    string key = "pollytest";
                    await Task.Run(() => S3Util.UploadToS3(S3Client, bucketName, key, ms));

                    // TODO : need to check the file exists in S3
                    result = S3Util.GetPresignedURL(S3Client, bucketName, key);
                }
                //syntheizeResponse.AudioStream.CopyTo(result);
                //result.Flush();
            }
            catch (AmazonPollyException pollyException)
            {
                Console.WriteLine(pollyException.Message, pollyException.InnerException);
            }

            return(result);
        }
Пример #7
0
        private TestLogger SetupMocks(string template)
        {
            var projectId   = S3Util.GenerateProjectId(template);
            var logger      = new TestLogger(this.output);
            var mockSts     = TestHelpers.GetSTSMock();
            var mockS3      = TestHelpers.GetS3ClientWithBucketMock();
            var mockContext = new Mock <IPSCloudFormationContext>();

            mockContext.Setup(c => c.Logger).Returns(logger);
            mockContext.Setup(c => c.Region).Returns(RegionEndpoint.EUWest1);
            mockS3.SetupSequence(s3 => s3.ListObjectsV2Async(It.IsAny <ListObjectsV2Request>(), default)).ReturnsAsync(
                new ListObjectsV2Response
            {
                S3Objects = new List <S3Object>
                {
                    new S3Object
                    {
                        BucketName = "test-bucket", Key = $"my_lambda-{projectId}-0000.zip"
                    }
                }
            }).ReturnsAsync(this.fileNotFound).ReturnsAsync(this.fileNotFound);

            mockS3.Setup(s3 => s3.GetObjectMetadataAsync(It.IsAny <GetObjectMetadataRequest>(), default)).ReturnsAsync(
                () =>
            {
                var resp = new GetObjectMetadataResponse();

                resp.Metadata.Add(S3Util.PackagerHashKey, "0");
                return(resp);
            });

            var mockClientFactory = new Mock <IPSAwsClientFactory>();

            mockClientFactory.Setup(f => f.CreateS3Client()).Returns(mockS3.Object);
            mockClientFactory.Setup(f => f.CreateSTSClient()).Returns(mockSts.Object);

            this.ClientFactory = mockClientFactory.Object;
            this.Context       = mockContext.Object;
            this.Logger        = logger;
            return(logger);
        }
Пример #8
0
        //public async Task<IActionResult> TranscribeTest([FromBody] TranscribeTestDTO dto)
        public async Task <IActionResult> TranscribeTest([FromForm] TranscribeTestDTO dto)
        {
            string transcriptionUri = null;

            Guid   g          = Guid.NewGuid();
            string guidString = Convert.ToBase64String(g.ToByteArray());

            guidString = guidString.Replace("=", "");
            guidString = guidString.Replace("+", "");
            guidString = guidString.Replace("/", "");

            // Validation check
            if (string.IsNullOrWhiteSpace(dto.language_code) == true)
            {
                return(BadRequest("language_code is empty."));
            }

            if (dto.WAVblob == null || dto.WAVblob.Length <= 0)
            {
                return(BadRequest("WAVblob is empty."));
            }

            using (MemoryStream ms = new MemoryStream())
            {
                dto.WAVblob.CopyTo(ms);

                // Upload image to S3 bucket
                await Task.Run(() => S3Util.UploadToS3(this.S3Client, "reinvent-indiamazones", "transcribe_test/mytest.wav", ms));
            }

            string mediaUri = "https://reinvent-indiamazones.s3-us-west-2.amazonaws.com/transcribe_test/mytest.wav";

            // call Transcribe API
            transcriptionUri = await TranscribeUtil.TranscribeDemo(this.TranscribeClient, dto.language_code, mediaUri);

            return(Ok(transcriptionUri));
        }
Пример #9
0
        public async Task ShouldNotUploadNewVersionOfTemplateArtifactWhenHashesMatch()
        {
            var templateDir = this.deepNestedStack;
            var template    = Path.Combine(templateDir, "base-stack.json");
            var projectId   = S3Util.GenerateProjectId(template);
            var logger      = new TestLogger(this.output);
            var mockSts     = TestHelpers.GetSTSMock();
            var mockS3      = TestHelpers.GetS3ClientWithBucketMock();
            var mockContext = new Mock <IPSCloudFormationContext>();

            mockContext.Setup(c => c.Logger).Returns(logger);
            mockContext.Setup(c => c.Region).Returns(RegionEndpoint.EUWest1);

            mockS3.SetupSequence(s3 => s3.ListObjectsV2Async(It.IsAny <ListObjectsV2Request>(), default))
            .ReturnsAsync(this.fileNotFound)
            .ReturnsAsync(
                new ListObjectsV2Response
            {
                S3Objects = new List <S3Object>
                {
                    new S3Object
                    {
                        BucketName = "test-bucket",
                        Key        = $"sub-nested-2-{projectId}-0000.json"
                    }
                }
            }).ReturnsAsync(this.fileNotFound);

            mockS3.Setup(s3 => s3.GetObjectMetadataAsync(It.IsAny <GetObjectMetadataRequest>(), default)).ReturnsAsync(
                () =>
            {
                var resp = new GetObjectMetadataResponse();

                resp.Metadata.Add(S3Util.PackagerHashKey, GetModifiedTemplateHash());
                return(resp);
            });

            var mockClientFactory = new Mock <IPSAwsClientFactory>();

            mockClientFactory.Setup(f => f.CreateS3Client()).Returns(mockS3.Object);
            mockClientFactory.Setup(f => f.CreateSTSClient()).Returns(mockSts.Object);

            using var workingDirectory = new TempDirectory();

            var packager = new PackagerUtils(
                new TestPathResolver(),
                logger,
                new S3Util(mockClientFactory.Object, mockContext.Object, template, "test-bucket", null, null),
                new OSInfo());

            var outputTemplatePath = await packager.ProcessTemplate(template, workingDirectory);

            this.output.WriteLine(string.Empty);
            this.output.WriteLine(await File.ReadAllTextAsync(outputTemplatePath));

            // Three objects should have been uploaded to S3
            mockS3.Verify(m => m.PutObjectAsync(It.IsAny <PutObjectRequest>(), default), Times.Exactly(2));

            // Bit hacky, but we need to know the hash of the template after modification.
            // Different on Windows and Linux due to line endings.
            string GetModifiedTemplateHash()
            {
                var re = new Regex(@"sub-nested-2\.json.*Hash: (?<hash>[0-9a-f]+)");

                var logLine = logger.DebugMessages.FirstOrDefault(line => re.IsMatch(line));

                if (logLine == null)
                {
                    return("0");
                }

                var mc = re.Match(logLine);

                return(mc.Groups["hash"].Value);
            }
        }
Пример #10
0
 static S3UtilTest()
 {
     RootTemplate = GetThisFilePath();
     ProjectId    = S3Util.GenerateProjectId(RootTemplate);
 }
Пример #11
0
 public static string S3ETag(this BlobFingerprint fingerprint)
 {
     return(S3Util.ComputeS3Etag(fingerprint.Md5));
 }
Пример #12
0
 public static string Key(this BlobFingerprint fingerprint)
 {
     return(S3Util.S3EncodeKey(fingerprint.Sha3_512));
 }
        public async Task <IActionResult> CalcGameResult(int game_id)
        {
            string bucketName = "reinvent-gottalent";

            var stageLogs = await _context.StageLog.Where(x => x.game_id == game_id).ToListAsync();

            double        totalScore   = 0.0f;
            string        genderResult = "";
            string        gradeResult  = "";
            List <string> signedURLs   = new List <string>();
            int           ageResult    = 0;

            foreach (var stageLog in stageLogs)
            {
                if (stageLog.action_type == "Profile")
                {
                    genderResult = stageLog.gender;
                    ageResult    = stageLog.age;
                }
                else
                {
                    totalScore += stageLog.score;
                    signedURLs.Add(S3Util.GetPresignedURL(this.S3Client, bucketName, stageLog.file_loc));
                }
            }

            // TODO : need to use Cache service for judgement
            if (totalScore < 160)
            {
                gradeResult = "Extra";
            }
            else if (160 <= totalScore && totalScore < 190)
            {
                gradeResult = "Supporting";
            }
            else
            {
                gradeResult = "Leading";
            }

            // casting randomly
            int    randomRecord  = new Random().Next() % _context.Cast.Where(x => x.gender == genderResult && x.grade == gradeResult).Count();;
            var    castResult    = _context.Cast.Where(x => x.gender == genderResult && x.grade == gradeResult).Skip(randomRecord).Take(1).First();
            string resultPageUrl = "TBD";

            // Database update
            GameResult newGameResult = new GameResult {
                game_id         = game_id,
                result_page_url = resultPageUrl,
                total_score     = totalScore,
                total_rank      = 0,
                cast_result     = castResult.cast_id,
                grade_result    = gradeResult,
                gender_result   = genderResult,
                age_result      = ageResult
            };

            Game game = await _context.Game.Where(x => x.game_id == game_id).FirstOrDefaultAsync();

            game.end_date = DateTime.Now;
            game.share_yn = "Y";

            var value = _context.GameResult.Add(newGameResult);
            await _context.SaveChangesAsync();

            RedisUtil.AddGameResultToRedis(newGameResult);
            newGameResult.total_rank = RedisUtil.GetGameRanking(newGameResult.game_id) + 1;

            return(Ok(new { newGameResult, castResult.actor, castResult.title, signedURLs }));
        }
        public async Task <IActionResult> Post([FromBody] GameStagePostImageDTO dto)
        {
            Console.WriteLine("PostImage entered.");

            string bucketName = "reinvent-gottalent";

            // Retrieving image data
            // ex: game/10/Happiness.jpg
            string keyName        = string.Format("game/{0}/{1}.jpg", dto.gameId, dto.actionType);
            string croppedKeyName = string.Format("game/{0}/{1}_cropped.jpg", dto.gameId, dto.actionType);

            byte[] imageByteArray = Convert.FromBase64String(dto.base64Image);
            if (imageByteArray.Length == 0)
            {
                return(BadRequest("Image length is 0."));
            }

            StageLog newStageLog = null;

            using (MemoryStream ms = new MemoryStream(imageByteArray))
            {
                // call Rekonition API
                FaceDetail faceDetail = await RekognitionUtil.GetFaceDetailFromStream(this.RekognitionClient, ms);

                // Crop image to get face only
                System.Drawing.Image originalImage = System.Drawing.Image.FromStream(ms);
                System.Drawing.Image croppedImage  = GetCroppedFaceImage(originalImage, faceDetail.BoundingBox);
                MemoryStream         croppedms     = new MemoryStream();
                croppedImage.Save(croppedms, ImageFormat.Jpeg);

                // Upload image to S3 bucket
                //await Task.Run(() => S3Util.UploadToS3(this.S3Client, bucketName, keyName, ms));
                await Task.Run(() => S3Util.UploadToS3(this.S3Client, bucketName, keyName, croppedms));

                // Get a specific emotion score
                double emotionScore = 0.0f;
                if (dto.actionType != "Profile")
                {
                    emotionScore = RekognitionUtil.GetEmotionScore(faceDetail.Emotions, dto.actionType);
                }

                int    evaluatedAge    = (faceDetail.AgeRange.High + faceDetail.AgeRange.Low) / 2;
                string evaluatedGender = faceDetail.Gender.Value;

                // Database update
                newStageLog = new StageLog {
                    game_id     = dto.gameId,
                    action_type = dto.actionType,
                    score       = emotionScore,
                    file_loc    = keyName,
                    age         = evaluatedAge,
                    gender      = evaluatedGender,
                    log_date    = DateTime.Now
                };

                var value = _context.StageLog.Add(newStageLog);
                await _context.SaveChangesAsync();
            }

            // Send response
            string signedURL = S3Util.GetPresignedURL(this.S3Client, bucketName, keyName);

            newStageLog.file_loc = signedURL;

            return(Ok(newStageLog));
        }