Пример #1
0
        public async Task <StoreData.Response> Save(LambdaStoreArg data)
        {
            var resp = new StoreData.Response()
            {
                status = StoreError.E_CHAOS.ToString()
            };

            try {
                using (var s3Client = new AmazonS3Client(Amazon.RegionEndpoint.APNortheast1))
                {
                    var s3Req = new PutObjectRequest
                    {
                        BucketName  = data.s3Bucket,
                        Key         = data.s3Path,
                        ContentType = ((StoreData.Type)data.dataType).ToContentType(),
                        ContentBody = encoding.GetString(Convert.FromBase64String(data.dataBody))
                    };
                    var s3Resp = await s3Client.PutObjectAsync(s3Req);

                    resp.status = StoreError.E_OK.ToString();
                }
            }
            catch (Exception e)
            {
                m_ctx.Log(e.ToString());
                resp.status = StoreError.E_CRITICAL.ToString();
            }

            return(resp);
        }
Пример #2
0
        public async Task <StoreData.Response> Load(LambdaStoreArg data)
        {
            var resp = new StoreData.Response()
            {
                status = StoreError.E_CHAOS.ToString()
            };

            try{
                using (var s3Client = new AmazonS3Client(Amazon.RegionEndpoint.APNortheast1))
                {
                    var s3Req = new GetObjectRequest
                    {
                        BucketName = data.s3Bucket,
                        Key        = data.s3Path
                    };
                    var s3Resp = await s3Client.GetObjectAsync(s3Req);

                    using (var s3Stream = new StreamReader(s3Resp.ResponseStream)){
                        var dataBody = s3Stream.ReadToEnd();
                        resp.status   = StoreError.E_OK.ToString();
                        resp.dataBody = Convert.ToBase64String(encoding.GetBytes(dataBody));
                    }
                }
            }
            catch (Exception ex) {
                m_ctx.Log(ex.Message);
                resp.status = StoreError.E_CRITICAL.ToString();
                resp.meta   = data.s3Path + " : " + ex.Message;
            }

            return(resp);
        }
Пример #3
0
        async Task <StoreData.Response> ServerJob(LambdaStoreArg data, ILambdaContext ctx)
        {
            StoreData.Response ret = null;

            var storeServer = new StoreServer(ctx);

            // 環境変数に依存する処理
            var env     = System.Environment.GetEnvironmentVariable("LAMBDA_ENV");
            var service = System.Environment.GetEnvironmentVariable("LAMBDA_SERVICE");

            data.s3Bucket = System.Environment.GetEnvironmentVariable("S3_BUCKET");

            ctx.Log("s3Bucket : " + data.s3Bucket);
            ctx.Log(data.mode + " >> Target Object : " + data.s3Path);

            if (data.mode == "load")
            {
                ret = await storeServer.Load(data);
            }
            else if (data.mode == "save")
            {
                ret = await storeServer.Save(data);
            }
            else
            {
                ret = await Task.Run(() => { return(new StoreData.Response()
                    {
                        status = StoreError.E_CHAOS.ToString()
                    }); });
            }

            return(ret);
        }
Пример #4
0
        async Task <StoreData.Response> ClientJob(LambdaStoreArg data, ILambdaContext ctx)
        {
            StoreData.Response ret = null;

            var env     = System.Environment.GetEnvironmentVariable("LAMBDA_ENV");
            var service = System.Environment.GetEnvironmentVariable("LAMBDA_SERVICE");

            data.apiUrl = System.Environment.GetEnvironmentVariable("DEBUG_STORE_API_URL");
            data.apiKey = System.Environment.GetEnvironmentVariable("DEBUG_STORE_API_KEY");

            ctx.Log(data.mode + " >> Target Object : " + data.s3Path);

            var storeClient = new StoreClient(data.apiUrl, data.apiKey, ctx);

            if (data.mode == "load")
            {
                ret = await storeClient.Load(data.sessCode, data.dataName, data.dataIndex);
            }
            else if (data.mode == "save")
            {
                ret = await storeClient.Save(data.sessCode, data.dataBody, data.dataType, data.dataName, data.dataIndex);
            }
            else
            {
                ret = await Task.Run(() => { return(new StoreData.Response()
                    {
                        status = StoreError.E_CHAOS.ToString()
                    }); });
            }

            return(ret);
        }
Пример #5
0
        public async Task <StoreData.Response> Save(string sessCode,
                                                    string dataBody, StoreData.Type dataType,
                                                    string dataName, int dataIndex = 1)
        {
            var resp = new StoreData.Response()
            {
                status = StoreError.E_CHAOS.ToString()
            };

            Log(m_apiUrl);
            Log(m_apiKey);

            using (var client = new HttpClient()){
                try {
                    var request = new HttpRequestMessage(HttpMethod.Post, m_apiUrl);
                    request.Content = new StringContent
                                      (
                        JsonSerializer.ToJsonString(new StoreData.Request()
                    {
                        role      = "server",
                        mode      = "save",
                        sessCode  = sessCode,
                        dataName  = dataName,
                        dataIndex = dataIndex,
                        data      = dataBody,
                        dataType  = (int)dataType
                    })
                        , new UTF8Encoding()
                        , dataType.ToContentType()
                                      );

                    request.Headers.Add(@"x-api-key", m_apiKey);

                    var response = await client.SendAsync(request);

                    var responseContent = await response.Content.ReadAsStringAsync();

                    if (!String.IsNullOrEmpty(responseContent))
                    {
                        Log("Got Resp >> " + responseContent);
                        resp = JsonSerializer.Deserialize <StoreData.Response>(responseContent);
                    }
                    else
                    {
                        resp.status = StoreError.E_CRITICAL.ToString();
                    }
                }
                catch (Exception e) {
                    Log("Exception : " + e.ToString());
                    resp.status = StoreError.E_CRITICAL.ToString();
                }
            }
            return(resp);
        }
Пример #6
0
        public override string Handler(LambdaStoreArg data, ILambdaContext context)
        {
            m_ctx  = context;
            m_data = data;

            m_data.sw.Start();

            m_ctx.Log(String.Format("{0:D8} : Start Process", m_data.sw.ElapsedMilliseconds));
            m_ctx.Log(data.dataName);

            string resp;

            if (data.role == "client")
            {
                Task <StoreData.Response> result = ClientJob(data, context);
                m_ctx.Log(String.Format("{0:D8} : Wait Async Method", m_data.sw.ElapsedMilliseconds));
                resp = JsonSerializer.ToJsonString(result.Result);
            }
            else if (data.role == "server")
            {
                Task <StoreData.Response> result = ServerJob(data, context);
                m_ctx.Log(String.Format("{0:D8} : Wait Async Method", m_data.sw.ElapsedMilliseconds));
                resp = JsonSerializer.ToJsonString(result.Result);
            }
            else
            {
                var dummyResp = new StoreData.Response()
                {
                    status = StoreError.E_CHAOS.ToString()
                };
                resp = JsonSerializer.ToJsonString(dummyResp);
            }

            m_ctx.Log(String.Format("{0:D8} : Complete Process", m_data.sw.ElapsedMilliseconds));

            return(resp);
        }