writer.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "fmt"
  17. "io"
  18. "unicode/utf8"
  19. "golang.org/x/net/context"
  20. "google.golang.org/api/googleapi"
  21. raw "google.golang.org/api/storage/v1"
  22. )
  23. // A Writer writes a Cloud Storage object.
  24. type Writer struct {
  25. // ObjectAttrs are optional attributes to set on the object. Any attributes
  26. // must be initialized before the first Write call. Nil or zero-valued
  27. // attributes are ignored.
  28. ObjectAttrs
  29. ctx context.Context
  30. o *ObjectHandle
  31. opened bool
  32. pw *io.PipeWriter
  33. donec chan struct{} // closed after err and obj are set.
  34. err error
  35. obj *ObjectAttrs
  36. }
  37. func (w *Writer) open() error {
  38. attrs := w.ObjectAttrs
  39. // Check the developer didn't change the object Name (this is unfortunate, but
  40. // we don't want to store an object under the wrong name).
  41. if attrs.Name != w.o.object {
  42. return fmt.Errorf("storage: Writer.Name %q does not match object name %q", attrs.Name, w.o.object)
  43. }
  44. if !utf8.ValidString(attrs.Name) {
  45. return fmt.Errorf("storage: object name %q is not valid UTF-8", attrs.Name)
  46. }
  47. pr, pw := io.Pipe()
  48. w.pw = pw
  49. w.opened = true
  50. var mediaOpts []googleapi.MediaOption
  51. if c := attrs.ContentType; c != "" {
  52. mediaOpts = append(mediaOpts, googleapi.ContentType(c))
  53. }
  54. go func() {
  55. defer close(w.donec)
  56. call := w.o.c.raw.Objects.Insert(w.o.bucket, attrs.toRawObject(w.o.bucket)).
  57. Media(pr, mediaOpts...).
  58. Projection("full").
  59. Context(w.ctx)
  60. var resp *raw.Object
  61. err := applyConds("NewWriter", w.o.conds, call)
  62. if err == nil {
  63. resp, err = call.Do()
  64. }
  65. if err != nil {
  66. w.err = err
  67. pr.CloseWithError(w.err)
  68. return
  69. }
  70. w.obj = newObject(resp)
  71. }()
  72. return nil
  73. }
  74. // Write appends to w.
  75. func (w *Writer) Write(p []byte) (n int, err error) {
  76. if w.err != nil {
  77. return 0, w.err
  78. }
  79. if !w.opened {
  80. if err := w.open(); err != nil {
  81. return 0, err
  82. }
  83. }
  84. return w.pw.Write(p)
  85. }
  86. // Close completes the write operation and flushes any buffered data.
  87. // If Close doesn't return an error, metadata about the written object
  88. // can be retrieved by calling Object.
  89. func (w *Writer) Close() error {
  90. if !w.opened {
  91. if err := w.open(); err != nil {
  92. return err
  93. }
  94. }
  95. if err := w.pw.Close(); err != nil {
  96. return err
  97. }
  98. <-w.donec
  99. return w.err
  100. }
  101. // CloseWithError aborts the write operation with the provided error.
  102. // CloseWithError always returns nil.
  103. func (w *Writer) CloseWithError(err error) error {
  104. if !w.opened {
  105. return nil
  106. }
  107. return w.pw.CloseWithError(err)
  108. }
  109. // ObjectAttrs returns metadata about a successfully-written object.
  110. // It's only valid to call it after Close returns nil.
  111. func (w *Writer) Attrs() *ObjectAttrs {
  112. return w.obj
  113. }