本文整理汇总了Golang中google/golang.org/cloud/storage.NewReader函数的典型用法代码示例。如果您正苦于以下问题:Golang NewReader函数的具体用法?Golang NewReader怎么用?Golang NewReader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewReader函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: StartupHandler
func StartupHandler(w http.ResponseWriter, r *http.Request) {
if len(Config) == 0 {
c := appengine.NewContext(r)
client := &http.Client{
Transport: &oauth2.Transport{
Source: google.AppEngineTokenSource(c, storage.ScopeReadOnly),
Base: &urlfetch.Transport{
Context: c,
},
},
}
bucket, _ := file.DefaultBucketName(c)
ctx := cloud.NewContext("davine-web", client)
rc, err := storage.NewReader(ctx, bucket, "config.yaml")
if err != nil {
c.Errorf("error reading config: %v", err.Error())
return
}
configFile, err := ioutil.ReadAll(rc)
rc.Close()
if err != nil {
c.Errorf("error reading config: %v", err.Error())
return
}
c.Infof("loaded config file: %v", configFile)
yaml.Unmarshal(configFile, &Config)
c.Infof("loaded config struct: %v", Config)
}
}
示例2: OpenTestFile
// OpenTestFile is like os.Open but for accessing test files.
func OpenTestFile(name string) (io.ReadCloser, error) {
fn := path.Join(cache, name)
f, err := os.Open(fn)
if err == nil {
return f, nil
}
if err != os.ErrNotExist {
return nil, err
}
rc, err := storage.NewReader(context.Background(), bucket, name)
if err != nil {
return nil, err
}
w, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_EXCL, os.ModeTemporary)
if err != nil {
return nil, err
}
pr, pw := io.Pipe()
go io.Copy(io.MultiWriter(pw, w), rc)
return pr, nil
}
示例3: Get
func (a *AppengineStore) Get(name string) (io.ReadCloser, error) {
r, err := storage.NewReader(a.Ctx, a.Cfg.Bucket, name)
if err == storage.ErrObjectNotExist || err == storage.ErrBucketNotExist {
// TODO log an error
return nil, files.ErrNotFound
}
return r, err
}
示例4: processImage
func processImage(bucket, name string) error {
r, err := storage.NewReader(ctx, bucket, name)
if err != nil {
return fmt.Errorf("storage reader: %v", err)
}
img, err := ioutil.ReadAll(r)
r.Close()
if err != nil {
return fmt.Errorf("read image: %v", err)
}
wand := imagick.NewMagickWand()
defer wand.Destroy()
wand.ReadImageBlob(img)
if err := wand.SetImageFormat("WEBP"); err != nil {
return fmt.Errorf("set WEBP format: %v", err)
}
errc := make(chan error, len(sizes))
for suffix, size := range sizes {
go func(wand *imagick.MagickWand, suffix string, x, y uint) {
errc <- func() error {
defer wand.Destroy()
if err := wand.AdaptiveResizeImage(size.x, size.y); err != nil {
return fmt.Errorf("resize: %v", err)
}
target := name
if sep := strings.LastIndex(target, "."); sep >= 0 {
target = target[:sep]
}
target = fmt.Sprintf("%s_%s.webp", target, suffix)
w := storage.NewWriter(ctx, outputBucket, target, nil)
if _, err := w.Write(wand.GetImageBlob()); err != nil {
return fmt.Errorf("new writer: %v", err)
}
if err := w.Close(); err != nil {
return fmt.Errorf("close object writer: %v", err)
}
if _, err := w.Object(); err != nil {
return fmt.Errorf("write op: %v", err)
}
return nil
}()
}(wand.Clone(), suffix, size.x, size.y)
}
for _ = range sizes {
if err := <-errc; err != nil {
return err
}
}
return nil
}
示例5: ImageHandler
func ImageHandler(w http.ResponseWriter, r *http.Request) {
val := bone.GetValue(r, "rows")
re := regexp.MustCompile("\\d*")
rowsStr := re.FindString(val)
rows, err := strconv.Atoi(rowsStr)
if err != nil {
fmt.Fprint(w, "Please pass in number of row")
return
}
if rows > 5000 {
fmt.Fprint(w, "Please pass in a row equal or smaller than 5000")
return
}
cachedImage, ok := cache[rows]
if ok {
w.Header().Set("Content-type", "image/png")
w.Header().Set("Cache-control", "public, max-age=259200")
w.Write(cachedImage.Bytes())
return
}
ctx, err := cloudAuthContext(r)
if err == nil {
rc, err := storage.NewReader(ctx, bucket, rowsStr+".png")
if err == nil {
image, err := ioutil.ReadAll(rc)
rc.Close()
if err == nil {
w.Header().Set("Content-type", "image/png")
w.Header().Set("Cache-control", "public, max-age=259200")
w.Write(image)
return
}
}
}
m := image.NewPaletted(image.Rectangle{Min: image.Point{0, 0}, Max: image.Point{rows, rows}}, bwPalette)
m.Pix[m.PixOffset(rows-1, 0)] = 1
for row := 1; row < rows; row++ {
m.Pix[m.PixOffset(rows-1, row)] = 1
for j := rows - row; j < rows-1; j++ {
mid := m.PixOffset(j, row-1)
left, right := mid-1, mid+1
m.Pix[m.PixOffset(j, row)] = rulesUint8[m.Pix[left]][m.Pix[mid]][m.Pix[right]]
}
}
w.Header().Set("Content-type", "image/png")
w.Header().Set("Cache-control", "public, max-age=259200")
buf := new(bytes.Buffer)
png.Encode(buf, m)
cache[rows] = buf
w.Write(buf.Bytes())
}
示例6: downloadFile
func downloadFile(
wri http.ResponseWriter,
req *http.Request,
) {
var gae = appengine.NewContext(req)
var bucket = util.GetBucketName()
var vars = mux.Vars(req)
log.Infof(gae, "download file %s from bucket %s", vars["key"], bucket)
var cloudCtx = getCloudContext(gae)
rc, err := storage.NewReader(cloudCtx, bucket, vars["key"])
if err != nil {
log.Infof(gae, "unable to open file %q from bucket %q", vars["key"], bucket)
wri.WriteHeader(http.StatusNotFound)
return
}
defer rc.Close()
// Return an signed URL from which the client can download a file
accessIdFile := os.Getenv("GOOGLE_ACCESS_ID")
if accessIdFile == "" {
log.Errorf(gae, "Missing GOOGLE_ACCESS_ID from app.yaml")
wri.WriteHeader(http.StatusInternalServerError)
return
}
accessId, err := ioutil.ReadFile(accessIdFile)
if err != nil {
log.Errorf(gae, "Cannot read service account access id from %s", accessIdFile)
wri.WriteHeader(http.StatusInternalServerError)
}
privateKeyFile := os.Getenv("GOOGLE_PRIVATE_KEY")
if privateKeyFile == "" {
log.Errorf(gae, "Missing GOOGLE_PRIVATE_KEY from app.yaml")
wri.WriteHeader(http.StatusInternalServerError)
return
}
log.Infof(gae, "Private key file %s", privateKeyFile)
privateKey, err := ioutil.ReadFile(privateKeyFile)
if err != nil {
log.Errorf(gae, "Cannot read service account private key from %s", privateKeyFile)
wri.WriteHeader(http.StatusInternalServerError)
return
}
var opts = storage.SignedURLOptions{
GoogleAccessID: string(accessId),
PrivateKey: privateKey,
Method: "GET",
Expires: time.Now().UTC().Add(300 * time.Second),
}
signedURL, err := storage.SignedURL(bucket, vars["key"], &opts)
if err != nil {
log.Errorf(gae, "Unable to generate a signedURL: %s", err)
wri.WriteHeader(http.StatusInternalServerError)
}
http.Redirect(wri, req, signedURL, http.StatusTemporaryRedirect)
}
示例7: OpenReader
func (bs *GCSBlobStore) OpenReader(blobpath string) (io.ReadCloser, error) {
ctx := bs.newAuthedContext(context.TODO())
rc, err := storage.NewReader(ctx, bs.bucketName, blobpath)
if err != nil {
if err == storage.ErrObjectNotExist {
return nil, blobstore.ENOENT
}
return nil, err
}
return rc, nil
}
示例8: handleGet
func handleGet(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
cctx := getCloudContext(ctx)
rdr, err := storage.NewReader(cctx, bucketName, "example.txt")
if err != nil {
http.Error(res, err.Error(), 500)
return
}
defer rdr.Close()
io.Copy(res, rdr)
}
示例9: Read
func (o *object) Read(p []byte) (n int, err error) {
if o.w != nil {
return 0, errors.New("object is already opened for writing")
}
if o.r == nil {
rc, err := s.NewReader(o.ctx, o.b, o.n)
if err != nil {
return 0, err
}
o.r = &rc
}
return (*o.r).Read(p)
}
示例10: PullDiff
func (s *googleReplicaAPIServer) PullDiff(request *pfs.PullDiffRequest, pullDiffServer pfs.ReplicaAPI_PullDiffServer) (retErr error) {
reader, err := storage.NewReader(s.ctx, s.bucket, fmt.Sprintf("%s/%s/%d", request.Commit.Repo.Name, request.Commit.Id, request.Shard))
if err != nil {
return err
}
defer func() {
if err := reader.Close(); err != nil && retErr == nil {
retErr = err
}
}()
writer := protostream.NewStreamingBytesWriter(pullDiffServer)
_, err = io.Copy(writer, reader)
return err
}
示例11: ExampleNewReader
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)
}
示例12: handleGet
func handleGet(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
cctx := getCloudContext(ctx)
filename := req.FormValue("f")
rdr, err := storage.NewReader(cctx, bucket, prefix+filename)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
defer rdr.Close()
io.Copy(res, rdr)
}
示例13: handleDownload
func handleDownload(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
cctx := getCloudContext(ctx)
fileName := req.FormValue("fname")
rdr, err := storage.NewReader(cctx, bucketName, fileName)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
defer rdr.Close()
res.Header().Set("Content-Disposition", `attachment; filename="`+fileName+`"`)
io.Copy(res, rdr)
}
示例14: fromStorage
func fromStorage(ctx context.Context, bucket, name string) (io.ReadCloser, error) {
rc, err := storage.NewReader(CloudContext(ctx), bucket, name)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
var crc = struct {
io.Reader
io.Closer
}{
io.TeeReader(rc, buf),
cachingCloser{ctx: ctx, rc: rc, buf: buf, key: key(bucket, name)},
}
return crc, nil
}
示例15: Example_readObjects
func Example_readObjects() {
// see the auth example how to initiate a context.
ctx := cloud.NewContext("project-id", &http.Client{Transport: nil})
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)
}