Open an object for read
(ctx context.Context, options ...fs.OpenOption)
| 1383 | |
| 1384 | // Open an object for read |
| 1385 | func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.ReadCloser, err error) { |
| 1386 | url := o.url |
| 1387 | if o.fs.opt.UserProject != "" { |
| 1388 | url += "&userProject=" + o.fs.opt.UserProject |
| 1389 | } |
| 1390 | req, err := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 1391 | if err != nil { |
| 1392 | return nil, err |
| 1393 | } |
| 1394 | fs.FixRangeOption(options, o.bytes) |
| 1395 | if o.gzipped && !o.fs.opt.Decompress { |
| 1396 | // Allow files which are stored on the cloud storage system |
| 1397 | // compressed to be downloaded without being decompressed. Note |
| 1398 | // that setting this here overrides the automatic decompression |
| 1399 | // in the Transport. |
| 1400 | // |
| 1401 | // See: https://cloud.google.com/storage/docs/transcoding |
| 1402 | req.Header.Set("Accept-Encoding", "gzip") |
| 1403 | o.fs.warnCompressed.Do(func() { |
| 1404 | fs.Logf(o, "Not decompressing 'Content-Encoding: gzip' compressed file. Use --gcs-decompress to override") |
| 1405 | }) |
| 1406 | } |
| 1407 | fs.OpenOptionAddHTTPHeaders(req.Header, options) |
| 1408 | var res *http.Response |
| 1409 | err = o.fs.pacer.Call(func() (bool, error) { |
| 1410 | res, err = o.fs.client.Do(req) |
| 1411 | if err == nil { |
| 1412 | err = googleapi.CheckResponse(res) |
| 1413 | if err != nil { |
| 1414 | _ = res.Body.Close() // ignore error |
| 1415 | } |
| 1416 | } |
| 1417 | return shouldRetry(ctx, err) |
| 1418 | }) |
| 1419 | if err != nil { |
| 1420 | return nil, err |
| 1421 | } |
| 1422 | _, isRanging := req.Header["Range"] |
| 1423 | if !(res.StatusCode == http.StatusOK || (isRanging && res.StatusCode == http.StatusPartialContent)) { |
| 1424 | _ = res.Body.Close() // ignore error |
| 1425 | return nil, fmt.Errorf("bad response: %d: %s", res.StatusCode, res.Status) |
| 1426 | } |
| 1427 | return res.Body, nil |
| 1428 | } |
| 1429 | |
| 1430 | // Update the object with the contents of the io.Reader, modTime and size |
| 1431 | // |
nothing calls this directly
no test coverage detected