Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a storage layer for attachments #11387

Merged
merged 59 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
e1fcbf9
Add a storage layer for attachments
lunny May 12, 2020
8099dcb
Fix some bug
lunny May 12, 2020
d7a681b
fix test
lunny May 12, 2020
0c3bad9
Fix copyright head and lint
lunny May 13, 2020
96784da
Fix bug
lunny May 14, 2020
02354a1
Add setting for minio and flags for migrate-storage
lunny May 17, 2020
3b47020
Add documents
lunny May 17, 2020
3188152
fix lint
lunny May 17, 2020
c64f602
Add test for minio store type on attachments
lunny May 17, 2020
d600a20
fix test
lunny May 18, 2020
ec36cce
fix test
lunny May 18, 2020
5390ce2
Apply suggestions from code review
lunny May 20, 2020
3c56f5d
Add warning when storage migrated successfully
lunny May 20, 2020
e8344c8
Fix drone
lunny Jun 2, 2020
bcede71
fix test
lunny Jun 8, 2020
3efe232
rebase
lunny Jun 18, 2020
60f6582
Fix test
lunny Jun 21, 2020
8547e77
display the error on console
lunny Jun 22, 2020
edcb300
Move minio test to amd64 since minio docker don't support arm64
lunny Jun 23, 2020
a5b033c
refactor the codes
lunny Jun 23, 2020
669922a
add trace
lunny Jun 23, 2020
80feb8e
Fix test
lunny Jun 23, 2020
76710e7
remove log on xorm
lunny Jun 24, 2020
afe4e8a
Fi download bug
lunny Jun 26, 2020
fb0dd9e
Add a storage layer for attachments
lunny May 12, 2020
886a651
Add setting for minio and flags for migrate-storage
lunny May 17, 2020
457644a
fix lint
lunny May 17, 2020
30f67cf
Add test for minio store type on attachments
lunny May 17, 2020
3507a2c
Apply suggestions from code review
lunny May 20, 2020
ac20111
Fix drone
lunny Jun 2, 2020
0b43483
fix test
lunny Jun 8, 2020
fa8776d
Fix test
lunny Jun 21, 2020
418153c
display the error on console
lunny Jun 22, 2020
d306cbe
Move minio test to amd64 since minio docker don't support arm64
lunny Jun 23, 2020
d9ca0d4
refactor the codes
lunny Jun 23, 2020
a097ad8
add trace
lunny Jun 23, 2020
4c40fc6
Fix test
lunny Jun 23, 2020
d54bed6
Add URL function to serve attachments directly from S3/Minio
tystuyfzand Jun 25, 2020
f6b74a7
Add ability to enable/disable redirection in attachment configuration
tystuyfzand Jun 25, 2020
21802ea
Fix typo
tystuyfzand Jun 25, 2020
b58df9e
Add a storage layer for attachments
lunny May 12, 2020
c8f3316
Add setting for minio and flags for migrate-storage
lunny May 17, 2020
751d721
fix lint
lunny May 17, 2020
4be66c5
Add test for minio store type on attachments
lunny May 17, 2020
ffa008c
Apply suggestions from code review
lunny May 20, 2020
23091b4
Fix drone
lunny Jun 2, 2020
64eddce
fix test
lunny Jun 8, 2020
e650f32
Fix test
lunny Jun 21, 2020
bdca7b0
display the error on console
lunny Jun 22, 2020
0f9d767
Move minio test to amd64 since minio docker don't support arm64
lunny Jun 23, 2020
d2974c0
don't change unrelated files
lunny Jun 26, 2020
d3e2973
Fix lint
lunny Jun 26, 2020
b9998cf
Fix build
lunny Jun 26, 2020
8f43baf
update go.mod and go.sum
lunny Jul 2, 2020
3dffb03
Use github.com/minio/minio-go/v6
lunny Jul 2, 2020
2e4da60
Remove unused function
lunny Jul 26, 2020
a4d0549
Upgrade minio to v7 and some other improvements
lunny Aug 13, 2020
562d3d2
fix lint
lunny Aug 13, 2020
7eb927e
Fix go mod
lunny Aug 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ services:
discovery.type: single-node
image: elasticsearch:7.5.0

- name: minio
image: minio/minio:RELEASE.2020-05-16T01-33-21Z
commands:
- minio server /data
environment:
MINIO_ACCESS_KEY: 123456
MINIO_SECRET_KEY: 12345678

steps:
- name: fetch-tags
pull: default
Expand Down
147 changes: 147 additions & 0 deletions cmd/migrate_storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package cmd

import (
"context"
"fmt"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/migrations"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"

"github.com/urfave/cli"
)

