Source File
reader.go
Belonging Package
cloud.google.com/go/storage
package storage
import (
)
var crc32cTable = crc32.MakeTable(crc32.Castagnoli)
func ( *ObjectHandle) ( context.Context) (*Reader, error) {
return .NewRangeReader(, 0, -1)
}
func ( *ObjectHandle) ( context.Context, , int64) ( *Reader, error) {
= trace.StartSpan(, "cloud.google.com/go/storage.Object.NewRangeReader")
defer func() { trace.EndSpan(, ) }()
if := .validate(); != nil {
return nil,
}
if < 0 && >= 0 {
return nil, fmt.Errorf("storage: invalid offset %d < 0 requires negative length", )
}
if .conds != nil {
if := .conds.validate("NewRangeReader"); != nil {
return nil,
}
}
:= &url.URL{
Scheme: .c.scheme,
Host: .c.readHost,
Path: fmt.Sprintf("/%s/%s", .bucket, .object),
}
:= "GET"
if == 0 {
= "HEAD"
}
, := http.NewRequest(, .String(), nil)
if != nil {
return nil,
}
= .WithContext()
if .userProject != "" {
.Header.Set("X-Goog-User-Project", .userProject)
}
if .readCompressed {
.Header.Set("Accept-Encoding", "gzip")
}
if := setEncryptionHeaders(.Header, .encryptionKey, false); != nil {
return nil,
}
:= .gen
.URL.RawQuery = conditionsQuery(, .conds)
var *http.Response
= runWithRetry(, func() error {
, = .c.hc.Do()
if != nil {
return
}
if .StatusCode == http.StatusNotFound {
.Body.Close()
return ErrObjectNotExist
}
if .StatusCode < 200 || .StatusCode > 299 {
, := ioutil.ReadAll(.Body)
.Body.Close()
return &googleapi.Error{
Code: .StatusCode,
Header: .Header,
Body: string(),
}
}
:=
!decompressiveTranscoding() &&
> 0 && != 0 &&
.StatusCode != http.StatusPartialContent
if {
.Body.Close()
return errors.New("storage: partial request not satisfied")
}
if < 0 && .Header.Get("X-Goog-Generation") != "" {
, := strconv.ParseInt(.Header.Get("X-Goog-Generation"), 10, 64)
if != nil {
return
}
=
}
return nil
})
if != nil {
return nil,
}
return , nil
}
, := (0)
if != nil {
return nil,
}
var (
int64 // total size of object, even if a range was requested.
bool
uint32
int64 // non-zero if range request.
)
if .StatusCode == http.StatusPartialContent {
:= strings.TrimSpace(.Header.Get("Content-Range"))
if !strings.HasPrefix(, "bytes ") || !strings.Contains(, "/") {
return nil, fmt.Errorf("storage: invalid Content-Range %q", )
}
, = strconv.ParseInt([strings.LastIndex(, "/")+1:], 10, 64)
if != nil {
return nil, fmt.Errorf("storage: invalid Content-Range %q", )
}
:= strings.Index(, "-")
if >= 0 {
, = strconv.ParseInt([len("bytes="):], 10, 64)
if != nil {
return nil, fmt.Errorf("storage: invalid Content-Range %q: %v", , )
}
}
} else {
if != 0 && !.Uncompressed && !uncompressedByServer() {
, = parseCRC32c()
}
}
:= .ContentLength
:= .Body
if == 0 {
= 0
.Close()
= emptyBody
}
var int64
if .Header.Get("X-Goog-Metageneration") != "" {
, = strconv.ParseInt(.Header.Get("X-Goog-Metageneration"), 10, 64)
if != nil {
return nil,
}
}
var time.Time
if .Header.Get("Last-Modified") != "" {
, = http.ParseTime(.Header.Get("Last-Modified"))
if != nil {
return nil,
}
}
:= ReaderObjectAttrs{
Size: ,
ContentType: .Header.Get("Content-Type"),
ContentEncoding: .Header.Get("Content-Encoding"),
CacheControl: .Header.Get("Cache-Control"),
LastModified: ,
StartOffset: ,
Generation: ,
Metageneration: ,
}
return &Reader{
Attrs: ,
body: ,
size: ,
remain: ,
wantCRC: ,
checkCRC: ,
reopen: ,
}, nil
}
return .Header.Get("X-Goog-Stored-Content-Encoding") == "gzip" &&
.Header.Get("Content-Encoding") != "gzip"
}
func ( *http.Response) (uint32, bool) {
const = "crc32c="
for , := range .Header["X-Goog-Hash"] {
if strings.HasPrefix(, ) {
, := decodeUint32([len():])
if == nil {
return , true
}
}
}
return 0, false
}
var emptyBody = ioutil.NopCloser(strings.NewReader(""))
func ( *Reader) () string {
return .Attrs.ContentType
}
func ( *Reader) () string {
return .Attrs.ContentEncoding
}
func ( *Reader) () string {
return .Attrs.CacheControl
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |