예제 #1
0
        /// <summary>
        /// Queries a bucket for a listing of objects it contains. Only objects with keys
        /// beginning with the given prefix will be returned.
        /// </summary>
        public IEnumerable <ListEntry> ListAllObjects(string bucketName, string prefix, string delimiter)
        {
            var args = new ListObjectsArgs
            {
                Prefix    = prefix,
                Delimiter = delimiter
            };

            while (true)
            {
                var request = new ListObjectsRequest(this, bucketName, args);

                using (var response = request.GetResponse())
                {
                    ListEntry lastEntry = null;

                    foreach (var entry in response.Entries)
                    {
                        lastEntry = entry;
                        yield return(entry);
                    }

                    if (response.IsTruncated)
                    {
                        // if you specified a delimiter, S3 is supposed to give us the marker
                        // name to use in order to get the next set of "stuff".
                        if (response.NextMarker != null)
                        {
                            args.Marker = response.NextMarker;
                        }
                        // if you didn't specify a delimiter, you won't get any CommonPrefixes,
                        // so we'll use the last ObjectEntry's key as the next delimiter.
                        else if (lastEntry is ObjectEntry)
                        {
                            args.Marker = (lastEntry as ObjectEntry).Key;
                        }
                        else
                        {
                            throw new Exception("S3 Server is misbehaving.");
                        }
                    }
                    else
                    {
                        break; // we're done!
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Queries a bucket for a listing of objects it contains. Only objects with keys
        /// beginning with the given prefix will be returned. The DefaultDelimiter will
        /// be used. If you expect a large number of objects to be returned, consider using
        /// ListAllObjects().
        /// </summary>
        public IList <ListEntry> ListObjects(string bucketName, string prefix)
        {
            var args = new ListObjectsArgs {
                Prefix = prefix, Delimiter = DefaultDelimiter
            };
            var request = new ListObjectsRequest(this, bucketName, args);

            using (ListObjectsResponse response = request.GetResponse())
            {
                if (response.IsTruncated)
                {
                    throw new Exception("The server truncated the list of items requested. Consider using the ListObjectsRequest class to query for large numbers of items.");
                }

                return(response.Entries.ToList());
            }
        }
예제 #3
0
        /// <summary>
        /// Queries S3 about the existance and ownership of the given bucket name.
        /// </summary>
        public BucketAccess QueryBucket(string bucketName)
        {
            try
            {
                // recommended technique from amazon: try and list contents of the bucket with 0 maxkeys
                var args = new ListObjectsArgs {
                    MaxKeys = 0
                };
                new ListObjectsRequest(this, bucketName, args).GetResponse().Close();

                return(BucketAccess.Accessible);
            }
            catch (S3Exception exception)
            {
                switch (exception.ErrorCode)
                {
                case S3ErrorCode.NoSuchBucket: return(BucketAccess.NoSuchBucket);

                case S3ErrorCode.AccessDenied: return(BucketAccess.NotAccessible);

                default: throw;
                }
            }
        }
예제 #4
0
 public ListObjectsRequest(S3Service service, string bucketName, ListObjectsArgs args)
     : base(service, "GET", bucketName, null, args != null ? args.ToQueryString() : null)
 {
     this.BucketName = bucketName;
 }