From 65410d3210e2dc09c0e4818efb898434ca9e7504 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 9 May 2022 15:24:42 +0200 Subject: [PATCH] check if key exists --- internal/client/client.go | 9 ++++++++- internal/s3/minio.go | 15 +++++++++++++++ internal/s3/s3.go | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/internal/client/client.go b/internal/client/client.go index 62954b5..f3c826c 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -66,7 +66,14 @@ func (c *Client) CreateShare(ctx context.Context, key string) (*types.Share, err Key: key, } - // TODO: check if key exists + exists, err := c.s3.KeyExists(ctx, key) + if err != nil { + return nil, err + } + + if !exists { + return nil, errors.New("key does not exist") + } err = c.db.CreateShare(ctx, share) if err != nil { diff --git a/internal/s3/minio.go b/internal/s3/minio.go index 0c80718..323008c 100644 --- a/internal/s3/minio.go +++ b/internal/s3/minio.go @@ -39,3 +39,18 @@ func (m *minioClient) GetObject(ctx context.Context, key string) (ObjectReader, return object, nil } + +func (m *minioClient) KeyExists(ctx context.Context, key string) (bool, error) { + _, err := m.client.StatObject(ctx, m.bucket, key, minio.GetObjectOptions{}) + if err != nil { + errResponse := minio.ToErrorResponse(err) + + if errResponse.Code == "NoSuchKey" { + return false, nil + } + + return false, err + } + + return true, nil +} diff --git a/internal/s3/s3.go b/internal/s3/s3.go index 8da9a9f..c59a67f 100644 --- a/internal/s3/s3.go +++ b/internal/s3/s3.go @@ -14,4 +14,5 @@ type ObjectReader interface { type S3 interface { GetObject(ctx context.Context, key string) (ObjectReader, error) + KeyExists(ctx context.Context, key string) (bool, error) }