func ExampleNewReader() { ctx := Example_auth() rc, err := storage.NewReader(ctx, "bucketname", "filename1") if err != nil { log.Fatal(err) } slurp, err := ioutil.ReadAll(rc) rc.Close() if err != nil { log.Fatal(err) } log.Println("file contents:", slurp) }
// readFile reads the named file in Google Cloud Storage. func (d *demo) readFile(fileName string) { io.WriteString(d.w, "\nAbbreviated file content (first line and last 1K):\n") rc, err := storage.NewReader(d.ctx, bucket, fileName) if err != nil { d.errorf("readFile: unable to open file from bucket %q, file %q: %v", bucket, fileName, err) return } defer rc.Close() slurp, err := ioutil.ReadAll(rc) if err != nil { d.errorf("readFile: unable to read data from bucket %q, file %q: %v", bucket, fileName, err) return } fmt.Fprintf(d.w, "%s\n", bytes.SplitN(slurp, []byte("\n"), 2)[0]) if len(slurp) > 1024 { fmt.Fprintf(d.w, "...%s\n", slurp[len(slurp)-1024:]) } else { fmt.Fprintf(d.w, "%s\n", slurp) } }