Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add write example #22

Merged
merged 2 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like our code not formatted.

Maybe we need to add format in make build: https://github.com/beyondstorage/go-storage-example/blob/master/Makefile#L22

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)
}