mirror of
https://github.com/dutchcoders/transfer.sh.git
synced 2024-11-26 22:20:18 +01:00
Merge pull request #201 from stefanbenten/sb/put-full
Add second S3 Handler for Non-Multipart Uploads
This commit is contained in:
commit
058f6e581c
4 changed files with 37 additions and 16 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -5,6 +5,7 @@ src/
|
||||||
bin/
|
bin/
|
||||||
*.pyc
|
*.pyc
|
||||||
*.egg-info/
|
*.egg-info/
|
||||||
|
.idea/
|
||||||
|
|
||||||
.tmp
|
.tmp
|
||||||
.vagrant
|
.vagrant
|
||||||
|
|
|
@ -126,6 +126,7 @@ provider | which storage provider to use | (s3, grdrive or local) |
|
||||||
aws-access-key | aws access key | | AWS_ACCESS_KEY
|
aws-access-key | aws access key | | AWS_ACCESS_KEY
|
||||||
aws-secret-key | aws access key | | AWS_SECRET_KEY
|
aws-secret-key | aws access key | | AWS_SECRET_KEY
|
||||||
bucket | aws bucket | | BUCKET
|
bucket | aws bucket | | BUCKET
|
||||||
|
s3-no-multipart | disables s3 multipart upload | false | |
|
||||||
basedir | path storage for local/gdrive provider| |
|
basedir | path storage for local/gdrive provider| |
|
||||||
gdrive-client-json-filepath | path to oauth client json config for gdrive provider| |
|
gdrive-client-json-filepath | path to oauth client json config for gdrive provider| |
|
||||||
gdrive-local-config-path | path to store local transfer.sh config cache for gdrive provider| |
|
gdrive-local-config-path | path to store local transfer.sh config cache for gdrive provider| |
|
||||||
|
|
10
cmd/cmd.go
10
cmd/cmd.go
|
@ -2,15 +2,13 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/dutchcoders/transfer.sh/server"
|
"github.com/dutchcoders/transfer.sh/server"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/minio/cli"
|
"github.com/minio/cli"
|
||||||
"log"
|
|
||||||
"google.golang.org/api/googleapi"
|
"google.golang.org/api/googleapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -117,6 +115,10 @@ var globalFlags = []cli.Flag{
|
||||||
Value: "",
|
Value: "",
|
||||||
EnvVar: "BUCKET",
|
EnvVar: "BUCKET",
|
||||||
},
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "s3-no-multipart",
|
||||||
|
Usage: "Disables S3 Multipart Puts",
|
||||||
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "gdrive-client-json-filepath",
|
Name: "gdrive-client-json-filepath",
|
||||||
Usage: "",
|
Usage: "",
|
||||||
|
@ -294,7 +296,7 @@ func New() *Cmd {
|
||||||
panic("secret-key not set.")
|
panic("secret-key not set.")
|
||||||
} else if bucket := c.String("bucket"); bucket == "" {
|
} else if bucket := c.String("bucket"); bucket == "" {
|
||||||
panic("bucket not set.")
|
panic("bucket not set.")
|
||||||
} else if storage, err := server.NewS3Storage(accessKey, secretKey, bucket, c.String("s3-endpoint"), logger); err != nil {
|
} else if storage, err := server.NewS3Storage(accessKey, secretKey, bucket, c.String("s3-endpoint"), logger, c.Bool("s3-no-multipart")); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
options = append(options, server.UseStorage(storage))
|
options = append(options, server.UseStorage(storage))
|
||||||
|
|
|
@ -2,26 +2,25 @@ package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"mime"
|
"mime"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"encoding/json"
|
|
||||||
"github.com/goamz/goamz/s3"
|
"github.com/goamz/goamz/s3"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"golang.org/x/oauth2/google"
|
"golang.org/x/oauth2/google"
|
||||||
"google.golang.org/api/drive/v3"
|
"google.golang.org/api/drive/v3"
|
||||||
"google.golang.org/api/googleapi"
|
"google.golang.org/api/googleapi"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Storage interface {
|
type Storage interface {
|
||||||
|
@ -125,17 +124,18 @@ func (s *LocalStorage) Put(token string, filename string, reader io.Reader, cont
|
||||||
|
|
||||||
type S3Storage struct {
|
type S3Storage struct {
|
||||||
Storage
|
Storage
|
||||||
bucket *s3.Bucket
|
bucket *s3.Bucket
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
|
noMultipart bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewS3Storage(accessKey, secretKey, bucketName, endpoint string, logger *log.Logger) (*S3Storage, error) {
|
func NewS3Storage(accessKey, secretKey, bucketName, endpoint string, logger *log.Logger, disableMultipart bool) (*S3Storage, error) {
|
||||||
bucket, err := getBucket(accessKey, secretKey, bucketName, endpoint)
|
bucket, err := getBucket(accessKey, secretKey, bucketName, endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &S3Storage{bucket: bucket, logger: logger}, nil
|
return &S3Storage{bucket: bucket, logger: logger, noMultipart: disableMultipart}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *S3Storage) Type() string {
|
func (s *S3Storage) Type() string {
|
||||||
|
@ -150,7 +150,6 @@ func (s *S3Storage) Head(token string, filename string) (contentType string, con
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
contentType = response.Header.Get("Content-Type")
|
contentType = response.Header.Get("Content-Type")
|
||||||
|
|
||||||
contentLength, err = strconv.ParseUint(response.Header.Get("Content-Length"), 10, 0)
|
contentLength, err = strconv.ParseUint(response.Header.Get("Content-Length"), 10, 0)
|
||||||
|
@ -202,8 +201,7 @@ func (s *S3Storage) Delete(token string, filename string) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *S3Storage) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) (err error) {
|
func (s *S3Storage) putMulti(key string, reader io.Reader, contentType string, contentLength uint64) (err error) {
|
||||||
key := fmt.Sprintf("%s/%s", token, filename)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
multi *s3.Multi
|
multi *s3.Multi
|
||||||
|
@ -316,6 +314,25 @@ func (s *S3Storage) Put(token string, filename string, reader io.Reader, content
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *S3Storage) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) (err error) {
|
||||||
|
key := fmt.Sprintf("%s/%s", token, filename)
|
||||||
|
|
||||||
|
s.logger.Printf("Uploading file %s to S3 Bucket", filename)
|
||||||
|
if !s.noMultipart {
|
||||||
|
err = s.putMulti(key, reader, contentType, contentLength)
|
||||||
|
} else {
|
||||||
|
err = s.bucket.PutReader(key, reader, int64(contentLength), contentType, s3.Private, s3.Options{})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Printf("Completed uploading %s", filename)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
type GDrive struct {
|
type GDrive struct {
|
||||||
service *drive.Service
|
service *drive.Service
|
||||||
rootId string
|
rootId string
|
||||||
|
|
Loading…
Reference in a new issue