Skip to content

Commit

Permalink
feat: new S3 method to check if key exists (#116)
Browse files Browse the repository at this point in the history
* new KeyExists() method

Signed-off-by: Julie Vogelman <[email protected]>

* fix: KeyExists() to handle errors correctly

Signed-off-by: Julie Vogelman <[email protected]>

* fix: include stack in error, for consistency with other errors

Signed-off-by: Julie Vogelman <[email protected]>
  • Loading branch information
juliev0 authored Jun 21, 2022
1 parent 671f292 commit 15e61a5
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type S3Client interface {
// OpenFile opens a file for much lower disk and memory usage that GetFile
OpenFile(bucket, key string) (io.ReadCloser, error)

// KeyExists checks if object exists (and if we have permission to access)
KeyExists(bucket, key string) (bool, error)

// Delete deletes the key from the bucket
Delete(bucket, key string) error

// GetDirectory downloads a directory to a local file path
Expand All @@ -45,7 +49,7 @@ type S3Client interface {
// ListDirectory list the contents of a directory/bucket
ListDirectory(bucket, keyPrefix string) ([]string, error)

// IsDirectory tests if the key is acting like a s3 directory
// IsDirectory tests if the key is acting like an s3 directory
IsDirectory(bucket, key string) (bool, error)

// BucketExists returns whether a bucket exists
Expand Down Expand Up @@ -279,6 +283,26 @@ func (s *s3client) OpenFile(bucket, key string) (io.ReadCloser, error) {
return f, nil
}

// checks if object exists (and if we have permission to access)
func (s *s3client) KeyExists(bucket, key string) (bool, error) {
log.WithFields(log.Fields{"endpoint": s.Endpoint, "bucket": bucket, "key": key}).Info("Checking key exists from s3")

encOpts, err := s.EncryptOpts.buildServerSideEnc(bucket, key)
if err != nil {
return false, errors.WithStack(err)
}

_, err = s.minioClient.StatObject(s.ctx, bucket, key, minio.StatObjectOptions{ServerSideEncryption: encOpts})
if err == nil {
return true, nil
}
if IsS3ErrCode(err, "NoSuchKey") {
return false, nil
}

return false, errors.WithStack(err)
}

func (s *s3client) Delete(bucket, key string) error {
log.WithFields(log.Fields{"endpoint": s.Endpoint, "bucket": bucket, "key": key}).Info("Deleting object from s3")
return s.minioClient.RemoveObject(s.ctx, bucket, key, minio.RemoveObjectOptions{})
Expand Down

0 comments on commit 15e61a5

Please sign in to comment.