Chris Lu
4 years ago
9 changed files with 135 additions and 184 deletions
-
2weed/filer/filer_conf.go
-
27weed/filer/read.go
-
63weed/filer/read_write.go
-
25weed/filer/s3iam_conf.go
-
31weed/filer/s3iam_conf_test.go
-
27weed/s3api/auth_credentials.go
-
95weed/s3iam/s3iam_filer_store.go
-
16weed/shell/command_fs_configure.go
-
33weed/shell/command_s3_configure.go
@ -1,27 +0,0 @@ |
|||
package filer |
|||
|
|||
import ( |
|||
"bytes" |
|||
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb" |
|||
"github.com/chrislusf/seaweedfs/weed/wdclient" |
|||
"math" |
|||
) |
|||
|
|||
func ReadEntry(masterClient *wdclient.MasterClient, filerClient filer_pb.SeaweedFilerClient, dir, name string, byteBuffer *bytes.Buffer) error { |
|||
|
|||
request := &filer_pb.LookupDirectoryEntryRequest{ |
|||
Directory: dir, |
|||
Name: name, |
|||
} |
|||
respLookupEntry, err := filer_pb.LookupEntry(filerClient, request) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
if len(respLookupEntry.Entry.Content) > 0 { |
|||
_, err = byteBuffer.Write(respLookupEntry.Entry.Content) |
|||
return err |
|||
} |
|||
|
|||
return StreamContent(masterClient, byteBuffer, respLookupEntry.Entry.Chunks, 0, math.MaxInt64) |
|||
|
|||
} |
@ -0,0 +1,63 @@ |
|||
package filer |
|||
|
|||
import ( |
|||
"bytes" |
|||
"fmt" |
|||
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb" |
|||
"github.com/chrislusf/seaweedfs/weed/util" |
|||
"github.com/chrislusf/seaweedfs/weed/wdclient" |
|||
"math" |
|||
"net/http" |
|||
) |
|||
|
|||
func ReadEntry(masterClient *wdclient.MasterClient, filerClient filer_pb.SeaweedFilerClient, dir, name string, byteBuffer *bytes.Buffer) error { |
|||
|
|||
request := &filer_pb.LookupDirectoryEntryRequest{ |
|||
Directory: dir, |
|||
Name: name, |
|||
} |
|||
respLookupEntry, err := filer_pb.LookupEntry(filerClient, request) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
if len(respLookupEntry.Entry.Content) > 0 { |
|||
_, err = byteBuffer.Write(respLookupEntry.Entry.Content) |
|||
return err |
|||
} |
|||
|
|||
return StreamContent(masterClient, byteBuffer, respLookupEntry.Entry.Chunks, 0, math.MaxInt64) |
|||
|
|||
} |
|||
|
|||
func ReadContent(filerAddress string, dir, name string) ([]byte, error) { |
|||
|
|||
target := fmt.Sprintf("http://%s%s/%s", filerAddress, dir, name) |
|||
|
|||
data, _, err := util.Get(target) |
|||
|
|||
return data, err |
|||
} |
|||
|
|||
func SaveAs(host string, port int, dir, name string, contentType string, byteBuffer *bytes.Buffer) error { |
|||
|
|||
target := fmt.Sprintf("http://%s:%d%s/%s", host, port, dir, name) |
|||
|
|||
// set the HTTP method, url, and request body
|
|||
req, err := http.NewRequest(http.MethodPut, target, byteBuffer) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
// set the request header Content-Type for json
|
|||
if contentType != "" { |
|||
req.Header.Set("Content-Type", contentType) |
|||
} |
|||
resp, err := http.DefaultClient.Do(req) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
util.CloseResponse(resp) |
|||
|
|||
return nil |
|||
|
|||
} |
@ -0,0 +1,25 @@ |
|||
package filer |
|||
|
|||
import ( |
|||
"bytes" |
|||
"github.com/chrislusf/seaweedfs/weed/pb/iam_pb" |
|||
"github.com/golang/protobuf/jsonpb" |
|||
"io" |
|||
) |
|||
|
|||
func ParseS3ConfigurationFromBytes(content []byte, config *iam_pb.S3ApiConfiguration) error { |
|||
if err := jsonpb.Unmarshal(bytes.NewBuffer(content), config); err != nil { |
|||
return err |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
func S3ConfigurationToText(writer io.Writer, config *iam_pb.S3ApiConfiguration) error { |
|||
|
|||
m := jsonpb.Marshaler{ |
|||
EmitDefaults: false, |
|||
Indent: " ", |
|||
} |
|||
|
|||
return m.Marshal(writer, config) |
|||
} |
@ -1,95 +0,0 @@ |
|||
package s3iam |
|||
|
|||
import ( |
|||
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb" |
|||
"github.com/chrislusf/seaweedfs/weed/pb/iam_pb" |
|||
"time" |
|||
|
|||
proto "github.com/golang/protobuf/proto" |
|||
) |
|||
|
|||
const ( |
|||
iamConfigPrefix = "/etc/iam" |
|||
iamIdentityFile = "identity.json" |
|||
) |
|||
|
|||
type IAMFilerStore struct { |
|||
client *filer_pb.SeaweedFilerClient |
|||
} |
|||
|
|||
func NewIAMFilerStore(client *filer_pb.SeaweedFilerClient) *IAMFilerStore { |
|||
return &IAMFilerStore{client: client} |
|||
} |
|||
|
|||
func (ifs *IAMFilerStore) getIAMConfigRequest() *filer_pb.LookupDirectoryEntryRequest { |
|||
return &filer_pb.LookupDirectoryEntryRequest{ |
|||
Directory: iamConfigPrefix, |
|||
Name: iamIdentityFile, |
|||
} |
|||
} |
|||
|
|||
func (ifs *IAMFilerStore) LoadIAMConfig(config *iam_pb.S3ApiConfiguration) error { |
|||
resp, err := filer_pb.LookupEntry(*ifs.client, ifs.getIAMConfigRequest()) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
err = ifs.loadIAMConfigFromBytes(resp.Entry.Content, config) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
func (ifs *IAMFilerStore) SaveIAMConfig(config *iam_pb.S3ApiConfiguration) error { |
|||
entry := &filer_pb.Entry{ |
|||
Name: iamIdentityFile, |
|||
IsDirectory: false, |
|||
Attributes: &filer_pb.FuseAttributes{ |
|||
Mtime: time.Now().Unix(), |
|||
Crtime: time.Now().Unix(), |
|||
FileMode: uint32(0644), |
|||
Collection: "", |
|||
Replication: "", |
|||
}, |
|||
Content: []byte{}, |
|||
} |
|||
err := ifs.saveIAMConfigToEntry(entry, config) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
_, err = filer_pb.LookupEntry(*ifs.client, ifs.getIAMConfigRequest()) |
|||
if err == filer_pb.ErrNotFound { |
|||
err = filer_pb.CreateEntry(*ifs.client, &filer_pb.CreateEntryRequest{ |
|||
Directory: iamConfigPrefix, |
|||
Entry: entry, |
|||
IsFromOtherCluster: false, |
|||
Signatures: nil, |
|||
}) |
|||
} else { |
|||
err = filer_pb.UpdateEntry(*ifs.client, &filer_pb.UpdateEntryRequest{ |
|||
Directory: iamConfigPrefix, |
|||
Entry: entry, |
|||
IsFromOtherCluster: false, |
|||
Signatures: nil, |
|||
}) |
|||
} |
|||
if err != nil { |
|||
return err |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
func (ifs *IAMFilerStore) loadIAMConfigFromBytes(content []byte, config *iam_pb.S3ApiConfiguration) error { |
|||
if err := proto.Unmarshal(content, config); err != nil { |
|||
return err |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
func (ifs *IAMFilerStore) saveIAMConfigToEntry(entry *filer_pb.Entry, config *iam_pb.S3ApiConfiguration) (err error) { |
|||
entry.Content, err = proto.Marshal(config) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
return nil |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue