2011-04-09 04:05:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2012-12-22 21:50:55 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
2013-12-02 21:20:51 +00:00
|
|
|
"camlistore.org/pkg/context"
|
2011-04-09 04:05:39 +00:00
|
|
|
)
|
|
|
|
|
2012-12-22 21:50:55 +00:00
|
|
|
var _ blobserver.MaxEnumerateConfig = (*s3Storage)(nil)
|
|
|
|
|
|
|
|
func (sto *s3Storage) MaxEnumerate() int { return 1000 }
|
2011-04-09 04:05:39 +00:00
|
|
|
|
2014-03-04 20:11:59 +00:00
|
|
|
// marker returns the string lexically greater than the provided s
|
|
|
|
// with the same length as s.
|
|
|
|
func nextStr(s string) string {
|
|
|
|
if s == "" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
b := []byte(s)
|
|
|
|
i := len(b)
|
|
|
|
for i > 0 {
|
|
|
|
i--
|
|
|
|
b[i]++
|
|
|
|
if b[i] != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2014-03-17 02:39:43 +00:00
|
|
|
func (sto *s3Storage) EnumerateBlobs(ctx *context.Context, dest chan<- blob.SizedRef, after string, limit int) (err error) {
|
2011-04-09 04:05:39 +00:00
|
|
|
defer close(dest)
|
2014-03-17 02:39:43 +00:00
|
|
|
if faultEnumerate.FailErr(&err) {
|
|
|
|
return
|
|
|
|
}
|
2014-03-04 20:11:59 +00:00
|
|
|
startAt := after
|
|
|
|
if _, ok := blob.Parse(after); ok {
|
|
|
|
startAt = nextStr(after)
|
|
|
|
}
|
|
|
|
objs, err := sto.s3Client.ListBucket(sto.bucket, startAt, limit)
|
2011-04-09 04:05:39 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("s3 ListBucket: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, obj := range objs {
|
2014-03-04 20:11:59 +00:00
|
|
|
if obj.Key == after {
|
|
|
|
continue
|
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
br, ok := blob.Parse(obj.Key)
|
|
|
|
if !ok {
|
2011-04-09 04:05:39 +00:00
|
|
|
continue
|
|
|
|
}
|
2013-12-02 21:20:51 +00:00
|
|
|
select {
|
2014-01-28 20:46:52 +00:00
|
|
|
case dest <- blob.SizedRef{Ref: br, Size: uint32(obj.Size)}:
|
2013-12-02 21:20:51 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return context.ErrCanceled
|
|
|
|
}
|
2011-04-09 04:05:39 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|