本文整理汇总了Golang中google/golang.org/cloud/storage.NewWriter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewWriter函数的具体用法?Golang NewWriter怎么用?Golang NewWriter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewWriter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
}
示例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)
})
}
示例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)
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
}
示例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()
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例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
}
}