Пример #1
0
        public async Task <ScoreMeta> GetScoreMetaAsync(string scoreName)
        {
            var convertor = new ScoreMetaConvertor();

            var key = $"{scoreName}/{ScoreMeta.FileName}";


            Stream currentStream;

            try
            {
                var request = new GetObjectRequest
                {
                    BucketName = BucketName,
                    Key        = key,
                };

                var response = await this.S3Client.GetObjectAsync(request);

                currentStream = response.ResponseStream;
            }
            catch (AmazonS3Exception e)
            {
                if (e.ErrorCode == "NoSuchKey")
                {
                    throw new InvalidOperationException($"'{scoreName}' は存在しません");
                }

                throw;
            }

            var scoreMeta = await convertor.ConvertToScoreMeta(currentStream);

            return(scoreMeta);
        }
Пример #2
0
        public async Task SaveScoreMetaAsync(string scoreName, ScoreMeta scoreMeta, string metaKey)
        {
            var convertor = new ScoreMetaConvertor();
            var key       = $"{scoreName}/{ScoreMeta.FileName}";

            int retryCount = 5;

            for (int i = 0; i < retryCount; i++)
            {
                try
                {
                    var stream = await convertor.ConvertToUtf(scoreMeta);

                    var putRequest = new PutObjectRequest
                    {
                        BucketName  = BucketName,
                        Key         = key,
                        InputStream = stream,
                        CannedACL   = S3CannedACL.PublicRead,
                    };
                    await this.S3Client.PutObjectAsync(putRequest);
                }
                catch (AmazonS3Exception)
                {
                    // Error
                    throw;
                }


                try
                {
                    var request = new GetObjectRequest
                    {
                        BucketName = BucketName,
                        Key        = key,
                    };

                    var response = await this.S3Client.GetObjectAsync(request);

                    var stream = response.ResponseStream;

                    var checkedMeta = await convertor.ConvertToScoreMeta(stream);

                    // 同一のキーが登録される可能性があるがキーには少数3桁の時間を指定してるので
                    // ほぼキーが重なる自体はないと考える
                    // もしキーの重複が問題になる用であれば内容の比較も行う
                    if (checkedMeta.ContainsKey(metaKey))
                    {
                        return;
                    }
                }
                catch (AmazonS3Exception)
                {
                    // skip
                }

                if (i < (retryCount - 1))
                {
                    Thread.Sleep(2000);
                }
            }

            throw new InvalidOperationException("追加更新に失敗しました");
        }