Adding sharepoint-uploader
This commit is contained in:
parent
375a503900
commit
a8ac706731
32
sharepoint-file-uploader/README.md
Normal file
32
sharepoint-file-uploader/README.md
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# Sharepoint File Uploader
|
||||||
|
Upload a file to sharepoint
|
||||||
|
|
||||||
|
## Compile from source
|
||||||
|
Install Golang and run `go build`. You can use the generated binary `sharepoint-upload`.
|
||||||
|
|
||||||
|
# Installation
|
||||||
|
## Binary (Linux)
|
||||||
|
Copy the binary `sharepoint-upload` and execute it.
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
|
||||||
|
Set `SP_PASSWORD` environment variable.
|
||||||
|
```
|
||||||
|
sharepoint-upload -f filename
|
||||||
|
```
|
||||||
|
|
||||||
|
Use `-h` to see more available options (like setting the directory).
|
||||||
|
```
|
||||||
|
$ ./sharepoint-upload -h
|
||||||
|
Usage of ./sharepoint-upload:
|
||||||
|
-d string
|
||||||
|
The directory on Sharepoint (default "Reports")
|
||||||
|
-f string
|
||||||
|
The file to upload
|
||||||
|
-overwrite
|
||||||
|
Specify if existing files should be overwritten
|
||||||
|
-s string
|
||||||
|
The Sharepoint site url (default "https://michuit.sharepoint.com/sites/csi")
|
||||||
|
-u string
|
||||||
|
The user for Sharepoint (default "michael")
|
||||||
|
```
|
5
sharepoint-file-uploader/go.mod
Normal file
5
sharepoint-file-uploader/go.mod
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module sharepoint-upload
|
||||||
|
|
||||||
|
require github.com/koltyakov/gosip v0.0.0-20190626081228-fb7b13816974
|
||||||
|
|
||||||
|
go 1.13
|
20
sharepoint-file-uploader/go.sum
Normal file
20
sharepoint-file-uploader/go.sum
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
github.com/Azure/go-ntlmssp v0.0.0-20180810175552-4a21cbd618b4/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
|
||||||
|
github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ=
|
||||||
|
github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI=
|
||||||
|
github.com/koltyakov/gosip v0.0.0-20190626081228-fb7b13816974 h1:U7Mpe+vBltofcXis/RXOoAaxyXYZqKRIKsHh5fMPpCs=
|
||||||
|
github.com/koltyakov/gosip v0.0.0-20190626081228-fb7b13816974/go.mod h1:k7aEmq6MiyPg0iwSj9SD32jnYYioAwugFR7LQQe0sCA=
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20190530182044-ad28b68e88f1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190530184349-ce1a3806b557/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
88
sharepoint-file-uploader/main.go
Normal file
88
sharepoint-file-uploader/main.go
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"github.com/koltyakov/gosip"
|
||||||
|
"github.com/koltyakov/gosip/auth/saml"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SharepointResponse struct {
|
||||||
|
Error struct {
|
||||||
|
Message struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
} `json:"message"`
|
||||||
|
} `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SharepointClient struct {
|
||||||
|
*gosip.SPClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func save(sp *gosip.SPClient, name, directory, content string, overwrite bool) error {
|
||||||
|
apiEndpoint := fmt.Sprintf("%v/_api/web/GetFolderByServerRelativeUrl('%v')/Files/add(url='%v',overwrite=%v)", sp.AuthCnfg.GetSiteURL(), directory, name, overwrite)
|
||||||
|
req, err := http.NewRequest("POST", apiEndpoint, bytes.NewBuffer([]byte(content)))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to create a request: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Accept", "application/json;odata=verbose")
|
||||||
|
|
||||||
|
resp, err := sp.Execute(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to read a response: %v\n", err)
|
||||||
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return fmt.Errorf("%s", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
dir := flag.String("d", "Reports", "The directory on Sharepoint")
|
||||||
|
fileName := flag.String("f", "", "The file to upload")
|
||||||
|
userName := flag.String("u", "michael", "The user for Sharepoint")
|
||||||
|
overwrite := flag.Bool("overwrite", false, "Specify if existing files should be overwritten")
|
||||||
|
siteURL := flag.String("s", "https://michuit.sharepoint.com/sites/csi", "The Sharepoint site url")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if *fileName == "" {
|
||||||
|
log.Fatal("File must be specified with -f")
|
||||||
|
}
|
||||||
|
|
||||||
|
fileBytes, err := ioutil.ReadFile(*fileName)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
password, ok := os.LookupEnv("SP_PASSWORD")
|
||||||
|
if !ok {
|
||||||
|
log.Fatal("SP_PASSWORD environment variable must be set!")
|
||||||
|
}
|
||||||
|
|
||||||
|
auth := &saml.AuthCnfg{
|
||||||
|
SiteURL: *siteURL,
|
||||||
|
Username: *userName,
|
||||||
|
Password: password,
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &gosip.SPClient{
|
||||||
|
AuthCnfg: auth,
|
||||||
|
}
|
||||||
|
err = save(client, path.Base(*fileName), *dir, string(fileBytes), *overwrite)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user