package space import ( "context" "errors" "fmt" "io" "strings" "github.com/minio/minio-go" ) var ErrBucketNotFound = errors.New("bucket not found") var ErrFileTooBig = errors.New("file is too big") var ErrNotEnabled = errors.New("s3 connection not enabled") var ErrDifferentURLRoot = errors.New("url root in path is different from current") type Space struct { bucket string urlRoot string root string client *minio.Client maxSize int64 } func Connect(host, accessKey, secretKey, bucket string, secure bool, maxSize int64, rootDirectory, urlRoot string) (*Space, error) { client, err := minio.New(host, accessKey, secretKey, secure) if err != nil { return nil, err } exists, err := client.BucketExists(bucket) if err != nil { return nil, err } if !exists { return nil, ErrBucketNotFound } if urlRoot == "" { urlRoot = fmt.Sprintf("https://%s.%s/%s/", bucket, host, rootDirectory) } else if !strings.HasSuffix(urlRoot, "/") { urlRoot += "/" } space := &Space{ client: client, bucket: bucket, urlRoot: urlRoot, maxSize: maxSize, root: rootDirectory, } return space, nil } // UploadFile uploads the file to the space. This does not do any checks on it, so the endpoints should // ensure that's all okay. func (space *Space) UploadFile(ctx context.Context, name string, mimeType string, reader io.Reader, size int64) error { if space == nil { return ErrNotEnabled } if size > space.maxSize { return ErrFileTooBig } _, err := space.client.PutObjectWithContext(ctx, space.bucket, space.root+"/"+name, reader, size, minio.PutObjectOptions{ ContentType: mimeType, UserMetadata: map[string]string{ "x-amz-acl": "public-read", }, }) if err != nil { return err } _, err = space.client.StatObject(space.bucket, space.root+"/"+name, minio.StatObjectOptions{}) if err != nil { return err } return nil } // RemoveFile removes a file from the space func (space *Space) RemoveFile(ctx context.Context, name string) error { if space == nil { return ErrNotEnabled } ch := make(chan string, 2) ch <- space.root + "/" + name close(ch) err := <-space.client.RemoveObjectsWithContext(ctx, space.bucket, ch) return err.Err } // DownloadFile opens a file for download, using the same path format as the UploadFile function. Remember to Close it! func (space *Space) DownloadFile(ctx context.Context, path string) (io.ReadCloser, error) { if space == nil { return nil, ErrNotEnabled } return space.client.GetObjectWithContext(ctx, space.bucket, space.root+"/"+path, minio.GetObjectOptions{}) } // URLFromPath gets the URL from the path returned by UploadFile func (space *Space) URLFromPath(path string) string { if space == nil { return "" } return space.urlRoot + path } // URLFromPath gets the URL from the path returned by UploadFile func (space *Space) PathFromURL(url string) (string, error) { if space == nil { return "", ErrNotEnabled } if !strings.HasPrefix(url, space.urlRoot) { return "", ErrDifferentURLRoot } return url[len(space.urlRoot):], nil }