web-dev-qa-db-ja.com

io.WriterAtを実行中のバッファー

Aws-sdkを使用してs3バケットからファイルをダウンロードしています。 S3ダウンロード関数はio.WriterAtを実装するものですが、bytes.Bufferはそれを実装していません。現在、io.WriterAtを実装するファイルを作成していますが、メモリに何かを入れたいです。

15
Onestay

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()))
32
Sam

標準ライブラリでこれを行う方法はわかりませんが、独自のバッファを作成できます。

それは本当にそれほど難しくはないでしょう...

編集:私はこれについて考えるのをやめることができませんでした、そして私はすべてのものを酸っぱくしてしまいました、楽しんでください:)

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
}
3

Io.Writerを使用したAWS WriterAtの偽装

これは元の質問に対する直接の回答ではなく、ここに着陸した後に私が実際に使用した解決策です。私が他の人を助けるかもしれないと考えるのは、同様のユースケースです。

AWSのドキュメントでは、downloader.Concurrency〜1の場合、シーケンシャル書き込みが保証されます。

downloader.Download(FakeWriterAt{w}, s3.GetObjectInput{
    Bucket: aws.String(bucket),
    Key:    aws.String(key),
})
downloader.Concurrency = 1

したがって、io.Writerio.WriterAt、不要になったoffsetを破棄します。

type FakeWriterAt struct {
    w io.Writer
}

func (fw FakeWriterAt) WriteAt(p []byte, offset int64) (n int, err error) {
    // ignore 'offset' because we forced sequential downloads
    return fw.w.Write(p)
}
3
CoolAJ86