• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang storage.NewReader函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中google/golang.org/cloud/storage.NewReader函数的典型用法代码示例。如果您正苦于以下问题:Golang NewReader函数的具体用法?Golang NewReader怎么用?Golang NewReader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewReader函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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


示例16: handleGet

func handleGet(res http.ResponseWriter, req *http.Request) {
	cctx, err := getCloudContext(req)
	if err != nil {
		http.Error(res, "ERROR GETTING CCTX: "+err.Error(), 500)
		return
	}

	rdr, err := storage.NewReader(cctx, gcsBucket, "example999.txt")
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
	defer rdr.Close()

	io.Copy(res, rdr)
}
开发者ID:RaviTezu,项目名称:GolangTraining,代码行数:16,代码来源:storage.go


示例17: ReadFile

func ReadFile(c appengine.Context, gsPath string) (data []byte, err error) {
	ctx, err := getGcsContext(c)
	if err != nil {
		return nil, err
	}

	filename := strings.SplitN(gsPath, "/", 4)[3]

	rc, err := storage.NewReader(ctx, gcsBucket, filename)
	if err != nil {
		return nil, err
	}

	data, err = ioutil.ReadAll(rc)
	rc.Close()
	return
}
开发者ID:jbeshir,项目名称:dreampics_frontend,代码行数:17,代码来源:file.go


示例18: handleGet

func handleGet(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
	cctx, err := getCloudContext(req)
	if err != nil {
		http.Error(res, "ERROR GETTING CCTX: "+err.Error(), 500)
		return
	}

	fileName := "test.txt"

	rdr, err := storage.NewReader(cctx, gcsBucket, fileName)
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
	defer rdr.Close()

	io.Copy(res, rdr)
}
开发者ID:sgkadle,项目名称:AdvWebProg,代码行数:18,代码来源:storage.go


示例19: Get

func (conn GoogleCloudStorageConnection) Get(name string) (string, error) {
	log.Debugf("GoogleCloudStorageConnection Get. name: %s", name)
	cacheFilepath, err := conn.getCacheFilepath(name)
	if err != nil {
		log.Errorf("Failed to getCacheFilepath in Get: %s", err)
		return cacheFilepath, err
	}
	cacheDirectory := filepath.Dir(cacheFilepath)
	err = os.MkdirAll(cacheDirectory, 0777)
	if err != nil {
		log.Errorf("Couldn't create cache directory for cacheFilepath %s: %s", cacheFilepath, err)
		return cacheFilepath, err
	}
	if _, err = os.Stat(cacheDirectory); err != nil {
		log.Errorf("Cache directory %s doesn't exist!", cacheDirectory)
		return cacheFilepath, err
	}
	w, err := os.Create(cacheFilepath)
	if err != nil {
		log.Errorf("Couldn't create cache file for cacheFilepath %s: %s", cacheFilepath, err)
		return cacheFilepath, err
	}
	defer w.Close()
	wBuffered := bufio.NewWriter(w)
	defer wBuffered.Flush()
	r, err := storage.NewReader(conn.Context, conn.BucketName, name)
	if err != nil {
		log.Errorf("Failed to download name %s during initialization: %s", name, err)
		defer os.Remove(cacheFilepath)
		return cacheFilepath, err
	}
	defer r.Close()
	_, err = io.Copy(wBuffered, r)
	time.Sleep(100 * time.Millisecond)
	if err != nil {
		log.Errorf("Failed to download name %s during download: %s", name, err)
		defer os.Remove(cacheFilepath)
		return cacheFilepath, err
	}
	return cacheFilepath, nil
}
开发者ID:asimihsan,项目名称:arqinator,代码行数:41,代码来源:googlecloudstorage.go


示例20: readFile

// 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, d.bucket, fileName)
	if err != nil {
		d.errorf("readFile: unable to open file from bucket %q, file %q: %v", d.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", d.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)
	}
}
开发者ID:qwo,项目名称:abelana-gcp,代码行数:23,代码来源:app.go



注:本文中的google/golang.org/cloud/storage.NewReader函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang storage.NewWriter函数代码示例发布时间:2022-05-28
下一篇:
Golang storage.NewClient函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap