3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GoでAmazon S3をKVSとして使う

Last updated at Posted at 2019-04-15

これはなに

永続化が必要なバイナリKVSとしてAmazon S3を使いたい場合があります。
セッションの取り回しや、aws.WriteAtBufferやエラーがトリッキーなのでスニペットです。

2020-12追記:
S3に強い一貫性がサポートされましたね!

package s3store

import (
        "bytes"

        "github.com/aws/aws-sdk-go/aws"
        "github.com/aws/aws-sdk-go/aws/awserr"
        "github.com/aws/aws-sdk-go/aws/session"
        "github.com/aws/aws-sdk-go/service/s3"
        "github.com/aws/aws-sdk-go/service/s3/s3manager"
)

type S3Store struct {
        Bucket     string
        Uploader   *s3manager.Uploader
        Downloader *s3manager.Downloader
}

func NewS3Store(bucket, region string) (*S3Store, error) {
        s := new(S3Store)
        s.Bucket = bucket

        sess, err := session.NewSession(&aws.Config{
                Region: aws.String(region),
        })
        if err != nil {
                return nil, err
        }

        s.Uploader = s3manager.NewUploader(sess)
        s.Downloader = s3manager.NewDownloader(sess)

        return s, nil
}

func (s *S3Store) Set(key string, body []byte) (err error) {
        params := &s3manager.UploadInput{
                Bucket: aws.String(s.Bucket),
                Key:    aws.String(key),
                Body:   bytes.NewReader(body),
        }

        _, err = s.Uploader.Upload(params)
        return
}

func (s *S3Store) Get(key string) ([]byte, error) {
        buffer := aws.NewWriteAtBuffer([]byte{})

        _, err := s.Downloader.Download(buffer, &s3.GetObjectInput{
                Bucket: aws.String(s.Bucket),
                Key:    aws.String(key),
        })
        if err != nil {
                if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "NoSuchKey" {
                        return nil, nil
                }
                return nil, err
        }

        return buffer.Bytes(), nil
}
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?