Skip to content

Commit

Permalink
Add write example (#22)
Browse files Browse the repository at this point in the history
* Add write example

* Add format in make build
  • Loading branch information
JinnyYi authored Sep 7, 2021
1 parent f83d95e commit ee7bb19
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ format:
vet:
go vet ./...

build: tidy check
build: tidy format check
go build ./...

test:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
- [Read a range of a file](read.go)
- [Read a file with callback](read.go)
- [Read a file using signed URL](read.go)
- [Write a file](write.go)
- [Write a file with callback](write.go)
- [Write a file using signed URL](write.go)
- [Append to a new file](append.go)
- [Append to an existing file](append.go)
- [Multipart upload](multipart.go)
Expand Down
2 changes: 1 addition & 1 deletion multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ func CancelMultipart(store types.Storager, path string) {
}

log.Printf("cancel multipart: %v", path)
}
}
3 changes: 1 addition & 2 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,14 @@ func ReadWithSignedURL(store types.Storager, path string, expire time.Duration)
}

// QuerySignHTTPRead needs at least two arguments.
//
// `path` is the path of object.
// `expire` provides the time period, with type time.Duration, for which the generated req.URL is valid.
//
// QuerySignHTTPRead will return two values.
// `req` is the generated `*http.Request`, `req.URL` specifies the URL to access with signature in the query string. And `req.Header` specifies the HTTP headers included in the signature.
// `err` is the error during this operation.
req, err := signer.QuerySignHTTPRead(path, expire)
if err !=nil {
if err != nil {
log.Fatalf("read %v: %v", path, err)
}

Expand Down
95 changes: 95 additions & 0 deletions write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package example

import (
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"time"

"github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/pkg/randbytes"
"github.com/beyondstorage/go-storage/v4/types"
)

func WriteData(store types.Storager, path string) {
// content to write
size := rand.Int63n(4 * 1024 * 1024)
r := io.LimitReader(randbytes.NewRand(), size)

// Write needs at least three arguments.
// `path` is the path of object.
// `r` is io.Reader instance for reading the data for uploading.
// `size` is the length, in bytes, of the data for uploading.
//
// Write will return two values.
// `n` is the size of write operation.
// `err` is the error during this operation.
n, err := store.Write(path, r, size)
if err != nil {
log.Fatalf("write %v: %v", path, err)
}

log.Printf("write size: %d", n)
}

func WriteWithCallback(store types.Storager, path string) {
// content to write
size := rand.Int63n(4 * 1024 * 1024)
r := io.LimitReader(randbytes.NewRand(), size)

cur := int64(0)
fn := func(bs []byte) {
cur += int64(len(bs))
log.Printf("write %d bytes already", cur)
}

// If IoCallback is specified, the storage will call it in every I/O operation.
// User could use this feature to implement progress bar.
n, err := store.Write(path, r, size, pairs.WithIoCallback(fn))
if err != nil {
log.Fatalf("write %v: %v", path, err)
}

log.Printf("write size: %d", n)
}

func WriteWithSignedURL(store types.Storager, path string, expire time.Duration) {
signer, ok := store.(types.StorageHTTPSigner)
if !ok {
log.Fatalf("StorageHTTPSigner unimplemented")
}

size := rand.Int63n(4 * 1024 * 1024)
r := io.LimitReader(randbytes.NewRand(), size)

// QuerySignHTTPWrite needs at least three arguments.
// `path` is the path of object.
// `size` is the length, in bytes, of the data for uploading.
// `expire` provides the time period, with type time.Duration, for which the generated req.URL is valid.
//
// QuerySignHTTPWrite will return two values.
//
// `req` is the generated `*http.Request`:
// `req.URL` specifies the URL to access with signature in the query string.
// `req.Header` specifies the HTTP headers included in the signature.
// `req.ContentLength` records the length of the associated content, the value equals to `size`.
//
// `err` is the error during this operation.
req, err := signer.QuerySignHTTPWrite(path, size, expire)
if err != nil {
log.Fatalf("write %v: %v", path, err)
}

// Set request body.
req.Body = ioutil.NopCloser(r)

client := http.Client{}
_, err = client.Do(req)
if err != nil {
log.Fatalf("send HTTP request for writing %v: %v", path, err)
}

log.Printf("write size: %d", size)
}

0 comments on commit ee7bb19

Please sign in to comment.