英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论