示例#1
0
 public ListBucketResult ListBucket(ListBucketRequest request)
 {
     return Util.RetryMethod<ListBucketResult>(() => DoListBucket(request), RetryCount);
 }
示例#2
0
        private ListBucketResult DoListBucket(ListBucketRequest request)
        {
            StringBuilder sb = new StringBuilder("?", 256);

            if (!string.IsNullOrEmpty(request.Delimiter))
                sb.Append(String.Concat("delimiter=", Util.UrlEncode(request.Delimiter), "&"));
            if (!string.IsNullOrEmpty(request.Marker))
                sb.Append(String.Concat("marker=", Util.UrlEncode(request.Marker), "&"));
            if (request.MaxKeys > 0)
                sb.Append(String.Concat("max-keys=", request.MaxKeys, "&"));
            if (!string.IsNullOrEmpty(request.Prefix))
                sb.Append(String.Concat("prefix=", Util.UrlEncode(request.Prefix), "&"));

            string parameters = sb.ToString();

            if (parameters.EndsWith("&", StringComparison.OrdinalIgnoreCase))
                parameters = parameters.Remove(parameters.Length - 1);

            if (parameters.Length <= 1)
                parameters = null;

            var wRequest = CreateRequest("GET", "text/plain", request.BucketName, Util.UrlEncode(request.Delimiter, true), parameters);

            using (var response = wRequest.GetResponse() as HttpWebResponse)
            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                var body = reader.ReadToEnd();
                var element = XElement.Parse(body);

                XmlSerializer serializer = new XmlSerializer(typeof(ListBucketResult));
                var listBucketResult = (ListBucketResult)serializer.Deserialize(element.CreateReader());

                if (listBucketResult.IsTruncated && listBucketResult.S3Objects.Count > 0)
                    listBucketResult.NextMarker = listBucketResult.S3Objects.Last().Key;

                return listBucketResult;
            }
        }