writer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. )
  21. // A Writer writes a Cloud Storage object.
  22. type Writer struct {
  23. // ObjectAttrs are optional attributes to set on the object. Any attributes
  24. // must be initialized before the first Write call. Nil or zero-valued
  25. // attributes are ignored.
  26. ObjectAttrs
  27. ctx context.Context
  28. client *Client
  29. bucket string
  30. name string
  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.name {
  42. return fmt.Errorf("storage: Writer.Name %q does not match object name %q", attrs.Name, w.name)
  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. r := &contentTyper{pr, attrs.ContentType}
  49. w.pw = pw
  50. w.opened = true
  51. go func() {
  52. resp, err := w.client.raw.Objects.Insert(
  53. w.bucket, attrs.toRawObject(w.bucket)).Media(r).Projection("full").Context(w.ctx).Do()
  54. w.err = err
  55. if err == nil {
  56. w.obj = newObject(resp)
  57. } else {
  58. pr.CloseWithError(w.err)
  59. }
  60. close(w.donec)
  61. }()
  62. return nil
  63. }
  64. // Write appends to w.
  65. func (w *Writer) Write(p []byte) (n int, err error) {
  66. if w.err != nil {
  67. return 0, w.err
  68. }
  69. if !w.opened {
  70. if err := w.open(); err != nil {
  71. return 0, err
  72. }
  73. }
  74. return w.pw.Write(p)
  75. }
  76. // Close completes the write operation and flushes any buffered data.
  77. // If Close doesn't return an error, metadata about the written object
  78. // can be retrieved by calling Object.
  79. func (w *Writer) Close() error {
  80. if !w.opened {
  81. if err := w.open(); err != nil {
  82. return err
  83. }
  84. }
  85. if err := w.pw.Close(); err != nil {
  86. return err
  87. }
  88. <-w.donec
  89. return w.err
  90. }
  91. // CloseWithError aborts the write operation with the provided error.
  92. // CloseWithError always returns nil.
  93. func (w *Writer) CloseWithError(err error) error {
  94. if !w.opened {
  95. return nil
  96. }
  97. return w.pw.CloseWithError(err)
  98. }
  99. // ObjectAttrs returns metadata about a successfully-written object.
  100. // It's only valid to call it after Close returns nil.
  101. func (w *Writer) Attrs() *ObjectAttrs {
  102. return w.obj
  103. }