| 1 |
// Copyright 2022 The Archivista Contributors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package blobstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"fmt" |
| 21 |
"github.com/minio/minio-go/v7" |
| 22 |
"io" |
| 23 |
|
| 24 |
"github.com/minio/minio-go/v7/pkg/credentials" |
| 25 |
) |
| 26 |
|
| 27 |
type Store struct { |
| 28 |
client *minio.Client |
| 29 |
bucket string |
| 30 |
location string |
| 31 |
} |
| 32 |
|
| 33 |
// PutBlob stores the attestation blob into the backend store |
| 34 |
func (store *Store) PutBlob(ctx context.Context, idx string, obj []byte) error { |
| 35 |
opt := minio.PutObjectOptions{} |
| 36 |
size := int64(len(obj)) |
| 37 |
n, err := store.client.PutObject(ctx, store.bucket, idx, bytes.NewReader(obj), size, opt) |
| 38 |
if err != nil { |
| 39 |
return fmt.Errorf("failed to put blob: %v", err) |
| 40 |
} else if n.Size != size { |
| 41 |
return fmt.Errorf("failed to upload full blob: size %d != uploaded size %d", size, n.Size) |
| 42 |
} |
| 43 |
return nil |
| 44 |
} |
| 45 |
|
| 46 |
// New returns a reader/writer for storing/retrieving attestations |
| 47 |
func New(ctx context.Context, endpoint string, creds *credentials.Credentials, bucketName string, useTLS bool) (*Store, <-chan error, error) { |
| 48 |
errCh := make(chan error) |
| 49 |
go func() { |
| 50 |
<-ctx.Done() |
| 51 |
close(errCh) |
| 52 |
}() |
| 53 |
|
| 54 |
c, err := minio.New(endpoint, &minio.Options{ |
| 55 |
Creds: creds, |
| 56 |
Secure: useTLS, |
| 57 |
}) |
| 58 |
if err != nil { |
| 59 |
return nil, errCh, err |
| 60 |
} |
| 61 |
|
| 62 |
exists, err := c.BucketExists(ctx, bucketName) |
| 63 |
if !exists || err != nil { |
| 64 |
return nil, errCh, fmt.Errorf("failed to find bucket exists: %v", err) |
| 65 |
} |
| 66 |
|
| 67 |
loc, err := c.GetBucketLocation(ctx, bucketName) |
| 68 |
if err != nil { |
| 69 |
return nil, errCh, err |
| 70 |
} |
| 71 |
|
| 72 |
return &Store{ |
| 73 |
client: c, |
| 74 |
location: loc, |
| 75 |
bucket: bucketName, |
| 76 |
}, errCh, nil |
| 77 |
} |
| 78 |
|
| 79 |
func (s *Store) Get(ctx context.Context, gitoid string) (io.ReadCloser, error) { |
| 80 |
return s.client.GetObject(ctx, s.bucket, gitoid, minio.GetObjectOptions{}) |
| 81 |
} |
| 82 |
|
| 83 |
func (s *Store) Store(ctx context.Context, gitoid string, payload []byte) error { |
| 84 |
return s.PutBlob(ctx, gitoid, payload) |
| 85 |
} |