// CmdMigrateStorage represents the available migrate storage sub-command.
var CmdMigrateStorage = cli.Command{
Name: "migrate-storage",
Usage: "Migrate the storage",
Description: "This is a command for migrating storage.",
Action: runMigrateStorage,
Flags: []cli.Flag{
cli.StringFlag{
Name: "type, t",
Value: "",
Usage: "Kinds of files to migrate, currently only 'attachments' is supported",
},
cli.StringFlag{
Name: "store, s",
Value: "local",
Usage: "New storage type, local or minio",
},
cli.StringFlag{
Name: "path, p",
Value: "",
Usage: "New storage placement if store is local (leave blank for default)",
},
cli.StringFlag{
Name: "minio-endpoint",
Value: "",
Usage: "Minio storage endpoint",
},
cli.StringFlag{
Name: "minio-access-key-id",
Value: "",
Usage: "Minio storage accessKeyID",
},
cli.StringFlag{
Name: "minio-secret-access-key",
Value: "",
Usage: "Minio storage secretAccessKey",
},
cli.StringFlag{
Name: "minio-bucket",
Value: "",
Usage: "Minio storage bucket",
},
cli.StringFlag{
Name: "minio-location",
Value: "",
Usage: "Minio storage location to create bucket",
},
cli.StringFlag{
Name: "minio-base-path",
Value: "",
Usage: "Minio storage basepath on the bucket",
},
cli.BoolFlag{
Name: "minio-use-ssl",
Usage: "Enable SSL for minio",
},
},
}

func migrateAttachments(dstStorage storage.ObjectStorage) error {
return models.IterateAttachment(func(attach *models.Attachment) error {
_, err := storage.Copy(dstStorage, attach.RelativePath(), storage.Attachments, attach.RelativePath())
lunny marked this conversation as resolved.
Show resolved Hide resolved
return err
})
}

func runMigrateStorage(ctx *cli.Context) error {
if err := initDB(); err != nil {
return err
}

log.Trace("AppPath: %s", setting.AppPath)
log.Trace("AppWorkPath: %s", setting.AppWorkPath)
log.Trace("Custom path: %s", setting.CustomPath)
log.Trace("Log path: %s", setting.LogRootPath)
setting.InitDBConfig()

if err := models.NewEngine(context.Background(), migrations.Migrate); err != nil {
log.Fatal("Failed to initialize ORM engine: %v", err)
return err
}

if err := storage.Init(); err != nil {
return err
}

tp := ctx.String("type")
switch tp {
case "attachments":
var dstStorage storage.ObjectStorage
var err error
switch ctx.String("store") {
case "local":
p := ctx.String("path")
if p == "" {
log.Fatal("Path must be given when store is loal")
return nil
}
dstStorage, err = storage.NewLocalStorage(p)
case "minio":
dstStorage, err = storage.NewMinioStorage(
context.Background(),
ctx.String("minio-endpoint"),
ctx.String("minio-access-key-id"),
ctx.String("minio-secret-access-key"),
ctx.String("minio-bucket"),
ctx.String("minio-location"),
ctx.String("minio-base-path"),
ctx.Bool("minio-use-ssl"),
)
default:
return fmt.Errorf("Unsupported attachments store type: %s", ctx.String("store"))
}

if err != nil {
return err
}
if err := migrateAttachments(dstStorage); err != nil {
return err
}

log.Warn("All files have been copied to the new placement but old files are still on the orignial placement.")

return nil
}

return nil
}
25 changes: 23 additions & 2 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -749,14 +749,35 @@ ENABLE_FEDERATED_AVATAR = false
[attachment]
; Whether attachments are enabled. Defaults to `true`
ENABLED = true
; Path for attachments. Defaults to `data/attachments`
PATH = data/attachments

; One or more allowed types, e.g. "image/jpeg|image/png". Use "*/*" for all types.
ALLOWED_TYPES = image/jpeg|image/png|application/zip|application/gzip
; Max size of each file. Defaults to 4MB
MAX_SIZE = 4
; Max number of files per upload. Defaults to 5
MAX_FILES = 5
; Storage type for attachments, `local` for local disk or `minio` for s3 compatible
; object storage service, default is `local`.
STORE_TYPE = local
; Allows the storage driver to redirect to authenticated URLs to serve files directly
; Currently, only `minio` is supported.
SERVE_DIRECT = false
; Path for attachments. Defaults to `data/attachments` only available when STORE_TYPE is `local`
PATH = data/attachments
; Minio endpoint to connect only available when STORE_TYPE is `minio`
MINIO_ENDPOINT = localhost:9000
; Minio accessKeyID to connect only available when STORE_TYPE is `minio`
MINIO_ACCESS_KEY_ID =
; Minio secretAccessKey to connect only available when STORE_TYPE is `minio`
MINIO_SECRET_ACCESS_KEY =
; Minio bucket to store the attachments only available when STORE_TYPE is `minio`
MINIO_BUCKET = gitea
; Minio location to create bucket only available when STORE_TYPE is `minio`
MINIO_LOCATION = us-east-1
; Minio base path on the bucket only available when STORE_TYPE is `minio`
MINIO_BASE_PATH = attachments/
; Minio enabled ssl only available when STORE_TYPE is `minio`
MINIO_USE_SSL = false

[time]
; Specifies the format for fully outputted dates. Defaults to RFC1123
Expand Down
11 changes: 10 additions & 1 deletion docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,20 @@ set name for unique queues. Individual queues will default to
## Attachment (`attachment`)

