コード例 #1
0
        public APIGatewayProxyResponse GenPresignedURLHandler(APIGatewayProxyRequest request)
        {
            var requestValidator       = new GetUrlRequestValidator();
            var requestValidatorResult = requestValidator.Validate(request);

            LambdaLogger.Log("Request validation response: " + requestValidatorResult.IsValid);

            // validate request
            if (!requestValidatorResult.IsValid)
            {
                var badRequestResponse = new APIGatewayProxyResponse
                {
                    StatusCode = 400,
                    Body       = JsonConvert.SerializeObject(requestValidatorResult.Errors),
                    Headers    = new Dictionary <string, string>
                    {
                        { "Content-Type", "application/json" },
                        { "Access-Control-Allow-Origin", "*" }
                    }
                };
                LambdaLogger.Log(JsonConvert.SerializeObject(requestValidatorResult.Errors));
                return(badRequestResponse);
            }

            LambdaLogger.Log("Input filename: " + request.QueryStringParameters["fileName"]);

            //create S3 bucket information model class
            var bucketInfo = new S3LookupModel(request.QueryStringParameters["bucketName"],
                                               request.QueryStringParameters["objectPath"],
                                               request.QueryStringParameters["region"],
                                               request.QueryStringParameters["fileName"],
                                               Amazon.S3.HttpVerb.GET);

            //generate pre-signed URL
            GeneratePreSignedUrl generatePresignedUrl = new GeneratePreSignedUrl(bucketInfo);

            var response = new APIGatewayProxyResponse
            {
                StatusCode = 200,
                Body       = JsonConvert.SerializeObject(new ResponseBoundary
                {
                    PresignedUrl = generatePresignedUrl.GenerateURL(),
                    Bucket       = bucketInfo.BucketName,
                    ObjectKey    = bucketInfo.ObjectKey,
                    Region       = bucketInfo.RegionEndpoint,
                    Expiry       = DateTime.Now.AddHours(2).ToString() //I am adding 2 hours for the JSON response to say the correct time as AWS has an ongoing issue of working with GMT+00 which does not take into consideration daylight saving hours.
                }),
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", "application/json" },
                    { "Access-Control-Allow-Origin", "*" }
                }
            };

            return(response);
        }
コード例 #2
0
 public GeneratePreSignedUrl(S3LookupModel _bucketInfo)
 {
     bucketInfo = _bucketInfo;
     s3Client   = new AmazonS3Client(RegionEndpoint.GetBySystemName(bucketInfo.RegionEndpoint));
 }