diff --git a/README.md b/README.md index 0d5ad65..668752d 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ - [Read a file](read.go) - [Read a range of a file](read.go) - [Read a file with callback](read.go) +- [Read a file using signed URL](read.go) - [Append to a new file](append.go) - [Append to an existing file](append.go) - [Multipart upload](multipart.go) diff --git a/read.go b/read.go index fd8fa95..a347b68 100644 --- a/read.go +++ b/read.go @@ -2,9 +2,13 @@ package example import ( "bytes" + "io/ioutil" + "log" + "net/http" + "time" + "github.com/beyondstorage/go-storage/v4/pairs" "github.com/beyondstorage/go-storage/v4/types" - "log" ) func ReadWhole(store types.Storager, path string) { @@ -70,3 +74,44 @@ func ReadWithCallback(store types.Storager, path string) { log.Printf("read size: %d", n) log.Printf("read content: %s", buf.Bytes()) } + +func ReadWithSignedURL(store types.Storager, path string, expire time.Duration) { + signer, ok := store.(types.StorageHTTPSigner) + if !ok { + log.Fatalf("StorageHTTPSigner unimplemented") + } + + // 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 { + log.Fatalf("read %v: %v", path, err) + } + + client := http.Client{} + resp, err := client.Do(req) + if err != nil { + log.Fatalf("send HTTP request for reading %v: %v", path, err) + } + + defer func() { + err = resp.Body.Close() + if err != nil { + log.Fatalf("close HTTP response body for reading %v: %v", path, err) + } + }() + + buf, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalf("read from HTTP response body for reading %v: %v", path, err) + } + + log.Printf("read size: %d", resp.ContentLength) + log.Printf("read content: %s", buf) +}