DynamoDB


aws/aws-sdk-go-v2를 사용하는 DynamoDB 스토리지 드라이버입니다.

참고: 자격 증명의 설정 필드가 주어지지 않으면, 자격 증명은 환경 변수, ~/.aws/credentials, 또는 EC2 인스턴스 역할에서 가져옵니다. 자격 증명의 설정 필드가 주어지면, 주어진 설정에서 자격 증명을 가져옵니다. 자격 증명 지정을 참조하세요.

참고: Go 1.19 이상이 필요합니다

Table of Contents

  • Signatures

  • Installation

  • Examples

  • Config

  • Default Config

Signatures

func New(config Config) Storage


func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error  
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() *awsdynamodb.Client

Installation

DynamoDB는 모듈을 지원하는 2개의 최신 Go 버전에서 테스트되었습니다. 아직 초기화하지 않았다면, 다음을 실행하여 먼저 초기화하세요:

go mod init github.com/<user>/<repo>

그리고 dynamodb 구현을 설치하세요:

go get github.com/gofiber/storage/dynamodb/v2

Examples

스토리지 패키지를 가져옵니다.

import "github.com/gofiber/storage/dynamodb/v2"  

다음과 같은 방법으로 스토리지를 생성할 수 있습니다:

// dynamodb 초기화
store := dynamodb.New(dynamodb.Config{

})  

Config

type Config struct {
	// 사용할 DynamoDB 서비스의 리전입니다. 
	// 유효한 값: https://docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region.
	// 예: "us-west-2".
	// 선택 사항 (설정되지 않은 경우 공유 설정 파일이나 환경 변수에서 읽음).
	// 환경 변수: "AWS_REGION".
	Region string

	// DynamoDB 테이블의 이름입니다.
	// 선택 사항 ("fiber_storage" 기본값).  
	Table string

	// CustomEndpoint를 사용하면 사용자 정의 DynamoDB 서비스 엔드포인트를 설정할 수 있습니다.
	// 이는 로컬 테스트를 위해 "DynamoDB local" Docker 컨테이너를 실행할 때 특히 유용합니다. 
	// Docker 컨테이너의 일반적인 값: "http://localhost:8000".
	// https://hub.docker.com/r/amazon/dynamodb-local/ 참조.
	// 선택 사항 ("" 기본값).
	Endpoint string 

	// Credentials는 AWS 액세스 키와 AWS 비밀 액세스 키를 오버라이드합니다. 권장되지 않습니다.
	//  
	// 선택 사항. 기본값은 Credentials{} 입니다.
	Credentials Credentials

	// 재시도 가능한 실패를 만나는 요청을 시도해야 하는 최대 횟수입니다.
	// 
	// 선택 사항. 기본값은 3입니다.
	MaxAttempts int

	// Reset은 기존 Bucket의 기존 키를 모두 삭제합니다.  
	//
	// 선택 사항. 기본값은 false입니다.
	Reset bool

	// 테이블의 ReadCapacityUnits입니다.
	// 테이블이 아직 없고 gokv에 의해 생성될 때만 필요합니다.
	// 선택 사항 (웹 콘솔에서 테이블을 생성할 때의 기본값과 동일한 5가 기본값임)  
	// 무료 계층에는 25 RCU가 포함됩니다 (모든 테이블에 걸쳐서).
	// 계산 예는 https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput 참조.
	// 제한은 https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput 참조.
	ReadCapacityUnits int64

	// 테이블의 WriteCapacityUnits입니다.  
	// 테이블이 아직 없고 gokv에 의해 생성될 때만 필요합니다.
	// 선택 사항 (웹 콘솔에서 테이블을 생성할 때의 기본값과 동일한 5가 기본값임)
	// 무료 계층에는 25 WCU가 포함됩니다 (모든 테이블에 걸쳐서).
	// 계산 예는 https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput 참조.
	// 제한은 https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput 참조. 
	WriteCapacityUnits int64

	// 테이블이 아직 없는 경우, gokv가 생성합니다.
	// WaitForTableCreation이 true이면, gokv는 15초의 타임아웃으로 테이블이 생성될 때까지 대기합니다.
	// 15초 후에도 테이블이 존재하지 않으면 오류가 반환됩니다.
	// WaitForTableCreation이 false이면, gokv는 클라이언트를 즉시 반환합니다.  
	// 후자의 경우 테이블이 생성되기 전에 테이블에서 읽거나 쓰면 안 되는데,
	// 그렇지 않으면 ResourceNotFoundException 오류가 발생하기 때문입니다.
	// 선택 사항 (기본값은 true).
	WaitForTableCreation *bool
}

type Credentials struct {
	AccessKey       string 
	SecretAccessKey string
}

Default Config

var ConfigDefault = Config{
	Table:                "fiber_storage",
	Credentials:          Credentials{},  
	MaxAttempts:          3,
	Reset:                false,
	ReadCapacityUnits:    5,
	WriteCapacityUnits:   5, 
	WaitForTableCreation: aws.Bool(true),
}

Last updated