Ejemplo n.º 1
0
        public async Task <IEnumerable <FileSpec> > GetFileListAsync(string searchPattern = null, int?limit = null, int?skip = null,
                                                                     CancellationToken cancellationToken = default(CancellationToken))
        {
            if (limit.HasValue && limit.Value <= 0)
            {
                return(new List <FileSpec>());
            }

            searchPattern = searchPattern?.Replace('\\', '/');
            string prefix       = searchPattern;
            Regex  patternRegex = null;
            int    wildcardPos  = searchPattern?.IndexOf('*') ?? -1;

            if (searchPattern != null && wildcardPos >= 0)
            {
                patternRegex = new Regex("^" + Regex.Escape(searchPattern).Replace("\\*", ".*?") + "$");
                int slashPos = searchPattern.LastIndexOf('/');
                prefix = slashPos >= 0 ? searchPattern.Substring(0, slashPos) : String.Empty;
            }
            prefix = prefix ?? String.Empty;

            string marker = null;
            var    blobs  = new List <OssObjectSummary>();

            do
            {
                var listing = await Task.Factory.FromAsync(
                    (request, callback, state) => _client.BeginListObjects(request, callback, state),
                    result => _client.EndListObjects(result), new ListObjectsRequest(_bucket) {
                    Prefix  = prefix,
                    Marker  = marker,
                    MaxKeys = limit
                },
                    null
                    ).AnyContext();

                marker = listing.NextMarker;

                blobs.AddRange(listing.ObjectSummaries.Where(blob => patternRegex == null || patternRegex.IsMatch(blob.Key)));
            } while (!cancellationToken.IsCancellationRequested && !String.IsNullOrEmpty(marker) && blobs.Count < limit.GetValueOrDefault(Int32.MaxValue));

            if (limit.HasValue)
            {
                blobs = blobs.Take(limit.Value).ToList();
            }

            return(blobs.Select(blob => new FileSpec {
                Path = blob.Key,
                Size = blob.Size,
                Created = blob.LastModified,
                Modified = blob.LastModified
            }));
        }
 public IEnumerable <OssObjectSummary> ListObjectsAsync()
 {
     try
     {
         resetEvent.Reset();
         var listObjectsRequest = new ListObjectsRequest(bucketName);
         client.BeginListObjects(listObjectsRequest, ListObjectCallback, null);
         resetEvent.WaitOne();
     }
     catch (OssException ex)
     {
         lastError = ex;
     }
     return(globalSummaryList);
 }
Ejemplo n.º 3
0
        public static void AsyncListObjects(string bucketName)
        {
            try
            {
                var listObjectsRequest = new ListObjectsRequest(bucketName);
                client.BeginListObjects(listObjectsRequest, ListObjectCallback, null);

                _event.WaitOne();
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
        private async Task <NextPageResult> GetFiles(string searchPattern, int page, int pageSize, CancellationToken cancellationToken)
        {
            int pagingLimit = pageSize;
            int skip        = (page - 1) * pagingLimit;

            if (pagingLimit < Int32.MaxValue)
            {
                pagingLimit = pagingLimit + 1;
            }

            searchPattern = searchPattern?.Replace('\\', '/');
            string prefix       = searchPattern;
            Regex  patternRegex = null;
            int    wildcardPos  = searchPattern?.IndexOf('*') ?? -1;

            if (searchPattern != null && wildcardPos >= 0)
            {
                patternRegex = new Regex("^" + Regex.Escape(searchPattern).Replace("\\*", ".*?") + "$");
                int slashPos = searchPattern.LastIndexOf('/');
                prefix = slashPos >= 0 ? searchPattern.Substring(0, slashPos) : String.Empty;
            }
            prefix = prefix ?? String.Empty;

            string marker = null;
            var    blobs  = new List <OssObjectSummary>();

            do
            {
                var listing = await Task.Factory.FromAsync(
                    (request, callback, state) => _client.BeginListObjects(request, callback, state),
                    result => _client.EndListObjects(result), new ListObjectsRequest(_bucket) {
                    Prefix  = prefix,
                    Marker  = marker,
                    MaxKeys = pagingLimit
                },
                    null
                    ).AnyContext();

                marker = listing.NextMarker;

                blobs.AddRange(listing.ObjectSummaries.Where(blob => patternRegex == null || patternRegex.IsMatch(blob.Key)));
            } while (!cancellationToken.IsCancellationRequested && !String.IsNullOrEmpty(marker) && blobs.Count < pagingLimit);

            var list = blobs.Select(blob => new FileSpec {
                Path     = blob.Key,
                Size     = blob.Size,
                Created  = blob.LastModified,
                Modified = blob.LastModified
            })
                       .Take(pagingLimit)
                       .ToList();

            bool hasMore = false;

            if (list.Count == pagingLimit)
            {
                hasMore = true;
                list.RemoveAt(pagingLimit - 1);
            }

            return(new NextPageResult {
                Success = true,
                HasMore = hasMore,
                Files = list,
                NextPageFunc = hasMore ? s => GetFiles(searchPattern, page + 1, pageSize, cancellationToken) : (Func <PagedFileListResult, Task <NextPageResult> >)null
            });
        }