Golang's http.FileSystem Implementation for AWS S3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.2 KiB

package s3fs
import (
"errors"
"os"
"path"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
func listObjects(bucket, prefix, delimiter string) ([]os.FileInfo, error) {
initSVC()
output, err := s3svc.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(bucket),
Prefix: aws.String(prefix),
Delimiter: aws.String(delimiter),
})
if err != nil {
return nil, err
}
infos := []os.FileInfo{}
for _, d := range output.CommonPrefixes {
fi := fileinfo{
isDir: true,
key: path.Clean(*d.Prefix),
modTime: time.Now(),
}
infos = append(infos, fi)
}
for _, f := range output.Contents {
if !strings.HasSuffix(*f.Key, "/") {
fi := fileinfo{
isDir: false,
key: path.Clean(*f.Key),
modTime: *f.LastModified,
size: *f.Size,
}
infos = append(infos, fi)
}
}
return infos, nil
}
func getInfo(bucket, key string) (os.FileInfo, error) {
var f fileinfo
o, err := listObjects(bucket, key, "/")
if err != nil {
return nil, err
}
if len(o) == 1 {
f = o[0].(fileinfo)
return f, nil
}
// in case if there are common prefixes
for _, i := range o {
if i.Name() == path.Base(key) {
return i, nil
}
}
return nil, errors.New("getInfo: unknown")
}