我使用aws-sdk从s3存储区下载文件。 S3下载功能需要的东西,实现io.WriterAt然而bytes.Buffer没有实现。现在我正在创建一个实现io.WriterAt的文件,但是我想要一些内存中的东西。执行io.WriterAt的缓冲区go
0
A
回答
1
我不知道有什么办法在标准库中做到这一点,但你可以编写自己的缓冲区。
这真的不会那么难......
编辑:我不能停止思考过这个问题,我结束了acidentally整个事情,享受:)
package main
import (
"errors"
"fmt"
)
func main() {
buff := NewWriteBuffer(0, 10)
buff.WriteAt([]byte("abc"), 5)
fmt.Printf("%#v\n", buff)
}
// WriteBuffer is a simple type that implements io.WriterAt on an in-memory buffer.
// The zero value of this type is an empty buffer ready to use.
type WriteBuffer struct {
d []byte
m int
}
// NewWriteBuffer creates and returns a new WriteBuffer with the given initial size and
// maximum. If maximum is <= 0 it is unlimited.
func NewWriteBuffer(size, max int) *WriteBuffer {
if max < size && max >= 0 {
max = size
}
return &WriteBuffer{make([]byte, size), max}
}
// SetMax sets the maximum capacity of the WriteBuffer. If the provided maximum is lower
// than the current capacity but greater than 0 it is set to the current capacity, if
// less than or equal to zero it is unlimited..
func (wb *WriteBuffer) SetMax(max int) {
if max < len(wb.d) && max >= 0 {
max = len(wb.d)
}
wb.m = max
}
// Bytes returns the WriteBuffer's underlying data. This value will remain valid so long
// as no other methods are called on the WriteBuffer.
func (wb *WriteBuffer) Bytes() []byte {
return wb.d
}
// Shape returns the current WriteBuffer size and its maximum if one was provided.
func (wb *WriteBuffer) Shape() (int, int) {
return len(wb.d), wb.m
}
func (wb *WriteBuffer) WriteAt(dat []byte, off int64) (int, error) {
// Range/sanity checks.
if int(off) < 0 {
return 0, errors.New("Offset out of range (too small).")
}
if int(off)+len(dat) >= wb.m && wb.m > 0 {
return 0, errors.New("Offset+data length out of range (too large).")
}
// Check fast path extension
if int(off) == len(wb.d) {
wb.d = append(wb.d, dat...)
return len(dat), nil
}
// Check slower path extension
if int(off)+len(dat) >= len(wb.d) {
nd := make([]byte, int(off)+len(dat))
copy(nd, wb.d)
wb.d = nd
}
// Once no extension is needed just copy bytes into place.
copy(wb.d[int(off):], dat)
return len(dat), nil
}
0
对于涉及AWS SDK的案例,请使用aws.WriteAtBuffer
将S3对象下载到内存中。
requestInput := s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
buf := aws.NewWriteAtBuffer([]byte{})
downloader.Download(buf, &requestInput)
fmt.Printf("Downloaded %v bytes", len(buf.Bytes()))
相关问题
- 1. python.el shell并执行缓冲区
- 2. Rxjs缓冲区执行反压
- 3. vc6 ado执行缓冲区溢出
- 4. Go +协议缓冲区的行为真的很奇怪
- 5. 收集缓冲区的行
- 6. 缓冲区溢出缓冲区长度
- 7. 帧缓冲区/颜色缓冲区?
- 8. 动态缓冲区行为
- 9. fgets的冲洗缓冲区
- 10. 区分Vim中的隐藏缓冲区和活动缓冲区
- 11. PY-执行行发送整个缓冲区蟒蛇过程
- 12. 在缓冲区
- 13. 与缓冲区
- 14. Ext.direct缓冲区
- 15. Go中的HTTP ResponseWriter的写入函数缓冲区是否会被缓存?
- 16. 如何分配一个可执行的内存缓冲区?
- 17. Windows上的gVIM:使用空格执行缓冲区和路径
- 18. C缓冲区中的指令仅作为sudo执行
- 19. 堆栈缓冲区溢出导致的奇怪执行路径
- 20. 的memcpy从一个char *缓冲区一个wchar_t的*缓冲区
- 21. 使cv mat的缓冲区指向QImage的缓冲区
- 22. 防止冲洗缓冲区
- 23. Perl缓冲区冲洗
- 24. 按行读取缓冲区行内容
- 25. 限制缓冲区缓存
- 26. 带缓冲区的并行UART输出
- 27. 缓冲区没有正确的行为
- 28. 清除串行端口的缓冲区
- 29. node.js缓冲区奇怪的行为
- 30. 所有emacs缓冲区中的行号