S3 GetObject方法返回内容,但从body中读取时未填充缓冲区。

huangapple go评论83阅读模式
英文:

S3 GetObject returns content but reading from the body does not fill the buffer

问题

我正在使用Go SDK从S3获取一个文件。
尽管响应中指示了ContentLength,但实际从body中读取的内容并没有填充到我的缓冲区中。

响应:

{
AcceptRanges: "bytes",
Body: buffer(0xc0421d6cc0),
ContentEncoding: "utf-8",
ContentLength: 13,
ContentType: "application/json",
ETag: "\"a15ce1024b219fd76684ba1561d23ccc\"",
LastModified: 2017-02-09 15:16:45 +0000 UTC,
Metadata: {
}
}

代码:

var buf []byte

r, err := resp.Body.Read(buf)
if err != nil {
    return "", err
}
defer resp.Body.Close()

fmt.Printf("read %d, buff %d", r, len(buf))

输出:"read 0, buff 0"

英文:

i am getting a file from s3 using the go sdk.
Although the response indicate a ContentLength the actual read from the body does not fill my buffer.

response

{
AcceptRanges: "bytes",
Body: buffer(0xc0421d6cc0),
ContentEncoding: "utf-8",
ContentLength: 13,
ContentType: "application/json",
ETag: "\"a15ce1024b219fd76684ba1561d23ccc\"",
LastModified: 2017-02-09 15:16:45 +0000 UTC,
Metadata: {
}
}

code

var buf []byte

r, err := resp.Body.Read(buf)
if err != nil {
	return "", err
}
defer resp.Body.Close()

fmt.Printf("read %d, buff %d", r, len(buf))

"read 0, buff 0"

答案1

得分: 4

io.Reader 需要一个切片来读取数据。如果你不提供任何缓冲空间,将不会读取任何内容。

如果你想将整个流读取到内存中,可以使用 ioutil.ReadAll 这个便利函数。

英文:

An io.Reader requires a slice to read into. If you don't provide any buffer space, nothing will be read.

If you want to read the entire stream into memory, you can use the ioutil.ReadAll convenience function.

huangapple
  • 本文由 发表于 2017年2月9日 23:31:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/42140713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定