mirror of https://github.com/casbin/casnode.git
Merge 81b13fd221
into 28c24c51f7
This commit is contained in:
commit
cc1173d958
|
@ -15,16 +15,63 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
_ "github.com/rclone/rclone/backend/all"
|
||||
"github.com/rclone/rclone/fs"
|
||||
"github.com/rclone/rclone/fs/config"
|
||||
"github.com/rclone/rclone/fs/config/configfile"
|
||||
"github.com/rclone/rclone/fs/object"
|
||||
"github.com/rclone/rclone/fs/rc"
|
||||
"time"
|
||||
)
|
||||
|
||||
func getFs(bucketName string) (fs.Fs, error) {
|
||||
f, err := fs.NewFs(context.Background(), bucketName)
|
||||
type ConnectConfig struct {
|
||||
AccessKeyId string `json:"access_key_id"`
|
||||
SecretAccessKey string `json:"secret_access_key"`
|
||||
TypeName string `json:"typeName"`
|
||||
Name string `json:"name"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
func CreateConnection(con ConnectConfig) error {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
keyValues := rc.Params{
|
||||
"access_key_id": con.AccessKeyId,
|
||||
"secret_access_key": con.SecretAccessKey,
|
||||
"token": con.Token,
|
||||
"drive_id": "F7F303BA8FDA4248",
|
||||
"drive_type": "personal",
|
||||
}
|
||||
|
||||
_, err := config.CreateRemote(ctx, con.Name, con.TypeName, keyValues, config.UpdateRemoteOpt{NonInteractive: true})
|
||||
|
||||
config.SaveConfig()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func ConfigLoad() []string {
|
||||
fmt.Println(config.GetConfigPath())
|
||||
|
||||
configfile.Install()
|
||||
sections := config.Data().GetSectionList()
|
||||
fmt.Println(config.Data().GetKeyList(sections[0]))
|
||||
|
||||
return sections
|
||||
}
|
||||
|
||||
func getFs(con ConnectConfig) (fs.Fs, error) {
|
||||
err := CreateConnection(con)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//ConfigLoad(con)
|
||||
f, err := fs.NewFs(context.Background(), con.Name+":/")
|
||||
fmt.Println(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -32,12 +79,12 @@ func getFs(bucketName string) (fs.Fs, error) {
|
|||
return f, nil
|
||||
}
|
||||
|
||||
func ListObjects2(bucketName string, prefix string) ([]fs.DirEntry, error) {
|
||||
if bucketName == "" {
|
||||
func ListObjects2(con ConnectConfig, prefix string) ([]fs.DirEntry, error) {
|
||||
if con.Name == "" {
|
||||
return nil, fmt.Errorf("bucket name is empty")
|
||||
}
|
||||
|
||||
f, err := getFs(bucketName)
|
||||
f, err := getFs(con)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -50,29 +97,35 @@ func ListObjects2(bucketName string, prefix string) ([]fs.DirEntry, error) {
|
|||
return entries, nil
|
||||
}
|
||||
|
||||
func PutObject2(bucketName string, key string, in io.Reader) error {
|
||||
f, err := getFs(bucketName)
|
||||
func PutObject2(con ConnectConfig, addPath string, in *bytes.Buffer) error {
|
||||
f, err := getFs(con)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Use Rcat to put an object to the remote
|
||||
//_, err = operations.Rcat(context.Background(), f, key, nil, nil, nil)
|
||||
print(f)
|
||||
dstObj := object.NewStaticObjectInfo(addPath, time.Now(), int64(in.Len()), true, nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = f.Put(context.Background(), in, dstObj)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func DeleteObject2(bucketName string, key string) error {
|
||||
f, err := getFs(bucketName)
|
||||
// DeleteObject2 support delete file or dir
|
||||
func DeleteObject2(con ConnectConfig, delPath string) error {
|
||||
f, err := getFs(con)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
remoteObj, err := f.NewObject(context.Background(), key)
|
||||
remoteObj, err := f.NewObject(context.Background(), delPath)
|
||||
if err != nil {
|
||||
return err
|
||||
// dir: only can delete empty dir
|
||||
return f.Rmdir(context.Background(), delPath)
|
||||
}
|
||||
|
||||
//object
|
||||
return remoteObj.Remove(context.Background())
|
||||
}
|
||||
|
|
|
@ -17,11 +17,69 @@
|
|||
|
||||
package storage
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var conConfig = ConnectConfig{
|
||||
Name: "",
|
||||
TypeName: "onedrive",
|
||||
AccessKeyId: "",
|
||||
SecretAccessKey: "",
|
||||
Token: "",
|
||||
}
|
||||
|
||||
func TestCreateConnection(t *testing.T) {
|
||||
CreateConnection(conConfig)
|
||||
}
|
||||
func TestStorage(t *testing.T) {
|
||||
_, err := ListObjects("casibase", "")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFs(t *testing.T) {
|
||||
b, err := getFs(conConfig)
|
||||
fmt.Println(b)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListObjects2(t *testing.T) {
|
||||
b, err := ListObjects2(conConfig, "/")
|
||||
fmt.Println(b)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigLoad(t *testing.T) {
|
||||
ConfigLoad()
|
||||
}
|
||||
|
||||
func TestDeleteObject2(t *testing.T) {
|
||||
fmt.Println(DeleteObject2(conConfig, "/test.txt"))
|
||||
}
|
||||
|
||||
func TestPutObject2(t *testing.T) {
|
||||
localFile, err := os.Open("D:\\1.txt")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
defer localFile.Close()
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
|
||||
_, err = io.Copy(buffer, localFile)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
fmt.Println(PutObject2(conConfig, "/test6666.txt", buffer))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue