github.com/cs3org/reva/v2@v2.27.7/examples/sdk/sdk.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package main 20 21 import ( 22 "fmt" 23 "log" 24 25 "github.com/cs3org/reva/v2/pkg/sdk" 26 "github.com/cs3org/reva/v2/pkg/sdk/action" 27 ) 28 29 func runActions(session *sdk.Session) { 30 // Try creating a directory 31 { 32 act := action.MustNewFileOperationsAction(session) 33 if err := act.MakePath("/home/subdir/subsub"); err == nil { 34 log.Println("Created path /home/subdir/subsub") 35 } else { 36 log.Println("Could not create path /home/subdir/subsub") 37 } 38 fmt.Println() 39 } 40 41 // Try deleting a directory 42 { 43 act := action.MustNewFileOperationsAction(session) 44 if err := act.Remove("/home/subdir/subsub"); err == nil { 45 log.Println("Removed path /home/subdir/subsub") 46 } else { 47 log.Println("Could not remove path /home/subdir/subsub") 48 } 49 fmt.Println() 50 } 51 52 // Try uploading 53 { 54 act := action.MustNewUploadAction(session) 55 act.EnableTUS = true 56 if info, err := act.UploadBytes([]byte("HELLO WORLD!\n"), "/home/subdir/tests.txt"); err == nil { 57 log.Printf("Uploaded file: %s [%db] -- %s", info.Path, info.Size, info.Type) 58 } else { 59 log.Printf("Can't upload file: %v", err) 60 } 61 fmt.Println() 62 } 63 64 // Try moving 65 { 66 act := action.MustNewFileOperationsAction(session) 67 if err := act.MoveTo("/home/subdir/tests.txt", "/home/sub2"); err == nil { 68 log.Println("Moved tests.txt around") 69 } else { 70 log.Println("Could not move tests.txt around") 71 } 72 fmt.Println() 73 } 74 75 // Try listing and downloading 76 { 77 act := action.MustNewEnumFilesAction(session) 78 if files, err := act.ListFiles("/home", true); err == nil { 79 for _, info := range files { 80 log.Printf("%s [%db] -- %s", info.Path, info.Size, info.Type) 81 82 // Download the file 83 actDl := action.MustNewDownloadAction(session) 84 if data, err := actDl.Download(info); err == nil { 85 log.Printf("Downloaded %d bytes for '%v'", len(data), info.Path) 86 } else { 87 log.Printf("Unable to download data for '%v': %v", info.Path, err) 88 } 89 90 log.Println("---") 91 } 92 } else { 93 log.Printf("Can't list files: %v", err) 94 } 95 fmt.Println() 96 } 97 98 // Try accessing some files and directories 99 { 100 act := action.MustNewFileOperationsAction(session) 101 if act.FileExists("/home/blargh.txt") { 102 log.Println("File '/home/blargh.txt' found") 103 } else { 104 log.Println("File '/home/blargh.txt' NOT found") 105 } 106 107 if act.DirExists("/home") { 108 log.Println("Directory '/home' found") 109 } else { 110 log.Println("Directory '/home' NOT found") 111 } 112 fmt.Println() 113 } 114 } 115 116 func main() { 117 session := sdk.MustNewSession() 118 if err := session.Initiate("sciencemesh-test.uni-muenster.de:9600", false); err != nil { 119 log.Fatalf("Can't initiate Reva session: %v", err) 120 } 121 122 if methods, err := session.GetLoginMethods(); err == nil { 123 fmt.Println("Supported login methods:") 124 for _, m := range methods { 125 fmt.Printf("* %v\n", m) 126 } 127 fmt.Println() 128 } else { 129 log.Fatalf("Can't list login methods: %v", err) 130 } 131 132 if err := session.BasicLogin("daniel", "danielpass"); err == nil { 133 log.Printf("Successfully logged into Reva (token=%v)", session.Token()) 134 fmt.Println() 135 runActions(session) 136 } else { 137 log.Fatalf("Can't log in to Reva: %v", err) 138 } 139 }