Exemple #1
0
        private Response ListObjects(string bucket)
        {
            var bucketObject = storage.GetBucket(bucket);

            if (bucketObject == null)
            {
                var responseNotFound = responder.Respond(new BucketNotFound {
                    BucketName = bucket
                });
                responseNotFound.StatusCode = HttpStatusCode.NotFound;
                return(responseNotFound);
            }

            var searchRequest = new S3ObjectSearchRequest
            {
                BucketName = bucket,
                Prefix     = Request.Query.prefix.HasValue ? Request.Query.prefix : string.Empty,
                Delimiter  = Request.Query.delimiter.HasValue ? Request.Query.delimiter : string.Empty,
                Marker     = Request.Query.marker.HasValue ? Request.Query.marker : string.Empty,
                MaxKeys    = Request.Query.maxkeys.HasValue ? Request.Query.maxkeys : 1000,
            };

            var searchResponse = storage.GetObjects(searchRequest);
            var response       = responder.Respond(searchResponse);

            return(response);
        }
Exemple #2
0
        private void ApplyDelimiterFilter(S3ObjectSearchRequest searchRequest, S3ObjectSearchResponse searchResponse)
        {
            IList <string>   prefixStrings   = new List <string>();
            IList <S3Object> objectsToRemove = new List <S3Object>();

            foreach (var s3Object in searchResponse.S3Objects)
            {
                var match = Regex.Match(s3Object.Key,
                                        string.Format("({0}.*?{1}).*?", searchRequest.Prefix, searchRequest.Delimiter));
                if (!match.Success)
                {
                    continue;
                }

                var @group = match.Groups[0].Value;
                if (!prefixStrings.Contains(@group))
                {
                    prefixStrings.Add(@group);
                }
                objectsToRemove.Add(s3Object);
            }

            foreach (var s3Object in objectsToRemove)
            {
                searchResponse.S3Objects.Remove(s3Object);
            }

            searchResponse.Prefixes = prefixStrings;
        }
Exemple #3
0
        public S3ObjectSearchResponse GetObjects(S3ObjectSearchRequest searchRequest)
        {
            var searchResponse = new S3ObjectSearchResponse
            {
                BucketName  = searchRequest.BucketName,
                Delimiter   = searchRequest.Delimiter,
                Marker      = searchRequest.Marker,
                MaxKeys     = searchRequest.MaxKeys,
                Prefix      = searchRequest.Prefix,
                IsTruncated = false,
                S3Objects   = new List <S3Object>(),
                Prefixes    = new List <string>()
            };

            var bucket = GetBucket(searchRequest.BucketName);

            if (bucket == null)
            {
                return(searchResponse);
            }

            QueryS3Objects(searchRequest, searchResponse);

            return(searchResponse);
        }
        private async Task <IActionResult> ListObjectsAsync(string bucket, CancellationToken cancellationToken)
        {
            var bucketObject = await _storage.GetBucketAsync(bucket, cancellationToken);

            if (bucketObject == null)
            {
                var responseNotFound = _responder.RespondContent(new BucketNotFound {
                    BucketName = bucket
                });
                responseNotFound.StatusCode = (int)HttpStatusCode.NotFound;
                return(responseNotFound);
            }

            var searchRequest = new S3ObjectSearchRequest
            {
                BucketName = bucket,
                Prefix     = Request.Query.ContainsKey("prefix") ? Request.Query["prefix"][0] : string.Empty,
                Delimiter  = Request.Query.ContainsKey("delimiter") ? Request.Query["delimiter"][0] : string.Empty,
                Marker     = Request.Query.ContainsKey("marker") ? Request.Query["marker"][0] : string.Empty,
            };

            var searchResponse = await _storage.SearchObjectsAsync(searchRequest, cancellationToken);

            var response = _responder.RespondContent(searchResponse);

#if DEBUG
            if (_logger.IsEnabled(LogLevel.Information))
            {
#else
            if (_logger.IsEnabled(LogLevel.Trace))
            {
#endif
#if DEBUG
                _logger.LogInformation
#else
                _logger.LogTrace
#endif
                    ("GET Request: bucket='{0}' query='{1}' response='{2}'",
                    bucket,
                    Request.QueryString.HasValue?Request.QueryString.Value : string.Empty,
                    string.IsNullOrEmpty(response.Content)?string.Empty : response.Content);
            }

            return(response);
        }
    }
Exemple #5
0
        private void QueryS3Objects(S3ObjectSearchRequest searchRequest, S3ObjectSearchResponse searchResponse)
        {
            using (var session = documentStore.OpenSession())
            {
                var query = string.IsNullOrWhiteSpace(searchRequest.Prefix) ? string.Format("Bucket:{0}", searchRequest.BucketName) :
                            string.Format("Bucket:{0} AND Key:{1}*", searchRequest.BucketName, searchRequest.Prefix);

                searchResponse.S3Objects = session.Advanced
                                           .LuceneQuery <S3Object, S3Object_Search>()
                                           .Where(query)
                                           .Take(searchRequest.MaxKeys ?? 1000)
                                           .WaitForNonStaleResultsAsOfNow()
                                           .ToList();
            }

            if (!string.IsNullOrWhiteSpace(searchRequest.Delimiter))
            {
                ApplyDelimiterFilter(searchRequest, searchResponse);
            }
        }