示例#1
0
        private static void ListObjectCallback(IAsyncResult ar)
        {
            try
            {
                var result = client.EndListObjects(ar);

                Console.WriteLine("List objects of bucket:{0} succeeded ", result.BucketName);

                foreach (var summary in result.ObjectSummaries)
                {
                    Console.WriteLine(summary.Key);
                }
            }
            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);
            }
            finally
            {
                _event.Set();
            }
        }
示例#2
0
        private static void ListObjectCallback(IAsyncResult ar)
        {
            var result = ossClient.EndListObjects(ar);

            foreach (var summary in result.ObjectSummaries)
            {
                Console.WriteLine(summary.Key);
            }

            evnet.Set();
        }
        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
            }));
        }
 private void ListObjectCallback(IAsyncResult ar)
 {
     try
     {
         var result = client.EndListObjects(ar);
         globalSummaryList = result.ObjectSummaries;
         resetEvent.Set();
     }
     catch (OssException ex)
     {
         lastError = ex;
     }
 }
        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
            });
        }