-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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