You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

262 lines
7.6 KiB

  1. // Copyright 2014 Google LLC
  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. "context"
  17. "encoding/base64"
  18. "errors"
  19. "fmt"
  20. "io"
  21. "sync"
  22. "unicode/utf8"
  23. "google.golang.org/api/googleapi"
  24. raw "google.golang.org/api/storage/v1"
  25. )
  26. // A Writer writes a Cloud Storage object.
  27. type Writer struct {
  28. // ObjectAttrs are optional attributes to set on the object. Any attributes
  29. // must be initialized before the first Write call. Nil or zero-valued
  30. // attributes are ignored.
  31. ObjectAttrs
  32. // SendCRC specifies whether to transmit a CRC32C field. It should be set
  33. // to true in addition to setting the Writer's CRC32C field, because zero
  34. // is a valid CRC and normally a zero would not be transmitted.
  35. // If a CRC32C is sent, and the data written does not match the checksum,
  36. // the write will be rejected.
  37. SendCRC32C bool
  38. // ChunkSize controls the maximum number of bytes of the object that the
  39. // Writer will attempt to send to the server in a single request. Objects
  40. // smaller than the size will be sent in a single request, while larger
  41. // objects will be split over multiple requests. The size will be rounded up
  42. // to the nearest multiple of 256K. If zero, chunking will be disabled and
  43. // the object will be uploaded in a single request.
  44. //
  45. // ChunkSize will default to a reasonable value. If you perform many concurrent
  46. // writes of small objects, you may wish set ChunkSize to a value that matches
  47. // your objects' sizes to avoid consuming large amounts of memory.
  48. //
  49. // ChunkSize must be set before the first Write call.
  50. ChunkSize int
  51. // ProgressFunc can be used to monitor the progress of a large write.
  52. // operation. If ProgressFunc is not nil and writing requires multiple
  53. // calls to the underlying service (see
  54. // https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload),
  55. // then ProgressFunc will be invoked after each call with the number of bytes of
  56. // content copied so far.
  57. //
  58. // ProgressFunc should return quickly without blocking.
  59. ProgressFunc func(int64)
  60. ctx context.Context
  61. o *ObjectHandle
  62. opened bool
  63. pw *io.PipeWriter
  64. donec chan struct{} // closed after err and obj are set.
  65. obj *ObjectAttrs
  66. mu sync.Mutex
  67. err error
  68. }
  69. func (w *Writer) open() error {
  70. attrs := w.ObjectAttrs
  71. // Check the developer didn't change the object Name (this is unfortunate, but
  72. // we don't want to store an object under the wrong name).
  73. if attrs.Name != w.o.object {
  74. return fmt.Errorf("storage: Writer.Name %q does not match object name %q", attrs.Name, w.o.object)
  75. }
  76. if !utf8.ValidString(attrs.Name) {
  77. return fmt.Errorf("storage: object name %q is not valid UTF-8", attrs.Name)
  78. }
  79. if attrs.KMSKeyName != "" && w.o.encryptionKey != nil {
  80. return errors.New("storage: cannot use KMSKeyName with a customer-supplied encryption key")
  81. }
  82. pr, pw := io.Pipe()
  83. w.pw = pw
  84. w.opened = true
  85. go w.monitorCancel()
  86. if w.ChunkSize < 0 {
  87. return errors.New("storage: Writer.ChunkSize must be non-negative")
  88. }
  89. mediaOpts := []googleapi.MediaOption{
  90. googleapi.ChunkSize(w.ChunkSize),
  91. }
  92. if c := attrs.ContentType; c != "" {
  93. mediaOpts = append(mediaOpts, googleapi.ContentType(c))
  94. }
  95. go func() {
  96. defer close(w.donec)
  97. rawObj := attrs.toRawObject(w.o.bucket)
  98. if w.SendCRC32C {
  99. rawObj.Crc32c = encodeUint32(attrs.CRC32C)
  100. }
  101. if w.MD5 != nil {
  102. rawObj.Md5Hash = base64.StdEncoding.EncodeToString(w.MD5)
  103. }
  104. call := w.o.c.raw.Objects.Insert(w.o.bucket, rawObj).
  105. Media(pr, mediaOpts...).
  106. Projection("full").
  107. Context(w.ctx)
  108. if w.ProgressFunc != nil {
  109. call.ProgressUpdater(func(n, _ int64) { w.ProgressFunc(n) })
  110. }
  111. if attrs.KMSKeyName != "" {
  112. call.KmsKeyName(attrs.KMSKeyName)
  113. }
  114. if attrs.PredefinedACL != "" {
  115. call.PredefinedAcl(attrs.PredefinedACL)
  116. }
  117. if err := setEncryptionHeaders(call.Header(), w.o.encryptionKey, false); err != nil {
  118. w.mu.Lock()
  119. w.err = err
  120. w.mu.Unlock()
  121. pr.CloseWithError(err)
  122. return
  123. }
  124. var resp *raw.Object
  125. err := applyConds("NewWriter", w.o.gen, w.o.conds, call)
  126. if err == nil {
  127. if w.o.userProject != "" {
  128. call.UserProject(w.o.userProject)
  129. }
  130. setClientHeader(call.Header())
  131. // If the chunk size is zero, then no chunking is done on the Reader,
  132. // which means we cannot retry: the first call will read the data, and if
  133. // it fails, there is no way to re-read.
  134. if w.ChunkSize == 0 {
  135. resp, err = call.Do()
  136. } else {
  137. // We will only retry here if the initial POST, which obtains a URI for
  138. // the resumable upload, fails with a retryable error. The upload itself
  139. // has its own retry logic.
  140. err = runWithRetry(w.ctx, func() error {
  141. var err2 error
  142. resp, err2 = call.Do()
  143. return err2
  144. })
  145. }
  146. }
  147. if err != nil {
  148. w.mu.Lock()
  149. w.err = err
  150. w.mu.Unlock()
  151. pr.CloseWithError(err)
  152. return
  153. }
  154. w.obj = newObject(resp)
  155. }()
  156. return nil
  157. }
  158. // Write appends to w. It implements the io.Writer interface.
  159. //
  160. // Since writes happen asynchronously, Write may return a nil
  161. // error even though the write failed (or will fail). Always
  162. // use the error returned from Writer.Close to determine if
  163. // the upload was successful.
  164. func (w *Writer) Write(p []byte) (n int, err error) {
  165. w.mu.Lock()
  166. werr := w.err
  167. w.mu.Unlock()
  168. if werr != nil {
  169. return 0, werr
  170. }
  171. if !w.opened {
  172. if err := w.open(); err != nil {
  173. return 0, err
  174. }
  175. }
  176. n, err = w.pw.Write(p)
  177. if err != nil {
  178. w.mu.Lock()
  179. werr := w.err
  180. w.mu.Unlock()
  181. // Preserve existing functionality that when context is canceled, Write will return
  182. // context.Canceled instead of "io: read/write on closed pipe". This hides the
  183. // pipe implementation detail from users and makes Write seem as though it's an RPC.
  184. if werr == context.Canceled || werr == context.DeadlineExceeded {
  185. return n, werr
  186. }
  187. }
  188. return n, err
  189. }
  190. // Close completes the write operation and flushes any buffered data.
  191. // If Close doesn't return an error, metadata about the written object
  192. // can be retrieved by calling Attrs.
  193. func (w *Writer) Close() error {
  194. if !w.opened {
  195. if err := w.open(); err != nil {
  196. return err
  197. }
  198. }
  199. // Closing either the read or write causes the entire pipe to close.
  200. if err := w.pw.Close(); err != nil {
  201. return err
  202. }
  203. <-w.donec
  204. w.mu.Lock()
  205. defer w.mu.Unlock()
  206. return w.err
  207. }
  208. // monitorCancel is intended to be used as a background goroutine. It monitors the
  209. // the context, and when it observes that the context has been canceled, it manually
  210. // closes things that do not take a context.
  211. func (w *Writer) monitorCancel() {
  212. select {
  213. case <-w.ctx.Done():
  214. w.mu.Lock()
  215. werr := w.ctx.Err()
  216. w.err = werr
  217. w.mu.Unlock()
  218. // Closing either the read or write causes the entire pipe to close.
  219. w.CloseWithError(werr)
  220. case <-w.donec:
  221. }
  222. }
  223. // CloseWithError aborts the write operation with the provided error.
  224. // CloseWithError always returns nil.
  225. //
  226. // Deprecated: cancel the context passed to NewWriter instead.
  227. func (w *Writer) CloseWithError(err error) error {
  228. if !w.opened {
  229. return nil
  230. }
  231. return w.pw.CloseWithError(err)
  232. }
  233. // Attrs returns metadata about a successfully-written object.
  234. // It's only valid to call it after Close returns nil.
  235. func (w *Writer) Attrs() *ObjectAttrs {
  236. return w.obj
  237. }