github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/s3/enumerate.go (about) 1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package s3 18 19 import ( 20 "log" 21 22 "camlistore.org/pkg/blob" 23 "camlistore.org/pkg/blobserver" 24 "camlistore.org/pkg/context" 25 ) 26 27 var _ blobserver.MaxEnumerateConfig = (*s3Storage)(nil) 28 29 func (sto *s3Storage) MaxEnumerate() int { return 1000 } 30 31 // marker returns the string lexically greater than the provided s 32 // with the same length as s. 33 func nextStr(s string) string { 34 if s == "" { 35 return s 36 } 37 b := []byte(s) 38 i := len(b) 39 for i > 0 { 40 i-- 41 b[i]++ 42 if b[i] != 0 { 43 break 44 } 45 } 46 return string(b) 47 } 48 49 func (sto *s3Storage) EnumerateBlobs(ctx *context.Context, dest chan<- blob.SizedRef, after string, limit int) (err error) { 50 defer close(dest) 51 if faultEnumerate.FailErr(&err) { 52 return 53 } 54 startAt := after 55 if _, ok := blob.Parse(after); ok { 56 startAt = nextStr(after) 57 } 58 objs, err := sto.s3Client.ListBucket(sto.bucket, startAt, limit) 59 if err != nil { 60 log.Printf("s3 ListBucket: %v", err) 61 return err 62 } 63 for _, obj := range objs { 64 if obj.Key == after { 65 continue 66 } 67 br, ok := blob.Parse(obj.Key) 68 if !ok { 69 continue 70 } 71 select { 72 case dest <- blob.SizedRef{Ref: br, Size: uint32(obj.Size)}: 73 case <-ctx.Done(): 74 return context.ErrCanceled 75 } 76 } 77 return nil 78 }