Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from deta/drive-readme
Browse files Browse the repository at this point in the history
Add drive examples to readme
  • Loading branch information
aavshr authored Jul 12, 2021
2 parents b62ffcf + f5f4f7c commit b57a22b
Showing 1 changed file with 80 additions and 2 deletions.
82 changes: 80 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ To get the latest SDK repository change use `@latest`.
go get github.com/aws/deta-go@latest
```

## Example
## Examples

### Base

```go
package main
Expand Down Expand Up @@ -72,4 +74,80 @@ func main(){
}
```

More examples and complete documentation on https://docs.deta.sh/docs/base/sdk
More examples and complete documentation on https://docs.deta.sh/docs/base/sdk

### Drive
```go
package main

import (
"fmt"
"github.com/deta/deta-go"
)

func main() {
// initialize with project key
// returns ErrBadProjectKey if project key is invalid
d, err := deta.New("project_key")
if err != nil {
fmt.Println("failed to init new Deta instance:", err)
return
}

// initialize with drive name
// returns ErrBadDriveName if drive name is invalid
drive, err := d.NewDrive("drive_name")
if err != nil {
fmt.Println("failed to init new Drive instance:", err)
return
}

// PUT
// reading from a local file
file, err := os.Open("./art.svg")
defer file.Close()

name, err = drive.Put(&deta.PutInput{
Name: "art.svg",
Body: bufio.NewReader(file),
ContentType: "image/svg+xml",
})
if err != nil {
fmt.Println("Failed to put file:", err)
return
}
fmt.Println("Successfully put file with name:", name)

// GET
name := "hello.txt"
f, err := drive.Get(name)
if err != nil {
fmt.Println("Failed to get file with name:", name)
return
}
defer f.Close()

c, err := ioutil.ReadAll(f)
if err != nil {
fmt.Println("Failed read file content with err:", err)
return
}
fmt.Println("file content:", string(c))

// DELETE
name, err := d.Delete("hello.txt")
if err != nil {
fmt.Println("Failed to delete file with name:", name)
return
}
fmt.Println("Successfully deleted file with name:", name)

// LIST
lr, err := drive.List(1000, "", "")
if err != nil {
fmt.Println("Failed to list names from drive with err:", err)
}
fmt.Println("names:", lr.Names)
}
```
More examples and complete documentation on https://docs.deta.sh/docs/drive/sdk/

0 comments on commit b57a22b

Please sign in to comment.