本文整理汇总了Golang中google/golang.org/cloud/storage.NewWriter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewWriter函数的具体用法?Golang NewWriter怎么用?Golang NewWriter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewWriter函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: handlePut
func handlePut(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
hc := &http.Client{
Transport: &oauth2.Transport{
Source: google.AppEngineTokenSource(ctx, storage.ScopeFullControl),
Base: &urlfetch.Transport{Context: ctx},
},
}
cctx := cloud.NewContext(appengine.AppID(ctx), hc)
rdr, hdr, err := req.FormFile("form-upload-file")
if err != nil {
http.Error(res, "ERROR RECEIVING FILE: "+err.Error(), 500)
return
}
writer := storage.NewWriter(cctx, bucketName, hdr.Filename)
io.Copy(writer, rdr)
err = writer.Close()
if err != nil {
http.Error(res, "ERROR WRITING TO BUCKET: "+err.Error(), 500)
return
}
}
开发者ID:kaveenherath,项目名称:SummerBootCamp,代码行数:27,代码来源:storage.go
示例2: PutContent
// PutContent stores the []byte content at a location designated by "path".
// This should primarily be used for small objects.
func (d *driver) PutContent(context ctx.Context, path string, contents []byte) error {
return retry(func() error {
wc := storage.NewWriter(d.context(context), d.bucket, d.pathToKey(path))
wc.ContentType = "application/octet-stream"
return putContentsClose(wc, contents)
})
}
开发者ID:ncdc,项目名称:origin,代码行数:9,代码来源:gcs.go
示例3: handlePut
func handlePut(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
cctx := getCloudContext(ctx)
if req.Method != "POST" {
http.Error(res, "Must send a file", http.StatusMethodNotAllowed)
return
}
file, hdr, err := req.FormFile("f")
if err != nil {
http.Error(res, err.Error(), 500)
return
}
filename := hdr.Filename
writer := storage.NewWriter(cctx, bucket, prefix+filename)
io.Copy(writer, file)
err = writer.Close()
if err != nil {
http.Error(res, "ERROR WRITING TO BUCKET: "+err.Error(), 500)
return
}
http.Redirect(res, req, "/", http.StatusSeeOther)
}
开发者ID:CodingDance,项目名称:GolangTraining,代码行数:26,代码来源:storage.go
示例4: PushDiff
func (s *googleReplicaAPIServer) PushDiff(pushDiffServer pfs.ReplicaAPI_PushDiffServer) (retErr error) {
defer func() {
if err := pushDiffServer.SendAndClose(google_protobuf.EmptyInstance); err != nil && retErr == nil {
retErr = err
}
}()
request, err := pushDiffServer.Recv()
if err != nil {
return err
}
writer := storage.NewWriter(s.ctx, s.bucket, fmt.Sprintf("%s/%s/%d", request.Commit.Repo.Name, request.Commit.Id, request.Shard))
defer func() {
if err := writer.Close(); err != nil && retErr == nil {
retErr = err
}
}()
reader := &pushDiffReader{
server: pushDiffServer,
}
_, err = reader.buffer.Write(request.Value)
if err != nil {
return err
}
_, err = io.Copy(writer, reader)
return err
}
开发者ID:klucar,项目名称:pachyderm,代码行数:26,代码来源:replica_server.go
示例5: uploadFileFromForm
// uploadFileFromForm uploads a file if it's present in the "image" form field.
func uploadFileFromForm(r *http.Request) (url string, err error) {
f, fh, err := r.FormFile("image")
if err == http.ErrMissingFile {
return "", nil
}
if err != nil {
return "", err
}
if bookshelf.StorageBucket == "" {
return "", errors.New("storage bucket is missing - check config.go")
}
// random filename, retaining existing extension.
name := uuid.NewV4().String() + path.Ext(fh.Filename)
w := storage.NewWriter(bookshelf.StorageCtx, bookshelf.StorageBucket, name)
w.ACL = []storage.ACLRule{{storage.AllUsers, storage.RoleReader}}
w.ContentType = fh.Header.Get("Content-Type")
// Entries are immutable, be aggressive about caching (1 day).
w.CacheControl = "public, max-age=86400"
if _, err := io.Copy(w, f); err != nil {
return "", err
}
if err := w.Close(); err != nil {
return "", err
}
const publicURL = "https://storage.googleapis.com/%s/%s"
return fmt.Sprintf(publicURL, bookshelf.StorageBucket, name), nil
}
开发者ID:huxq17,项目名称:golang-samples,代码行数:34,代码来源:app.go
示例6: Create
func (p provider) Create(ctx context.Context, name string, maxAge time.Duration, contentType string) (storage.Object, error) {
i := strings.Index(name, "/")
if i == -1 {
return nil, errors.New("name invalid, must contain '/'")
}
b := name[:i]
n := name[i:]
w := s.NewWriter(ctx, name, name)
w.ObjectAttrs = s.ObjectAttrs{
ContentType: contentType,
ContentLanguage: "", // TODO(flowlo): Does it make sense to set this?
ContentEncoding: "utf-8",
CacheControl: "max-age=" + strconv.FormatFloat(maxAge.Seconds(), 'f', 0, 64),
ContentDisposition: "attachment; filename=\"" + path.Base(name) + "\"",
}
return &object{
b: b,
n: n,
w: w,
r: nil,
ctx: ctx,
}, nil
}
开发者ID:pbochis,项目名称:api,代码行数:27,代码来源:google.go
示例7: Post
func (c *GoogleStorage) Post(obj *Object) (*Object, error) {
_, err := c.fetch(obj)
if err != nil {
log.Println("error on read url:", obj.Url, err)
return nil, err
}
// path = obj.path
wc := storage.NewWriter(c.ctx, c.bucket, obj.Path)
wc.ContentType = obj.MimeType
if _, err := wc.Write(obj.Content); err != nil {
log.Printf("error on write data: %s", err)
return nil, err
}
if err := wc.Close(); err != nil {
log.Printf("error on close writer: %s", err)
return nil, err
}
gcsObj := wc.Object()
newUrl := "https://storage.googleapis.com/" + c.bucket + "/" + gcsObj.Name
newObj := &Object{
Filename: obj.Filename,
Bucket: c.bucket,
Path: gcsObj.Name, // without bucket
MimeType: obj.MimeType,
Url: newUrl,
}
return newObj, nil
}
开发者ID:hi-trust,项目名称:friendfeed,代码行数:31,代码来源:media.go
示例8: PutContent
// PutContent stores the []byte content at a location designated by "path".
// This should primarily be used for small objects.
func (d *driver) PutContent(context ctx.Context, path string, contents []byte) error {
wc := storage.NewWriter(d.context(context), d.bucket, d.pathToKey(path))
wc.ContentType = "application/octet-stream"
defer wc.Close()
_, err := wc.Write(contents)
return err
}
开发者ID:tv42,项目名称:pachyderm,代码行数:9,代码来源:gcs.go
示例9: createFile
// createFile creates a file in Google Cloud Storage.
func (d *demo) createFile(fileName string) {
fmt.Fprintf(d.w, "Creating file /%v/%v\n", d.bucket, fileName)
wc := storage.NewWriter(d.ctx, d.bucket, fileName, &storage.Object{
ContentType: "text/plain",
Metadata: map[string]string{
"x-goog-meta-foo": "foo",
"x-goog-meta-bar": "bar",
},
})
d.cleanUp = append(d.cleanUp, fileName)
if _, err := wc.Write([]byte("abcde\n")); err != nil {
d.errorf("createFile: unable to write data to bucket %q, file %q: %v", d.bucket, fileName, err)
return
}
if _, err := wc.Write([]byte(strings.Repeat("f", 1024*4) + "\n")); err != nil {
d.errorf("createFile: unable to write data to bucket %q, file %q: %v", d.bucket, fileName, err)
return
}
if err := wc.Close(); err != nil {
d.errorf("createFile: unable to close bucket %q, file %q: %v", d.bucket, fileName, err)
return
}
// Wait for the file to be fully written.
_, err := wc.Object()
if err != nil {
d.errorf("createFile: unable to finalize file from bucket %q, file %q: %v", d.bucket, fileName, err)
return
}
}
开发者ID:qwo,项目名称:abelana-gcp,代码行数:32,代码来源:app.go
示例10: putFile
func putFile(ctx context.Context, name string, rdr io.Reader) error {
cctx := getCloudContext(ctx)
writer := storage.NewWriter(cctx, bucketName, name)
writer.ACL = []storage.ACLRule{
{storage.AllUsers, storage.RoleReader},
}
io.Copy(writer, rdr)
return writer.Close()
}
开发者ID:magj2006,项目名称:bootcamp-examples,代码行数:9,代码来源:storage.go
示例11: 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
示例12: OpenWriter
func (bs *GCSBlobStore) OpenWriter(blobpath string) (io.WriteCloser, error) {
if !oflags.IsWriteAllowed(bs.flags) {
return nil, otaru.EPERM
}
ctx := bs.newAuthedContext(context.TODO())
gcsw := storage.NewWriter(ctx, bs.bucketName, blobpath)
gcsw.ContentType = "application/octet-stream"
return &Writer{gcsw}, nil
}
开发者ID:hajimehoshi,项目名称:otaru,代码行数:11,代码来源:gcsblobstore.go
示例13: Write
func (o *object) Write(p []byte) (n int, err error) {
if o.r != nil {
return 0, errors.New("object is already opened for reading")
}
if o.w == nil {
o.w = s.NewWriter(o.ctx, o.b, o.n)
}
if o.w == nil {
return 0, errors.New("failed to connect to gcs")
}
return o.w.Write(p)
}
开发者ID:pbochis,项目名称:api,代码行数:12,代码来源:google.go
示例14: uploadFile
func uploadFile(ctx context.Context, b *Build, version, filename string) error {
file, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
base := filepath.Base(filename)
// Upload the file to Google Cloud Storage.
wr := storage.NewWriter(ctx, storageBucket, base)
wr.ACL = []storage.ACLRule{
{Entity: storage.AllUsers, Role: storage.RoleReader},
}
wr.Write(file)
if err := wr.Close(); err != nil {
return fmt.Errorf("uploading file: %v", err)
}
// Post file details to golang.org.
var kind string
switch {
case b.Source:
kind = "source"
case strings.HasSuffix(base, ".tar.gz"), strings.HasSuffix(base, ".zip"):
kind = "archive"
case strings.HasSuffix(base, ".msi"), strings.HasSuffix(base, ".pkg"):
kind = "installer"
}
req, err := json.Marshal(File{
Filename: base,
Version: version,
OS: b.OS,
Arch: b.Arch,
Checksum: fmt.Sprintf("%x", sha1.Sum(file)),
Size: int64(len(file)),
Kind: kind,
})
if err != nil {
return err
}
v := url.Values{"user": {*user}, "key": []string{userToken()}}
u := fmt.Sprintf("%s?%s", uploadURL, v.Encode())
resp, err := http.Post(u, "application/json", bytes.NewReader(req))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("upload failed: %v\n%s", resp.Status, b)
}
return nil
}
开发者ID:rdterner,项目名称:build,代码行数:53,代码来源:upload.go
示例15: handlePut
func handlePut(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
cctx := getCloudContext(ctx)
writer := storage.NewWriter(cctx, bucketName, "example.txt")
io.WriteString(writer, "Works")
err := writer.Close()
if err != nil {
http.Error(res, "ERROR WRITING TO BUCKET: "+err.Error(), 500)
return
}
}
开发者ID:magj2006,项目名称:bootcamp-examples,代码行数:12,代码来源:storage.go
示例16: fileHandler
func fileHandler(c *gin.Context) {
username := c.Param("username")
title := c.Param("title")
filename := c.Param("fileName")
gaeContext := appengine.NewContext(c.Request)
hc := &http.Client{
Transport: &CloudStorageTransport{&oauth2.Transport{
Source: google.AppEngineTokenSource(gaeContext, storage.ScopeFullControl),
Base: &urlfetch.Transport{Context: gaeContext},
}},
}
bucketName := "balde_de_bits"
bucketFile := username + "/" + title + "/" + filename
log.Errorf(gaeContext, "ID ->>> %v", appengine.AppID(gaeContext))
log.Errorf(gaeContext, "File name ->>> %v", bucketFile)
ctx := cloud.NewContext(appengine.AppID(gaeContext), hc)
wc := storage.NewWriter(ctx, bucketName, bucketFile)
if strings.Contains(filename, "m3u8") {
wc.ContentType = "application/x-mpegURL"
wc.CacheControl = "max-age:0"
} else if strings.Contains(filename, "ts") {
wc.ContentType = "video/MP2T"
} else if strings.Contains(filename, "jpg") {
wc.ContentType = "image/jpeg"
}
defer wc.Close()
bytesWritten, err := io.Copy(wc, c.Request.Body)
if err != nil {
log.Errorf(gaeContext, "Writing to cloud storage failed. %v", err.Error())
c.JSON(200, gin.H{
"response": "< FAILED >",
})
return
}
log.Errorf(gaeContext, "Wrote < %v > bytes for file < %v >", bytesWritten, filename)
c.JSON(200, gin.H{
"response": "< worked >",
})
}
开发者ID:BearchInc,项目名称:http-streaming,代码行数:51,代码来源:main.go
示例17: ExampleNewWriter
func ExampleNewWriter() {
ctx := Example_auth()
wc := storage.NewWriter(ctx, "bucketname", "filename1")
wc.ContentType = "text/plain"
wc.ACL = []storage.ACLRule{{storage.AllUsers, storage.RoleReader}}
if _, err := wc.Write([]byte("hello world")); err != nil {
log.Fatal(err)
}
if err := wc.Close(); err != nil {
log.Fatal(err)
}
log.Println("updated object:", wc.Object())
}
开发者ID:rkazak,项目名称:distribution,代码行数:14,代码来源:example_test.go
示例18: handlePut
func handlePut(res http.ResponseWriter, req *http.Request) {
cctx, err := getCloudContext(req)
if err != nil {
http.Error(res, "ERROR GETTING CCTX: "+err.Error(), 500)
return
}
writer := storage.NewWriter(cctx, gcsBucket, "example999.txt")
io.WriteString(writer, "showing the put get list")
err = writer.Close()
if err != nil {
http.Error(res, "ERROR WRITING TO BUCKET: "+err.Error(), 500)
return
}
}
开发者ID:RaviTezu,项目名称:GolangTraining,代码行数:15,代码来源:storage.go
示例19: handlePut
func handlePut(res http.ResponseWriter, req *http.Request) {
cctx, err := getCloudContext(req)
if err != nil {
http.Error(res, "ERROR GETTING CCTX: "+err.Error(), 500)
return
}
writer := storage.NewWriter(cctx, gcsBucket, "myOffice.txt")
writer.ContentType = "text/plain"
io.WriteString(writer, "in my office")
err = writer.Close()
if err != nil {
http.Error(res, "ERROR WRITING TO BUCKET: "+err.Error(), 500)
return
}
}
开发者ID:robertsturner52,项目名称:CIT90,代码行数:17,代码来源:storage.go
示例20: upload
func upload(ctx context.Context, bucket, base string, files []*multipart.FileHeader) multiError {
errc := make(chan error)
var errs []error
for _, fh := range files {
go func(fh *multipart.FileHeader) {
f, err := fh.Open()
if err != nil {
errc <- err
return
}
name := base + fh.Filename
wc := storage.NewWriter(ctx, bucket, name)
wc.ObjectAttrs = defaultObjectAttrs(path.Base(fh.Filename))
if _, err := io.Copy(wc, f); err != nil {
errc <- err
return
}
if err := wc.Close(); err != nil {
errc <- err
return
}
if err := f.Close(); err != nil {
errc <- err
return
}
errc <- nil
}(fh)
}
for range files {
err := <-errc
if err != nil {
errs = append(errs, err)
}
}
close(errc)
return errs
}
开发者ID:flowlo,项目名称:coduno-api,代码行数:46,代码来源:submission.go
注:本文中的google/golang.org/cloud/storage.NewWriter函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论