Skip to content

Commit bad749e

Browse files
author
Charles-Antoine Mathieu
committed
S3 backend path prefix root-gg#316
1 parent 325cfd4 commit bad749e

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

server/data/s3/s3.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Config struct {
2121
SecretAccessKey string
2222
Bucket string
2323
Location string
24+
Prefix string
2425
PartSize uint64
2526
UseSSL bool
2627
}
@@ -31,7 +32,7 @@ func NewConfig(params map[string]interface{}) (config *Config) {
3132
config = new(Config)
3233
config.Bucket = "plik"
3334
config.Location = "us-east-1"
34-
config.PartSize = 32 * 1024 * 1024 // 32MB
35+
config.PartSize = 16 * 1000 * 1000 // 16MB
3536
utils.Assign(config, params)
3637
return
3738
}
@@ -53,7 +54,7 @@ func (config *Config) Validate() error {
5354
if config.Location == "" {
5455
return fmt.Errorf("missing location")
5556
}
56-
if config.PartSize < 5*1024*1024 {
57+
if config.PartSize < 5*1000*1000 {
5758
return fmt.Errorf("invalid part size")
5859
}
5960
return nil
@@ -100,24 +101,31 @@ func NewBackend(config *Config) (b *Backend, err error) {
100101

101102
// GetFile implementation for S3 Data Backend
102103
func (b *Backend) GetFile(file *common.File) (reader io.ReadCloser, err error) {
103-
return b.client.GetObject(b.config.Bucket, file.ID, minio.GetObjectOptions{})
104+
return b.client.GetObject(b.config.Bucket, b.getObjectName(file.ID), minio.GetObjectOptions{})
104105
}
105106

106107
// AddFile implementation for S3 Data Backend
107108
func (b *Backend) AddFile(file *common.File, fileReader io.Reader) (backendDetails string, err error) {
108109
if file.Size > 0 {
109-
_, err = b.client.PutObject(b.config.Bucket, file.ID, fileReader, file.Size, minio.PutObjectOptions{ContentType: file.Type})
110+
_, err = b.client.PutObject(b.config.Bucket, b.getObjectName(file.ID), fileReader, file.Size, minio.PutObjectOptions{ContentType: file.Type})
110111
} else {
111112
// https://github.com/minio/minio-go/issues/989
112113
// Minio defaults to 128MB chunks and has to actually allocate a buffer of this size before uploading the chunk
113114
// This can lead to very high memory usage when uploading a lot of small files in parallel
114-
// We default to 32MB which allow to store files up to 320GB ( 10000 chunks of 32MB ), feel free to adjust this parameter to your needs.
115-
_, err = b.client.PutObject(b.config.Bucket, file.ID, fileReader, -1, minio.PutObjectOptions{ContentType: file.Type, PartSize: b.config.PartSize})
115+
// We default to 16MB which allow to store files up to 160GB ( 10000 chunks of 16MB ), feel free to adjust this parameter to your needs.
116+
_, err = b.client.PutObject(b.config.Bucket, b.getObjectName(file.ID), fileReader, -1, minio.PutObjectOptions{ContentType: file.Type, PartSize: b.config.PartSize})
116117
}
117118
return "", err
118119
}
119120

120121
// RemoveFile implementation for S3 Data Backend
121122
func (b *Backend) RemoveFile(file *common.File) (err error) {
122-
return b.client.RemoveObject(b.config.Bucket, file.ID)
123+
return b.client.RemoveObject(b.config.Bucket, b.getObjectName(file.ID))
123124
}
125+
126+
func (b *Backend) getObjectName(name string) string {
127+
if b.config.Prefix != "" {
128+
return fmt.Sprintf("%s/%s", b.config.Prefix, name)
129+
}
130+
return name
131+
}

server/plikd.cfg

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ OvhApiEndpoint = "" # OVH api endpoint to use. Defaults to https
6868
# SecretAccessKey = "access_key_secret"
6969
# Bucket = "plik"
7070
# Location = "us-east-1"
71+
# Prefix = ""
7172
# UseSSL = true
72-
# PartSize = 33554432 // Chunk size when file size is not known ( default to 32MB )
73+
# PartSize = 16000000 // Chunk size when file size is not known. (default to 16MB)
74+
# // Multiply by 10000 to get the max upload file size (max upload file size 160GB)
7375

7476
DataBackend = "file"
7577
[DataBackendConfig]

testing/minio/plikd.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ DataBackend = "s3"
1212
Endpoint = "127.0.0.1:2604"
1313
AccessKeyID = "access_key"
1414
SecretAccessKey = "access_key_secret"
15+
Prefix = ""
1516
PartSize = 5242880 # 5MB
1617

1718
[MetadataBackendConfig]

0 commit comments

Comments
 (0)