api-get-object.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2015-2020 MinIO, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package minio
  18. import (
  19. "context"
  20. "errors"
  21. "fmt"
  22. "io"
  23. "net/http"
  24. "net/url"
  25. "sync"
  26. "github.com/minio/minio-go/v7/pkg/s3utils"
  27. )
  28. // GetObject wrapper function that accepts a request context
  29. func (c Client) GetObject(ctx context.Context, bucketName, objectName string, opts GetObjectOptions) (*Object, error) {
  30. // Input validation.
  31. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  32. return nil, err
  33. }
  34. if err := s3utils.CheckValidObjectName(objectName); err != nil {
  35. return nil, err
  36. }
  37. // Detect if snowball is server location we are talking to.
  38. var snowball bool
  39. if location, ok := c.bucketLocCache.Get(bucketName); ok {
  40. if location == "snowball" {
  41. snowball = true
  42. }
  43. }
  44. var (
  45. err error
  46. httpReader io.ReadCloser
  47. objectInfo ObjectInfo
  48. totalRead int
  49. )
  50. // Create request channel.
  51. reqCh := make(chan getRequest)
  52. // Create response channel.
  53. resCh := make(chan getResponse)
  54. // Create done channel.
  55. doneCh := make(chan struct{})
  56. // This routine feeds partial object data as and when the caller reads.
  57. go func() {
  58. defer close(reqCh)
  59. defer close(resCh)
  60. // Used to verify if etag of object has changed since last read.
  61. var etag string
  62. // Loop through the incoming control messages and read data.
  63. for {
  64. select {
  65. // When the done channel is closed exit our routine.
  66. case <-doneCh:
  67. // Close the http response body before returning.
  68. // This ends the connection with the server.
  69. if httpReader != nil {
  70. httpReader.Close()
  71. }
  72. return
  73. // Gather incoming request.
  74. case req := <-reqCh:
  75. // If this is the first request we may not need to do a getObject request yet.
  76. if req.isFirstReq {
  77. // First request is a Read/ReadAt.
  78. if req.isReadOp {
  79. // Differentiate between wanting the whole object and just a range.
  80. if req.isReadAt {
  81. // If this is a ReadAt request only get the specified range.
  82. // Range is set with respect to the offset and length of the buffer requested.
  83. // Do not set objectInfo from the first readAt request because it will not get
  84. // the whole object.
  85. opts.SetRange(req.Offset, req.Offset+int64(len(req.Buffer))-1)
  86. } else if req.Offset > 0 {
  87. opts.SetRange(req.Offset, 0)
  88. }
  89. httpReader, objectInfo, _, err = c.getObject(ctx, bucketName, objectName, opts)
  90. if err != nil {
  91. resCh <- getResponse{Error: err}
  92. return
  93. }
  94. etag = objectInfo.ETag
  95. // Read at least firstReq.Buffer bytes, if not we have
  96. // reached our EOF.
  97. size, err := readFull(httpReader, req.Buffer)
  98. totalRead += size
  99. if size > 0 && err == io.ErrUnexpectedEOF {
  100. if int64(size) < objectInfo.Size {
  101. // In situations when returned size
  102. // is less than the expected content
  103. // length set by the server, make sure
  104. // we return io.ErrUnexpectedEOF
  105. err = io.ErrUnexpectedEOF
  106. } else {
  107. // If an EOF happens after reading some but not
  108. // all the bytes ReadFull returns ErrUnexpectedEOF
  109. err = io.EOF
  110. }
  111. } else if size == 0 && err == io.EOF && objectInfo.Size > 0 {
  112. // Special cases when server writes more data
  113. // than the content-length, net/http response
  114. // body returns an error, instead of converting
  115. // it to io.EOF - return unexpected EOF.
  116. err = io.ErrUnexpectedEOF
  117. }
  118. // Send back the first response.
  119. resCh <- getResponse{
  120. objectInfo: objectInfo,
  121. Size: size,
  122. Error: err,
  123. didRead: true,
  124. }
  125. } else {
  126. // First request is a Stat or Seek call.
  127. // Only need to run a StatObject until an actual Read or ReadAt request comes through.
  128. // Remove range header if already set, for stat Operations to get original file size.
  129. delete(opts.headers, "Range")
  130. objectInfo, err = c.statObject(ctx, bucketName, objectName, StatObjectOptions(opts))
  131. if err != nil {
  132. resCh <- getResponse{
  133. Error: err,
  134. }
  135. // Exit the go-routine.
  136. return
  137. }
  138. etag = objectInfo.ETag
  139. // Send back the first response.
  140. resCh <- getResponse{
  141. objectInfo: objectInfo,
  142. }
  143. }
  144. } else if req.settingObjectInfo { // Request is just to get objectInfo.
  145. // Remove range header if already set, for stat Operations to get original file size.
  146. delete(opts.headers, "Range")
  147. // Check whether this is snowball
  148. // if yes do not use If-Match feature
  149. // it doesn't work.
  150. if etag != "" && !snowball {
  151. opts.SetMatchETag(etag)
  152. }
  153. objectInfo, err := c.statObject(ctx, bucketName, objectName, StatObjectOptions(opts))
  154. if err != nil {
  155. resCh <- getResponse{
  156. Error: err,
  157. }
  158. // Exit the goroutine.
  159. return
  160. }
  161. // Send back the objectInfo.
  162. resCh <- getResponse{
  163. objectInfo: objectInfo,
  164. }
  165. } else {
  166. // Offset changes fetch the new object at an Offset.
  167. // Because the httpReader may not be set by the first
  168. // request if it was a stat or seek it must be checked
  169. // if the object has been read or not to only initialize
  170. // new ones when they haven't been already.
  171. // All readAt requests are new requests.
  172. if req.DidOffsetChange || !req.beenRead {
  173. // Check whether this is snowball
  174. // if yes do not use If-Match feature
  175. // it doesn't work.
  176. if etag != "" && !snowball {
  177. opts.SetMatchETag(etag)
  178. }
  179. if httpReader != nil {
  180. // Close previously opened http reader.
  181. httpReader.Close()
  182. }
  183. // If this request is a readAt only get the specified range.
  184. if req.isReadAt {
  185. // Range is set with respect to the offset and length of the buffer requested.
  186. opts.SetRange(req.Offset, req.Offset+int64(len(req.Buffer))-1)
  187. } else if req.Offset > 0 { // Range is set with respect to the offset.
  188. opts.SetRange(req.Offset, 0)
  189. }
  190. httpReader, objectInfo, _, err = c.getObject(ctx, bucketName, objectName, opts)
  191. if err != nil {
  192. resCh <- getResponse{
  193. Error: err,
  194. }
  195. return
  196. }
  197. totalRead = 0
  198. }
  199. // Read at least req.Buffer bytes, if not we have
  200. // reached our EOF.
  201. size, err := readFull(httpReader, req.Buffer)
  202. totalRead += size
  203. if size > 0 && err == io.ErrUnexpectedEOF {
  204. if int64(totalRead) < objectInfo.Size {
  205. // In situations when returned size
  206. // is less than the expected content
  207. // length set by the server, make sure
  208. // we return io.ErrUnexpectedEOF
  209. err = io.ErrUnexpectedEOF
  210. } else {
  211. // If an EOF happens after reading some but not
  212. // all the bytes ReadFull returns ErrUnexpectedEOF
  213. err = io.EOF
  214. }
  215. } else if size == 0 && err == io.EOF && objectInfo.Size > 0 {
  216. // Special cases when server writes more data
  217. // than the content-length, net/http response
  218. // body returns an error, instead of converting
  219. // it to io.EOF - return unexpected EOF.
  220. err = io.ErrUnexpectedEOF
  221. }
  222. // Reply back how much was read.
  223. resCh <- getResponse{
  224. Size: size,
  225. Error: err,
  226. didRead: true,
  227. objectInfo: objectInfo,
  228. }
  229. }
  230. }
  231. }
  232. }()
  233. // Create a newObject through the information sent back by reqCh.
  234. return newObject(reqCh, resCh, doneCh), nil
  235. }
  236. // get request message container to communicate with internal
  237. // go-routine.
  238. type getRequest struct {
  239. Buffer []byte
  240. Offset int64 // readAt offset.
  241. DidOffsetChange bool // Tracks the offset changes for Seek requests.
  242. beenRead bool // Determines if this is the first time an object is being read.
  243. isReadAt bool // Determines if this request is a request to a specific range
  244. isReadOp bool // Determines if this request is a Read or Read/At request.
  245. isFirstReq bool // Determines if this request is the first time an object is being accessed.
  246. settingObjectInfo bool // Determines if this request is to set the objectInfo of an object.
  247. }
  248. // get response message container to reply back for the request.
  249. type getResponse struct {
  250. Size int
  251. Error error
  252. didRead bool // Lets subsequent calls know whether or not httpReader has been initiated.
  253. objectInfo ObjectInfo // Used for the first request.
  254. }
  255. // Object represents an open object. It implements
  256. // Reader, ReaderAt, Seeker, Closer for a HTTP stream.
  257. type Object struct {
  258. // Mutex.
  259. mutex *sync.Mutex
  260. // User allocated and defined.
  261. reqCh chan<- getRequest
  262. resCh <-chan getResponse
  263. doneCh chan<- struct{}
  264. currOffset int64
  265. objectInfo ObjectInfo
  266. // Ask lower level to initiate data fetching based on currOffset
  267. seekData bool
  268. // Keeps track of closed call.
  269. isClosed bool
  270. // Keeps track of if this is the first call.
  271. isStarted bool
  272. // Previous error saved for future calls.
  273. prevErr error
  274. // Keeps track of if this object has been read yet.
  275. beenRead bool
  276. // Keeps track of if objectInfo has been set yet.
  277. objectInfoSet bool
  278. }
  279. // doGetRequest - sends and blocks on the firstReqCh and reqCh of an object.
  280. // Returns back the size of the buffer read, if anything was read, as well
  281. // as any error encountered. For all first requests sent on the object
  282. // it is also responsible for sending back the objectInfo.
  283. func (o *Object) doGetRequest(request getRequest) (getResponse, error) {
  284. o.reqCh <- request
  285. response := <-o.resCh
  286. // Return any error to the top level.
  287. if response.Error != nil {
  288. return response, response.Error
  289. }
  290. // This was the first request.
  291. if !o.isStarted {
  292. // The object has been operated on.
  293. o.isStarted = true
  294. }
  295. // Set the objectInfo if the request was not readAt
  296. // and it hasn't been set before.
  297. if !o.objectInfoSet && !request.isReadAt {
  298. o.objectInfo = response.objectInfo
  299. o.objectInfoSet = true
  300. }
  301. // Set beenRead only if it has not been set before.
  302. if !o.beenRead {
  303. o.beenRead = response.didRead
  304. }
  305. // Data are ready on the wire, no need to reinitiate connection in lower level
  306. o.seekData = false
  307. return response, nil
  308. }
  309. // setOffset - handles the setting of offsets for
  310. // Read/ReadAt/Seek requests.
  311. func (o *Object) setOffset(bytesRead int64) error {
  312. // Update the currentOffset.
  313. o.currOffset += bytesRead
  314. if o.objectInfo.Size > -1 && o.currOffset >= o.objectInfo.Size {
  315. return io.EOF
  316. }
  317. return nil
  318. }
  319. // Read reads up to len(b) bytes into b. It returns the number of
  320. // bytes read (0 <= n <= len(b)) and any error encountered. Returns
  321. // io.EOF upon end of file.
  322. func (o *Object) Read(b []byte) (n int, err error) {
  323. if o == nil {
  324. return 0, errInvalidArgument("Object is nil")
  325. }
  326. // Locking.
  327. o.mutex.Lock()
  328. defer o.mutex.Unlock()
  329. // prevErr is previous error saved from previous operation.
  330. if o.prevErr != nil || o.isClosed {
  331. return 0, o.prevErr
  332. }
  333. // Create a new request.
  334. readReq := getRequest{
  335. isReadOp: true,
  336. beenRead: o.beenRead,
  337. Buffer: b,
  338. }
  339. // Alert that this is the first request.
  340. if !o.isStarted {
  341. readReq.isFirstReq = true
  342. }
  343. // Ask to establish a new data fetch routine based on seekData flag
  344. readReq.DidOffsetChange = o.seekData
  345. readReq.Offset = o.currOffset
  346. // Send and receive from the first request.
  347. response, err := o.doGetRequest(readReq)
  348. if err != nil && err != io.EOF {
  349. // Save the error for future calls.
  350. o.prevErr = err
  351. return response.Size, err
  352. }
  353. // Bytes read.
  354. bytesRead := int64(response.Size)
  355. // Set the new offset.
  356. oerr := o.setOffset(bytesRead)
  357. if oerr != nil {
  358. // Save the error for future calls.
  359. o.prevErr = oerr
  360. return response.Size, oerr
  361. }
  362. // Return the response.
  363. return response.Size, err
  364. }
  365. // Stat returns the ObjectInfo structure describing Object.
  366. func (o *Object) Stat() (ObjectInfo, error) {
  367. if o == nil {
  368. return ObjectInfo{}, errInvalidArgument("Object is nil")
  369. }
  370. // Locking.
  371. o.mutex.Lock()
  372. defer o.mutex.Unlock()
  373. if o.prevErr != nil && o.prevErr != io.EOF || o.isClosed {
  374. return ObjectInfo{}, o.prevErr
  375. }
  376. // This is the first request.
  377. if !o.isStarted || !o.objectInfoSet {
  378. // Send the request and get the response.
  379. _, err := o.doGetRequest(getRequest{
  380. isFirstReq: !o.isStarted,
  381. settingObjectInfo: !o.objectInfoSet,
  382. })
  383. if err != nil {
  384. o.prevErr = err
  385. return ObjectInfo{}, err
  386. }
  387. }
  388. return o.objectInfo, nil
  389. }
  390. // ReadAt reads len(b) bytes from the File starting at byte offset
  391. // off. It returns the number of bytes read and the error, if any.
  392. // ReadAt always returns a non-nil error when n < len(b). At end of
  393. // file, that error is io.EOF.
  394. func (o *Object) ReadAt(b []byte, offset int64) (n int, err error) {
  395. if o == nil {
  396. return 0, errInvalidArgument("Object is nil")
  397. }
  398. // Locking.
  399. o.mutex.Lock()
  400. defer o.mutex.Unlock()
  401. // prevErr is error which was saved in previous operation.
  402. if o.prevErr != nil && o.prevErr != io.EOF || o.isClosed {
  403. return 0, o.prevErr
  404. }
  405. // Set the current offset to ReadAt offset, because the current offset will be shifted at the end of this method.
  406. o.currOffset = offset
  407. // Can only compare offsets to size when size has been set.
  408. if o.objectInfoSet {
  409. // If offset is negative than we return io.EOF.
  410. // If offset is greater than or equal to object size we return io.EOF.
  411. if (o.objectInfo.Size > -1 && offset >= o.objectInfo.Size) || offset < 0 {
  412. return 0, io.EOF
  413. }
  414. }
  415. // Create the new readAt request.
  416. readAtReq := getRequest{
  417. isReadOp: true,
  418. isReadAt: true,
  419. DidOffsetChange: true, // Offset always changes.
  420. beenRead: o.beenRead, // Set if this is the first request to try and read.
  421. Offset: offset, // Set the offset.
  422. Buffer: b,
  423. }
  424. // Alert that this is the first request.
  425. if !o.isStarted {
  426. readAtReq.isFirstReq = true
  427. }
  428. // Send and receive from the first request.
  429. response, err := o.doGetRequest(readAtReq)
  430. if err != nil && err != io.EOF {
  431. // Save the error.
  432. o.prevErr = err
  433. return response.Size, err
  434. }
  435. // Bytes read.
  436. bytesRead := int64(response.Size)
  437. // There is no valid objectInfo yet
  438. // to compare against for EOF.
  439. if !o.objectInfoSet {
  440. // Update the currentOffset.
  441. o.currOffset += bytesRead
  442. } else {
  443. // If this was not the first request update
  444. // the offsets and compare against objectInfo
  445. // for EOF.
  446. oerr := o.setOffset(bytesRead)
  447. if oerr != nil {
  448. o.prevErr = oerr
  449. return response.Size, oerr
  450. }
  451. }
  452. return response.Size, err
  453. }
  454. // Seek sets the offset for the next Read or Write to offset,
  455. // interpreted according to whence: 0 means relative to the
  456. // origin of the file, 1 means relative to the current offset,
  457. // and 2 means relative to the end.
  458. // Seek returns the new offset and an error, if any.
  459. //
  460. // Seeking to a negative offset is an error. Seeking to any positive
  461. // offset is legal, subsequent io operations succeed until the
  462. // underlying object is not closed.
  463. func (o *Object) Seek(offset int64, whence int) (n int64, err error) {
  464. if o == nil {
  465. return 0, errInvalidArgument("Object is nil")
  466. }
  467. // Locking.
  468. o.mutex.Lock()
  469. defer o.mutex.Unlock()
  470. // At EOF seeking is legal allow only io.EOF, for any other errors we return.
  471. if o.prevErr != nil && o.prevErr != io.EOF {
  472. return 0, o.prevErr
  473. }
  474. // Negative offset is valid for whence of '2'.
  475. if offset < 0 && whence != 2 {
  476. return 0, errInvalidArgument(fmt.Sprintf("Negative position not allowed for %d", whence))
  477. }
  478. // This is the first request. So before anything else
  479. // get the ObjectInfo.
  480. if !o.isStarted || !o.objectInfoSet {
  481. // Create the new Seek request.
  482. seekReq := getRequest{
  483. isReadOp: false,
  484. Offset: offset,
  485. isFirstReq: true,
  486. }
  487. // Send and receive from the seek request.
  488. _, err := o.doGetRequest(seekReq)
  489. if err != nil {
  490. // Save the error.
  491. o.prevErr = err
  492. return 0, err
  493. }
  494. }
  495. // Switch through whence.
  496. switch whence {
  497. default:
  498. return 0, errInvalidArgument(fmt.Sprintf("Invalid whence %d", whence))
  499. case 0:
  500. if o.objectInfo.Size > -1 && offset > o.objectInfo.Size {
  501. return 0, io.EOF
  502. }
  503. o.currOffset = offset
  504. case 1:
  505. if o.objectInfo.Size > -1 && o.currOffset+offset > o.objectInfo.Size {
  506. return 0, io.EOF
  507. }
  508. o.currOffset += offset
  509. case 2:
  510. // If we don't know the object size return an error for io.SeekEnd
  511. if o.objectInfo.Size < 0 {
  512. return 0, errInvalidArgument("Whence END is not supported when the object size is unknown")
  513. }
  514. // Seeking to positive offset is valid for whence '2', but
  515. // since we are backing a Reader we have reached 'EOF' if
  516. // offset is positive.
  517. if offset > 0 {
  518. return 0, io.EOF
  519. }
  520. // Seeking to negative position not allowed for whence.
  521. if o.objectInfo.Size+offset < 0 {
  522. return 0, errInvalidArgument(fmt.Sprintf("Seeking at negative offset not allowed for %d", whence))
  523. }
  524. o.currOffset = o.objectInfo.Size + offset
  525. }
  526. // Reset the saved error since we successfully seeked, let the Read
  527. // and ReadAt decide.
  528. if o.prevErr == io.EOF {
  529. o.prevErr = nil
  530. }
  531. // Ask lower level to fetch again from source
  532. o.seekData = true
  533. // Return the effective offset.
  534. return o.currOffset, nil
  535. }
  536. // Close - The behavior of Close after the first call returns error
  537. // for subsequent Close() calls.
  538. func (o *Object) Close() (err error) {
  539. if o == nil {
  540. return errInvalidArgument("Object is nil")
  541. }
  542. // Locking.
  543. o.mutex.Lock()
  544. defer o.mutex.Unlock()
  545. // if already closed return an error.
  546. if o.isClosed {
  547. return o.prevErr
  548. }
  549. // Close successfully.
  550. close(o.doneCh)
  551. // Save for future operations.
  552. errMsg := "Object is already closed. Bad file descriptor."
  553. o.prevErr = errors.New(errMsg)
  554. // Save here that we closed done channel successfully.
  555. o.isClosed = true
  556. return nil
  557. }
  558. // newObject instantiates a new *minio.Object*
  559. // ObjectInfo will be set by setObjectInfo
  560. func newObject(reqCh chan<- getRequest, resCh <-chan getResponse, doneCh chan<- struct{}) *Object {
  561. return &Object{
  562. mutex: &sync.Mutex{},
  563. reqCh: reqCh,
  564. resCh: resCh,
  565. doneCh: doneCh,
  566. }
  567. }
  568. // getObject - retrieve object from Object Storage.
  569. //
  570. // Additionally this function also takes range arguments to download the specified
  571. // range bytes of an object. Setting offset and length = 0 will download the full object.
  572. //
  573. // For more information about the HTTP Range header.
  574. // go to http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.
  575. func (c Client) getObject(ctx context.Context, bucketName, objectName string, opts GetObjectOptions) (io.ReadCloser, ObjectInfo, http.Header, error) {
  576. // Validate input arguments.
  577. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  578. return nil, ObjectInfo{}, nil, err
  579. }
  580. if err := s3utils.CheckValidObjectName(objectName); err != nil {
  581. return nil, ObjectInfo{}, nil, err
  582. }
  583. urlValues := make(url.Values)
  584. if opts.VersionID != "" {
  585. urlValues.Set("versionId", opts.VersionID)
  586. }
  587. // Execute GET on objectName.
  588. resp, err := c.executeMethod(ctx, http.MethodGet, requestMetadata{
  589. bucketName: bucketName,
  590. objectName: objectName,
  591. queryValues: urlValues,
  592. customHeader: opts.Header(),
  593. contentSHA256Hex: emptySHA256Hex,
  594. })
  595. if err != nil {
  596. return nil, ObjectInfo{}, nil, err
  597. }
  598. if resp != nil {
  599. if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
  600. return nil, ObjectInfo{}, nil, httpRespToErrorResponse(resp, bucketName, objectName)
  601. }
  602. }
  603. objectStat, err := ToObjectInfo(bucketName, objectName, resp.Header)
  604. if err != nil {
  605. closeResponse(resp)
  606. return nil, ObjectInfo{}, nil, err
  607. }
  608. // do not close body here, caller will close
  609. return resp.Body, objectStat, resp.Header, nil
  610. }