當前位置: 首頁>>代碼示例>>Golang>>正文


Golang storage.NewReader函數代碼示例

本文整理匯總了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)
    }
}
開發者ID:vishnuvr,項目名稱:davine,代碼行數:30,代碼來源:routes.go

示例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
}
開發者ID:coduno,項目名稱:compute,代碼行數:28,代碼來源:runner.go

示例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
}
開發者ID:orian,項目名稱:files,代碼行數:8,代碼來源:appengine.go

示例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
}
開發者ID:qwo,項目名稱:abelana-gcp,代碼行數:57,代碼來源:imagemagick.go

示例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())
}
開發者ID:matthewdu,項目名稱:rule110-go,代碼行數:55,代碼來源:rule110.go

示例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)
}
開發者ID:XiaonuoGantan,項目名稱:gaefile,代碼行數:54,代碼來源:upload.go

示例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
}
開發者ID:hajimehoshi,項目名稱:otaru,代碼行數:11,代碼來源:gcsblobstore.go

示例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)
}
開發者ID:kaveenherath,項目名稱:SummerBootCamp,代碼行數:12,代碼來源:storage.go

示例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)
}
開發者ID:pbochis,項目名稱:api,代碼行數:13,代碼來源:google.go

示例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
}
開發者ID:klucar,項目名稱:pachyderm,代碼行數:14,代碼來源:replica_server.go

示例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)
}
開發者ID:rkazak,項目名稱:distribution,代碼行數:15,代碼來源:example_test.go

示例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)
}
開發者ID:CodingDance,項目名稱:GolangTraining,代碼行數:15,代碼來源:storage.go

示例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)
}
開發者ID:kaveenherath,項目名稱:SummerBootCamp,代碼行數:15,代碼來源:storage.go

示例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
}
開發者ID:flowlo,項目名稱:coduno-api,代碼行數:15,代碼來源:storage.go

示例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)
}
開發者ID:qwo,項目名稱:abelana-gcp,代碼行數:16,代碼來源:example_test.go


注:本文中的google/golang.org/cloud/storage.NewReader函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。