From 0174cf1e38c089e62eddd45cf2767579a88d9b9d Mon Sep 17 00:00:00 2001 From: JinnyYi Date: Tue, 7 Sep 2021 12:46:57 +0800 Subject: [PATCH 1/2] Add write example --- README.md | 3 ++ write.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 write.go diff --git a/README.md b/README.md index 668752d..5dc5770 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/write.go b/write.go new file mode 100644 index 0000000..570d9da --- /dev/null +++ b/write.go @@ -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) +} From e93e81039b9344f13defc7e79fc0af6200783642 Mon Sep 17 00:00:00 2001 From: JinnyYi Date: Tue, 7 Sep 2021 12:57:02 +0800 Subject: [PATCH 2/2] Add format in make build --- Makefile | 2 +- multipart.go | 2 +- read.go | 3 +-- write.go | 8 ++++---- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index b5ff085..a8c3df9 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ format: vet: go vet ./... -build: tidy check +build: tidy format check go build ./... test: diff --git a/multipart.go b/multipart.go index f25bcac..a0f3740 100644 --- a/multipart.go +++ b/multipart.go @@ -179,4 +179,4 @@ func CancelMultipart(store types.Storager, path string) { } log.Printf("cancel multipart: %v", path) -} \ No newline at end of file +} diff --git a/read.go b/read.go index a347b68..20f6898 100644 --- a/read.go +++ b/read.go @@ -82,7 +82,6 @@ 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. // @@ -90,7 +89,7 @@ func ReadWithSignedURL(store types.Storager, path string, expire time.Duration) // `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) } diff --git a/write.go b/write.go index 570d9da..bddd9a1 100644 --- a/write.go +++ b/write.go @@ -13,7 +13,7 @@ import ( "github.com/beyondstorage/go-storage/v4/types" ) -func WriteData (store types.Storager, path string) { +func WriteData(store types.Storager, path string) { // content to write size := rand.Int63n(4 * 1024 * 1024) r := io.LimitReader(randbytes.NewRand(), size) @@ -34,7 +34,7 @@ func WriteData (store types.Storager, path string) { log.Printf("write size: %d", n) } -func WriteWithCallback (store types.Storager, path string) { +func WriteWithCallback(store types.Storager, path string) { // content to write size := rand.Int63n(4 * 1024 * 1024) r := io.LimitReader(randbytes.NewRand(), size) @@ -55,7 +55,7 @@ func WriteWithCallback (store types.Storager, path string) { log.Printf("write size: %d", n) } -func WriteWithSignedURL (store types.Storager, path string, expire time.Duration) { +func WriteWithSignedURL(store types.Storager, path string, expire time.Duration) { signer, ok := store.(types.StorageHTTPSigner) if !ok { log.Fatalf("StorageHTTPSigner unimplemented") @@ -78,7 +78,7 @@ func WriteWithSignedURL (store types.Storager, path string, expire time.Duration // // `err` is the error during this operation. req, err := signer.QuerySignHTTPWrite(path, size, expire) - if err !=nil { + if err != nil { log.Fatalf("write %v: %v", path, err) }