- `ENABLED`: **true**: Enable this to allow uploading attachments.
- `PATH`: **data/attachments**: Path to store attachments.
- `ALLOWED_TYPES`: **see app.example.ini**: Allowed MIME types, e.g. `image/jpeg|image/png`.
Use `*/*` for all types.
- `MAX_SIZE`: **4**: Maximum size (MB).
- `MAX_FILES`: **5**: Maximum number of attachments that can be uploaded at once.
- `STORE_TYPE`: **local**: Storage type for attachments, `local` for local disk or `minio` for s3 compatible object storage service, default is `local`.
- `SERVE_DIRECT`: **false**: Allows the storage driver to redirect to authenticated URLs to serve files directly. Currently, only Minio/S3 is supported via signed URLs, local does nothing.
- `PATH`: **data/attachments**: Path to store attachments only available when STORE_TYPE is `local`
- `MINIO_ENDPOINT`: **localhost:9000**: Minio endpoint to connect only available when STORE_TYPE is `minio`
- `MINIO_ACCESS_KEY_ID`: Minio accessKeyID to connect only available when STORE_TYPE is `minio`
- `MINIO_SECRET_ACCESS_KEY`: Minio secretAccessKey to connect only available when STORE_TYPE is `minio`
- `MINIO_BUCKET`: **gitea**: Minio bucket to store the attachments only available when STORE_TYPE is `minio`
- `MINIO_LOCATION`: **us-east-1**: Minio location to create bucket only available when STORE_TYPE is `minio`
- `MINIO_BASE_PATH`: **attachments/**: Minio base path on the bucket only available when STORE_TYPE is `minio`
- `MINIO_USE_SSL`: **false**: Minio enabled ssl only available when STORE_TYPE is `minio`

## Log (`log`)

Expand Down
10 changes: 9 additions & 1 deletion docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,18 @@ menu:
## Attachment (`attachment`)

- `ENABLED`: 是否允许用户上传附件。
- `PATH`: 附件存储路径
- `ALLOWED_TYPES`: 允许上传的附件类型。比如:`image/jpeg|image/png`,用 `*/*` 表示允许任何类型。
- `MAX_SIZE`: 附件最大限制,单位 MB,比如: `4`。
- `MAX_FILES`: 一次最多上传的附件数量,比如: `5`。
- `STORE_TYPE`: **local**: 附件存储类型,`local` 将存储到本地文件夹, `minio` 将存储到 s3 兼容的对象存储服务中。
- `PATH`: **data/attachments**: 附件存储路径,仅当 `STORE_TYPE` 为 `local` 时有效。
- `MINIO_ENDPOINT`: **localhost:9000**: Minio 终端,仅当 `STORE_TYPE` 是 `minio` 时有效。
- `MINIO_ACCESS_KEY_ID`: Minio accessKeyID ,仅当 `STORE_TYPE` 是 `minio` 时有效。
- `MINIO_SECRET_ACCESS_KEY`: Minio secretAccessKey,仅当 `STORE_TYPE` 是 `minio` 时有效。
- `MINIO_BUCKET`: **gitea**: Minio bucket to store the attachments,仅当 `STORE_TYPE` 是 `minio` 时有效。
- `MINIO_LOCATION`: **us-east-1**: Minio location to create bucket,仅当 `STORE_TYPE` 是 `minio` 时有效。
- `MINIO_BASE_PATH`: **attachments/**: Minio base path on the bucket,仅当 `STORE_TYPE` 是 `minio` 时有效。
- `MINIO_USE_SSL`: **false**: Minio enabled ssl,仅当 `STORE_TYPE` 是 `minio` 时有效。

关于 `ALLOWED_TYPES`, 在 (*)unix 系统中可以使用`file -I <filename>` 来快速获得对应的 `MIME type`。

Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ require (
github.com/mgechev/revive v1.0.2
github.com/mholt/archiver/v3 v3.3.0
github.com/microcosm-cc/bluemonday v1.0.3-0.20191119130333-0a75d7616912
github.com/minio/minio-go/v7 v7.0.4
github.com/mitchellh/go-homedir v1.1.0
github.com/msteinert/pam v0.0.0-20151204160544-02ccfbfaf0cc
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5
Expand Down Expand Up @@ -104,17 +105,17 @@ require (
github.com/yuin/goldmark-highlighting v0.0.0-20200307114337-60d527fdb691
github.com/yuin/goldmark-meta v0.0.0-20191126180153-f0638e958b60
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de
golang.org/x/net v0.0.0-20200625001655-4c5254603344
golang.org/x/net v0.0.0-20200707034311-ab3426394381
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1
golang.org/x/text v0.3.2
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae
golang.org/x/text v0.3.3
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
golang.org/x/tools v0.0.0-20200814230902-9882f1d1823d
google.golang.org/appengine v1.6.5 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/asn1-ber.v1 v1.0.0-20150924051756-4e86f4367175 // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/ini.v1 v1.52.0
gopkg.in/ini.v1 v1.57.0
gopkg.in/ldap.v3 v3.0.2
gopkg.in/yaml.v2 v2.3.0
mvdan.cc/xurls/v2 v2.1.0
Expand Down
Loading