2017-11-22 15:12:10 -05:00
|
|
|
/*
|
2018-03-26 12:11:39 -04:00
|
|
|
* Minio Cloud Storage, (C) 2017, 2018 Minio, Inc.
|
2017-11-22 15:12:10 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
package sia
|
2017-11-22 15:12:10 -05:00
|
|
|
|
|
|
|
import (
|
2017-11-28 03:10:44 -05:00
|
|
|
"bytes"
|
2018-03-14 15:01:47 -04:00
|
|
|
"context"
|
2017-11-28 03:10:44 -05:00
|
|
|
"encoding/hex"
|
2017-11-22 15:12:10 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2017-12-05 20:58:09 -05:00
|
|
|
"path"
|
2017-11-22 15:12:10 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
|
|
|
"github.com/fatih/color"
|
2017-11-22 15:12:10 -05:00
|
|
|
"github.com/minio/cli"
|
|
|
|
"github.com/minio/minio-go/pkg/set"
|
2017-12-05 20:58:09 -05:00
|
|
|
minio "github.com/minio/minio/cmd"
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2017-12-05 20:58:09 -05:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
2017-11-22 15:12:10 -05:00
|
|
|
"github.com/minio/minio/pkg/hash"
|
2018-08-06 21:00:10 -04:00
|
|
|
"github.com/minio/sha256-simd"
|
2017-11-22 15:12:10 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
siaBackend = "sia"
|
|
|
|
)
|
|
|
|
|
|
|
|
type siaObjects struct {
|
2017-12-05 20:58:09 -05:00
|
|
|
minio.GatewayUnsupported
|
2017-11-22 15:12:10 -05:00
|
|
|
Address string // Address and port of Sia Daemon.
|
|
|
|
TempDir string // Temporary storage location for file transfers.
|
|
|
|
RootDir string // Root directory to store files on Sia.
|
|
|
|
password string // Sia password for uploading content in authenticated manner.
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
const siaGatewayTemplate = `NAME:
|
|
|
|
{{.HelpName}} - {{.Usage}}
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
{{.HelpName}} {{if .VisibleFlags}}[FLAGS]{{end}} [SIA_DAEMON_ADDR]
|
|
|
|
{{if .VisibleFlags}}
|
|
|
|
FLAGS:
|
|
|
|
{{range .VisibleFlags}}{{.}}
|
|
|
|
{{end}}{{end}}
|
|
|
|
ENVIRONMENT VARIABLES: (Default values in parenthesis)
|
|
|
|
ACCESS:
|
|
|
|
MINIO_ACCESS_KEY: Custom access key (Do not reuse same access keys on all instances)
|
|
|
|
MINIO_SECRET_KEY: Custom secret key (Do not reuse same secret keys on all instances)
|
|
|
|
|
2017-12-15 15:33:42 -05:00
|
|
|
BROWSER:
|
|
|
|
MINIO_BROWSER: To disable web browser access, set this value to "off".
|
|
|
|
|
2018-05-21 14:11:57 -04:00
|
|
|
DOMAIN:
|
|
|
|
MINIO_DOMAIN: To enable virtual-host-style requests, set this value to Minio host domain name.
|
|
|
|
|
2018-03-28 17:14:06 -04:00
|
|
|
CACHE:
|
2018-04-02 00:55:09 -04:00
|
|
|
MINIO_CACHE_DRIVES: List of mounted drives or directories delimited by ";".
|
2018-03-29 16:29:21 -04:00
|
|
|
MINIO_CACHE_EXCLUDE: List of cache exclusion patterns delimited by ";".
|
|
|
|
MINIO_CACHE_EXPIRY: Cache expiry duration in days.
|
2018-06-25 13:24:12 -04:00
|
|
|
MINIO_CACHE_MAXUSE: Maximum permitted usage of the cache in percentage (0-100).
|
2018-03-28 17:14:06 -04:00
|
|
|
|
2017-11-22 15:12:10 -05:00
|
|
|
SIA_TEMP_DIR: The name of the local Sia temporary storage directory. (.sia_temp)
|
|
|
|
SIA_API_PASSWORD: API password for Sia daemon. (default is empty)
|
|
|
|
|
|
|
|
EXAMPLES:
|
|
|
|
1. Start minio gateway server for Sia backend.
|
2018-05-21 14:11:57 -04:00
|
|
|
$ {{.HelpName}}
|
2018-03-28 17:14:06 -04:00
|
|
|
|
|
|
|
2. Start minio gateway server for Sia backend with edge caching enabled.
|
2018-05-21 14:11:57 -04:00
|
|
|
$ export MINIO_CACHE_DRIVES="/mnt/drive1;/mnt/drive2;/mnt/drive3;/mnt/drive4"
|
|
|
|
$ export MINIO_CACHE_EXCLUDE="bucket1/*;*.png"
|
|
|
|
$ export MINIO_CACHE_EXPIRY=40
|
2018-06-25 13:24:12 -04:00
|
|
|
$ export MINIO_CACHE_MAXUSE=80
|
2018-05-21 14:11:57 -04:00
|
|
|
$ {{.HelpName}}
|
2017-11-22 15:12:10 -05:00
|
|
|
`
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
minio.RegisterGatewayCommand(cli.Command{
|
2017-11-22 15:12:10 -05:00
|
|
|
Name: siaBackend,
|
|
|
|
Usage: "Sia Decentralized Cloud.",
|
|
|
|
Action: siaGatewayMain,
|
|
|
|
CustomHelpTemplate: siaGatewayTemplate,
|
|
|
|
HideHelpCommand: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for 'minio gateway sia' command line.
|
|
|
|
func siaGatewayMain(ctx *cli.Context) {
|
|
|
|
// Validate gateway arguments.
|
|
|
|
host := ctx.Args().First()
|
|
|
|
// Validate gateway arguments.
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.FatalIf(minio.ValidateGatewayArguments(ctx.GlobalString("address"), host), "Invalid argument")
|
2017-11-22 15:12:10 -05:00
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
minio.StartGateway(ctx, &Sia{host})
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
// Sia implements Gateway.
|
|
|
|
type Sia struct {
|
2017-11-22 15:12:10 -05:00
|
|
|
host string // Sia daemon host address
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements Gateway interface.
|
2017-12-05 20:58:09 -05:00
|
|
|
func (g *Sia) Name() string {
|
2017-11-22 15:12:10 -05:00
|
|
|
return siaBackend
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
// NewGatewayLayer returns Sia gateway layer, implements ObjectLayer interface to
|
2017-12-05 20:58:09 -05:00
|
|
|
// talk to Sia backend.
|
2018-02-09 18:19:30 -05:00
|
|
|
func (g *Sia) NewGatewayLayer(creds auth.Credentials) (minio.ObjectLayer, error) {
|
2017-12-05 20:58:09 -05:00
|
|
|
sia := &siaObjects{
|
|
|
|
Address: g.host,
|
|
|
|
// RootDir uses access key directly, provides partitioning for
|
|
|
|
// concurrent users talking to same sia daemon.
|
|
|
|
RootDir: creds.AccessKey,
|
|
|
|
TempDir: os.Getenv("SIA_TEMP_DIR"),
|
|
|
|
password: os.Getenv("SIA_API_PASSWORD"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Address not provided on command line or ENV, default to:
|
|
|
|
if sia.Address == "" {
|
|
|
|
sia.Address = "127.0.0.1:9980"
|
|
|
|
}
|
|
|
|
|
|
|
|
// If local Sia temp directory not specified, default to:
|
|
|
|
if sia.TempDir == "" {
|
|
|
|
sia.TempDir = ".sia_temp"
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
sia.TempDir, err = filepath.Abs(sia.TempDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the temp directory with proper permissions.
|
|
|
|
// Ignore error when dir already exists.
|
|
|
|
if err = os.MkdirAll(sia.TempDir, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
colorBlue := color.New(color.FgBlue).SprintfFunc()
|
|
|
|
colorBold := color.New(color.Bold).SprintFunc()
|
|
|
|
|
2018-04-13 14:57:05 -04:00
|
|
|
formatStr := "%" + fmt.Sprintf("%ds", len(sia.Address)+7)
|
|
|
|
logger.StartupMessage(colorBlue("\nSia Configuration:"))
|
|
|
|
logger.StartupMessage(colorBlue(" API Address:") + colorBold(fmt.Sprintf(formatStr, sia.Address)))
|
|
|
|
logger.StartupMessage(colorBlue(" Staging Directory:") + colorBold(fmt.Sprintf(" %s", sia.TempDir)))
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
return sia, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Production - sia gateway is not ready for production use.
|
|
|
|
func (g *Sia) Production() bool {
|
|
|
|
return false
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// non2xx returns true for non-success HTTP status codes.
|
|
|
|
func non2xx(code int) bool {
|
|
|
|
return code < 200 || code > 299
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodeError returns the api.Error from a API response. This method should
|
|
|
|
// only be called if the response's status code is non-2xx. The error returned
|
|
|
|
// may not be of type api.Error in the event of an error unmarshalling the
|
|
|
|
// JSON.
|
|
|
|
type siaError struct {
|
|
|
|
// Message describes the error in English. Typically it is set to
|
|
|
|
// `err.Error()`. This field is required.
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s siaError) Error() string {
|
|
|
|
return s.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeError(resp *http.Response) error {
|
|
|
|
// Error is a type that is encoded as JSON and returned in an API response in
|
|
|
|
// the event of an error. Only the Message field is required. More fields may
|
|
|
|
// be added to this struct in the future for better error reporting.
|
|
|
|
var apiErr siaError
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return apiErr
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
// MethodNotSupported - returned if call returned error.
|
|
|
|
type MethodNotSupported struct {
|
2017-11-22 15:12:10 -05:00
|
|
|
method string
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
func (s MethodNotSupported) Error() string {
|
2017-11-22 15:12:10 -05:00
|
|
|
return fmt.Sprintf("API call not recognized: %s", s.method)
|
|
|
|
}
|
|
|
|
|
|
|
|
// apiGet wraps a GET request with a status code check, such that if the GET does
|
|
|
|
// not return 2xx, the error will be read and returned. The response body is
|
|
|
|
// not closed.
|
2018-04-05 18:04:40 -04:00
|
|
|
func apiGet(ctx context.Context, addr, call, apiPassword string) (*http.Response, error) {
|
2017-11-22 15:12:10 -05:00
|
|
|
req, err := http.NewRequest("GET", "http://"+addr+call, nil)
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return nil, err
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
req.Header.Set("User-Agent", "Sia-Agent")
|
|
|
|
if apiPassword != "" {
|
|
|
|
req.SetBasicAuth("", apiPassword)
|
|
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return nil, err
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
resp.Body.Close()
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, MethodNotSupported{call})
|
2017-12-05 20:58:09 -05:00
|
|
|
return nil, MethodNotSupported{call}
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
if non2xx(resp.StatusCode) {
|
|
|
|
err := decodeError(resp)
|
|
|
|
resp.Body.Close()
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2017-11-22 15:12:10 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// apiPost wraps a POST request with a status code check, such that if the POST
|
|
|
|
// does not return 2xx, the error will be read and returned. The response body
|
|
|
|
// is not closed.
|
2018-04-05 18:04:40 -04:00
|
|
|
func apiPost(ctx context.Context, addr, call, vals, apiPassword string) (*http.Response, error) {
|
2017-11-22 15:12:10 -05:00
|
|
|
req, err := http.NewRequest("POST", "http://"+addr+call, strings.NewReader(vals))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("User-Agent", "Sia-Agent")
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
if apiPassword != "" {
|
|
|
|
req.SetBasicAuth("", apiPassword)
|
|
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return nil, err
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
resp.Body.Close()
|
2017-12-05 20:58:09 -05:00
|
|
|
return nil, MethodNotSupported{call}
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if non2xx(resp.StatusCode) {
|
|
|
|
err := decodeError(resp)
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// post makes an API call and discards the response. An error is returned if
|
|
|
|
// the response status is not 2xx.
|
2018-04-05 18:04:40 -04:00
|
|
|
func post(ctx context.Context, addr, call, vals, apiPassword string) error {
|
|
|
|
resp, err := apiPost(ctx, addr, call, vals, apiPassword)
|
2017-11-22 15:12:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// list makes a lists all the uploaded files, decodes the json response.
|
2018-04-05 18:04:40 -04:00
|
|
|
func list(ctx context.Context, addr string, apiPassword string, obj *renterFiles) error {
|
|
|
|
resp, err := apiGet(ctx, addr, "/renter/files", apiPassword)
|
2017-11-22 15:12:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNoContent {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, fmt.Errorf("Expecting a response, but API returned %s", resp.Status))
|
2017-11-25 14:58:29 -05:00
|
|
|
return fmt.Errorf("Expecting a response, but API returned %s", resp.Status)
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(obj)
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// get makes an API call and discards the response. An error is returned if the
|
|
|
|
// responsee status is not 2xx.
|
2018-04-05 18:04:40 -04:00
|
|
|
func get(ctx context.Context, addr, call, apiPassword string) error {
|
|
|
|
resp, err := apiGet(ctx, addr, call, apiPassword)
|
2017-11-22 15:12:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown saves any gateway metadata to disk
|
|
|
|
// if necessary and reload upon next restart.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (s *siaObjects) Shutdown(ctx context.Context) error {
|
2017-11-22 15:12:10 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageInfo is not relevant to Sia backend.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (s *siaObjects) StorageInfo(ctx context.Context) (si minio.StorageInfo) {
|
2017-11-22 15:12:10 -05:00
|
|
|
return si
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeBucket creates a new container on Sia backend.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (s *siaObjects) MakeBucketWithLocation(ctx context.Context, bucket, location string) error {
|
2017-12-05 20:58:09 -05:00
|
|
|
srcFile := path.Join(s.TempDir, minio.MustGetUUID())
|
|
|
|
defer os.Remove(srcFile)
|
2017-11-28 03:10:44 -05:00
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
writer, err := os.Create(srcFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(writer, bytes.NewReader([]byte(""))); err != nil {
|
2017-11-28 03:10:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sha256sum := sha256.Sum256([]byte(bucket))
|
2017-12-05 20:58:09 -05:00
|
|
|
var siaObj = path.Join(s.RootDir, bucket, hex.EncodeToString(sha256sum[:]))
|
2018-04-05 18:04:40 -04:00
|
|
|
return post(ctx, s.Address, "/renter/upload/"+siaObj, "source="+srcFile, s.password)
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketInfo gets bucket metadata.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (s *siaObjects) GetBucketInfo(ctx context.Context, bucket string) (bi minio.BucketInfo, err error) {
|
2017-11-28 03:10:44 -05:00
|
|
|
sha256sum := sha256.Sum256([]byte(bucket))
|
2017-12-05 20:58:09 -05:00
|
|
|
var siaObj = path.Join(s.RootDir, bucket, hex.EncodeToString(sha256sum[:]))
|
2017-11-28 03:10:44 -05:00
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
dstFile := path.Join(s.TempDir, minio.MustGetUUID())
|
|
|
|
defer os.Remove(dstFile)
|
2017-11-28 03:10:44 -05:00
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
if err := get(ctx, s.Address, "/renter/download/"+siaObj+"?destination="+url.QueryEscape(dstFile), s.password); err != nil {
|
2017-11-28 03:10:44 -05:00
|
|
|
return bi, err
|
|
|
|
}
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.BucketInfo{Name: bucket}, nil
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListBuckets will detect and return existing buckets on Sia.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (s *siaObjects) ListBuckets(ctx context.Context) (buckets []minio.BucketInfo, err error) {
|
2018-04-05 18:04:40 -04:00
|
|
|
sObjs, serr := s.listRenterFiles(ctx, "")
|
2017-11-22 15:12:10 -05:00
|
|
|
if serr != nil {
|
|
|
|
return buckets, serr
|
|
|
|
}
|
|
|
|
|
|
|
|
m := make(set.StringSet)
|
|
|
|
|
|
|
|
prefix := s.RootDir + "/"
|
|
|
|
for _, sObj := range sObjs {
|
|
|
|
if strings.HasPrefix(sObj.SiaPath, prefix) {
|
|
|
|
siaObj := strings.TrimPrefix(sObj.SiaPath, prefix)
|
|
|
|
idx := strings.Index(siaObj, "/")
|
|
|
|
if idx > 0 {
|
|
|
|
m.Add(siaObj[0:idx])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, bktName := range m.ToSlice() {
|
2017-12-05 20:58:09 -05:00
|
|
|
buckets = append(buckets, minio.BucketInfo{
|
2017-11-22 15:12:10 -05:00
|
|
|
Name: bktName,
|
2017-11-28 03:10:44 -05:00
|
|
|
Created: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
|
2017-11-22 15:12:10 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return buckets, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBucket deletes a bucket on Sia.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (s *siaObjects) DeleteBucket(ctx context.Context, bucket string) error {
|
2017-11-28 03:10:44 -05:00
|
|
|
sha256sum := sha256.Sum256([]byte(bucket))
|
2017-12-05 20:58:09 -05:00
|
|
|
var siaObj = path.Join(s.RootDir, bucket, hex.EncodeToString(sha256sum[:]))
|
2017-11-28 03:10:44 -05:00
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
return post(ctx, s.Address, "/renter/delete/"+siaObj, "", s.password)
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
2018-03-14 15:01:47 -04:00
|
|
|
func (s *siaObjects) ListObjects(ctx context.Context, bucket string, prefix string, marker string, delimiter string, maxKeys int) (loi minio.ListObjectsInfo, err error) {
|
2018-04-05 18:04:40 -04:00
|
|
|
siaObjs, siaErr := s.listRenterFiles(ctx, bucket)
|
2017-11-22 15:12:10 -05:00
|
|
|
if siaErr != nil {
|
|
|
|
return loi, siaErr
|
|
|
|
}
|
|
|
|
|
|
|
|
loi.IsTruncated = false
|
|
|
|
loi.NextMarker = ""
|
|
|
|
|
|
|
|
root := s.RootDir + "/"
|
|
|
|
|
2017-11-28 03:10:44 -05:00
|
|
|
sha256sum := sha256.Sum256([]byte(bucket))
|
2017-11-22 15:12:10 -05:00
|
|
|
// FIXME(harsha) - No paginated output supported for Sia backend right now, only prefix
|
|
|
|
// based filtering. Once list renter files API supports paginated output we can support
|
|
|
|
// paginated results here as well - until then Listing is an expensive operation.
|
|
|
|
for _, sObj := range siaObjs {
|
2017-12-05 20:58:09 -05:00
|
|
|
name := strings.TrimPrefix(sObj.SiaPath, path.Join(root, bucket)+"/")
|
2017-11-28 03:10:44 -05:00
|
|
|
// Skip the file created specially when bucket was created.
|
|
|
|
if name == hex.EncodeToString(sha256sum[:]) {
|
|
|
|
continue
|
|
|
|
}
|
2017-11-22 15:12:10 -05:00
|
|
|
if strings.HasPrefix(name, prefix) {
|
2017-12-05 20:58:09 -05:00
|
|
|
loi.Objects = append(loi.Objects, minio.ObjectInfo{
|
2017-11-22 15:12:10 -05:00
|
|
|
Bucket: bucket,
|
|
|
|
Name: name,
|
|
|
|
Size: int64(sObj.Filesize),
|
|
|
|
IsDir: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return loi, nil
|
|
|
|
}
|
|
|
|
|
2018-03-14 15:01:47 -04:00
|
|
|
func (s *siaObjects) GetObject(ctx context.Context, bucket string, object string, startOffset int64, length int64, writer io.Writer, etag string) error {
|
2017-12-05 20:58:09 -05:00
|
|
|
dstFile := path.Join(s.TempDir, minio.MustGetUUID())
|
|
|
|
defer os.Remove(dstFile)
|
2017-11-22 15:12:10 -05:00
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
var siaObj = path.Join(s.RootDir, bucket, object)
|
2018-04-05 18:04:40 -04:00
|
|
|
if err := get(ctx, s.Address, "/renter/download/"+siaObj+"?destination="+url.QueryEscape(dstFile), s.password); err != nil {
|
2017-11-22 15:12:10 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
reader, err := os.Open(dstFile)
|
2017-11-22 15:12:10 -05:00
|
|
|
if err != nil {
|
2017-12-05 20:58:09 -05:00
|
|
|
return err
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
defer reader.Close()
|
2017-12-05 20:58:09 -05:00
|
|
|
st, err := reader.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
size := st.Size()
|
|
|
|
if _, err = reader.Seek(startOffset, os.SEEK_SET); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-22 15:12:10 -05:00
|
|
|
|
|
|
|
// For negative length we read everything.
|
|
|
|
if length < 0 {
|
|
|
|
length = size - startOffset
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
bufSize := int64(1 * humanize.MiByte)
|
2017-11-22 15:12:10 -05:00
|
|
|
if bufSize > length {
|
|
|
|
bufSize = length
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reply back invalid range if the input offset and length fall out of range.
|
|
|
|
if startOffset > size || startOffset+length > size {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, minio.InvalidRange{
|
2017-12-05 20:58:09 -05:00
|
|
|
OffsetBegin: startOffset,
|
|
|
|
OffsetEnd: length,
|
|
|
|
ResourceSize: size,
|
|
|
|
})
|
2018-04-05 18:04:40 -04:00
|
|
|
return minio.InvalidRange{
|
|
|
|
OffsetBegin: startOffset,
|
|
|
|
OffsetEnd: length,
|
|
|
|
ResourceSize: size,
|
|
|
|
}
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a staging buffer.
|
|
|
|
buf := make([]byte, int(bufSize))
|
|
|
|
|
|
|
|
_, err = io.CopyBuffer(writer, io.LimitReader(reader, length), buf)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-28 01:55:15 -05:00
|
|
|
// findSiaObject retrieves the siaObjectInfo for the Sia object with the given
|
|
|
|
// Sia path name.
|
2018-04-05 18:04:40 -04:00
|
|
|
func (s *siaObjects) findSiaObject(ctx context.Context, bucket, object string) (siaObjectInfo, error) {
|
2017-12-05 20:58:09 -05:00
|
|
|
siaPath := path.Join(s.RootDir, bucket, object)
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
sObjs, err := s.listRenterFiles(ctx, "")
|
2017-11-28 01:55:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return siaObjectInfo{}, err
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, sObj := range sObjs {
|
2017-11-28 01:55:15 -05:00
|
|
|
if sObj.SiaPath == siaPath {
|
|
|
|
return sObj, nil
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, minio.ObjectNotFound{
|
2017-12-05 20:58:09 -05:00
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
})
|
2018-04-05 18:04:40 -04:00
|
|
|
return siaObjectInfo{}, minio.ObjectNotFound{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
}
|
2017-11-28 01:55:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectInfo reads object info and replies back ObjectInfo
|
2018-03-14 15:01:47 -04:00
|
|
|
func (s *siaObjects) GetObjectInfo(ctx context.Context, bucket string, object string) (minio.ObjectInfo, error) {
|
2018-04-05 18:04:40 -04:00
|
|
|
so, err := s.findSiaObject(ctx, bucket, object)
|
2017-11-28 01:55:15 -05:00
|
|
|
if err != nil {
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.ObjectInfo{}, err
|
2017-11-28 01:55:15 -05:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
// Metadata about sia objects is just quite minimal. Sia only provides file size.
|
|
|
|
return minio.ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: object,
|
|
|
|
ModTime: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
|
|
|
|
Size: int64(so.Filesize),
|
|
|
|
IsDir: false,
|
2017-11-28 01:55:15 -05:00
|
|
|
}, nil
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutObject creates a new object with the incoming data,
|
2018-03-14 15:01:47 -04:00
|
|
|
func (s *siaObjects) PutObject(ctx context.Context, bucket string, object string, data *hash.Reader, metadata map[string]string) (objInfo minio.ObjectInfo, err error) {
|
2017-12-05 20:58:09 -05:00
|
|
|
srcFile := path.Join(s.TempDir, minio.MustGetUUID())
|
|
|
|
writer, err := os.Create(srcFile)
|
|
|
|
if err != nil {
|
|
|
|
return objInfo, err
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
wsize, err := io.CopyN(writer, data, data.Size())
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(srcFile)
|
2017-11-22 15:12:10 -05:00
|
|
|
return objInfo, err
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
if err = post(ctx, s.Address, "/renter/upload/"+path.Join(s.RootDir, bucket, object), "source="+srcFile, s.password); err != nil {
|
2017-12-05 20:58:09 -05:00
|
|
|
os.Remove(srcFile)
|
2017-11-22 15:12:10 -05:00
|
|
|
return objInfo, err
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
defer s.deleteTempFileWhenUploadCompletes(ctx, srcFile, bucket, object)
|
2017-11-22 15:12:10 -05:00
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
return minio.ObjectInfo{
|
2017-11-22 15:12:10 -05:00
|
|
|
Name: object,
|
|
|
|
Bucket: bucket,
|
|
|
|
ModTime: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
|
2017-12-05 20:58:09 -05:00
|
|
|
Size: wsize,
|
|
|
|
ETag: minio.GenETag(),
|
|
|
|
}, nil
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteObject deletes a blob in bucket
|
2018-03-14 15:01:47 -04:00
|
|
|
func (s *siaObjects) DeleteObject(ctx context.Context, bucket string, object string) error {
|
2017-11-22 15:12:10 -05:00
|
|
|
// Tell Sia daemon to delete the object
|
2017-12-05 20:58:09 -05:00
|
|
|
var siaObj = path.Join(s.RootDir, bucket, object)
|
2018-04-05 18:04:40 -04:00
|
|
|
return post(ctx, s.Address, "/renter/delete/"+siaObj, "", s.password)
|
2017-11-22 15:12:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// siaObjectInfo represents object info stored on Sia
|
|
|
|
type siaObjectInfo struct {
|
|
|
|
SiaPath string `json:"siapath"`
|
|
|
|
LocalPath string `json:"localpath"`
|
|
|
|
Filesize uint64 `json:"filesize"`
|
|
|
|
Available bool `json:"available"`
|
|
|
|
Renewing bool `json:"renewing"`
|
|
|
|
Redundancy float64 `json:"redundancy"`
|
|
|
|
UploadProgress float64 `json:"uploadprogress"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type renterFiles struct {
|
|
|
|
Files []siaObjectInfo `json:"files"`
|
|
|
|
}
|
|
|
|
|
2017-11-28 01:55:15 -05:00
|
|
|
// listRenterFiles will return a list of existing objects in the bucket provided
|
2018-04-05 18:04:40 -04:00
|
|
|
func (s *siaObjects) listRenterFiles(ctx context.Context, bucket string) (siaObjs []siaObjectInfo, err error) {
|
2017-11-22 15:12:10 -05:00
|
|
|
// Get list of all renter files
|
|
|
|
var rf renterFiles
|
2018-04-05 18:04:40 -04:00
|
|
|
if err = list(ctx, s.Address, s.password, &rf); err != nil {
|
2017-11-22 15:12:10 -05:00
|
|
|
return siaObjs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var prefix string
|
|
|
|
root := s.RootDir + "/"
|
|
|
|
if bucket == "" {
|
|
|
|
prefix = root
|
|
|
|
} else {
|
|
|
|
prefix = root + bucket + "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range rf.Files {
|
|
|
|
if strings.HasPrefix(f.SiaPath, prefix) {
|
|
|
|
siaObjs = append(siaObjs, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return siaObjs, nil
|
|
|
|
}
|
2017-11-28 01:55:15 -05:00
|
|
|
|
|
|
|
// deleteTempFileWhenUploadCompletes checks the status of a Sia file upload
|
|
|
|
// until it reaches 100% upload progress, then deletes the local temp copy from
|
|
|
|
// the filesystem.
|
2018-04-05 18:04:40 -04:00
|
|
|
func (s *siaObjects) deleteTempFileWhenUploadCompletes(ctx context.Context, tempFile string, bucket, object string) {
|
2017-11-28 01:55:15 -05:00
|
|
|
var soi siaObjectInfo
|
|
|
|
// Wait until 100% upload instead of 1x redundancy because if we delete
|
|
|
|
// after 1x redundancy, the user has to pay the cost of other hosts
|
|
|
|
// redistributing the file.
|
|
|
|
for soi.UploadProgress < 100.0 {
|
|
|
|
var err error
|
2018-04-05 18:04:40 -04:00
|
|
|
soi, err = s.findSiaObject(ctx, bucket, object)
|
2017-11-28 01:55:15 -05:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:58:09 -05:00
|
|
|
// Sleep between each check so that we're not hammering
|
|
|
|
// the Sia daemon with requests.
|
2017-11-28 01:55:15 -05:00
|
|
|
time.Sleep(15 * time.Second)
|
|
|
|
}
|
2017-12-05 20:58:09 -05:00
|
|
|
|
|
|
|
os.Remove(tempFile)
|
2017-11-28 01:55:15 -05:00
|
|
|
}
|