- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6.2k
Add a storage layer for attachments #11387
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
          
     Merged
      
      
    
  
     Merged
                    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 8099dcb
              
                Fix some bug
              
              
                lunny d7a681b
              
                fix test
              
              
                lunny 0c3bad9
              
                Fix copyright head and lint
              
              
                lunny 96784da
              
                Fix bug
              
              
                lunny 02354a1
              
                Add setting for minio and flags for migrate-storage
              
              
                lunny 3b47020
              
                Add documents
              
              
                lunny 3188152
              
                fix lint
              
              
                lunny c64f602
              
                Add test for minio store type on attachments
              
              
                lunny d600a20
              
                fix test
              
              
                lunny ec36cce
              
                fix test
              
              
                lunny 5390ce2
              
                Apply suggestions from code review
              
              
                lunny 3c56f5d
              
                Add warning when storage migrated successfully
              
              
                lunny e8344c8
              
                Fix drone
              
              
                lunny bcede71
              
                fix test
              
              
                lunny 3efe232
              
                rebase
              
              
                lunny 60f6582
              
                Fix test
              
              
                lunny 8547e77
              
                display the error on console
              
              
                lunny edcb300
              
                Move minio test to amd64 since minio docker don't support arm64
              
              
                lunny a5b033c
              
                refactor the codes
              
              
                lunny 669922a
              
                add trace
              
              
                lunny 80feb8e
              
                Fix test
              
              
                lunny 76710e7
              
                remove log on xorm
              
              
                lunny afe4e8a
              
                Fi download bug
              
              
                lunny fb0dd9e
              
                Add a storage layer for attachments
              
              
                lunny 886a651
              
                Add setting for minio and flags for migrate-storage
              
              
                lunny 457644a
              
                fix lint
              
              
                lunny 30f67cf
              
                Add test for minio store type on attachments
              
              
                lunny 3507a2c
              
                Apply suggestions from code review
              
              
                lunny ac20111
              
                Fix drone
              
              
                lunny 0b43483
              
                fix test
              
              
                lunny fa8776d
              
                Fix test
              
              
                lunny 418153c
              
                display the error on console
              
              
                lunny d306cbe
              
                Move minio test to amd64 since minio docker don't support arm64
              
              
                lunny d9ca0d4
              
                refactor the codes
              
              
                lunny a097ad8
              
                add trace
              
              
                lunny 4c40fc6
              
                Fix test
              
              
                lunny d54bed6
              
                Add URL function to serve attachments directly from S3/Minio
              
              
                tystuyfzand f6b74a7
              
                Add ability to enable/disable redirection in attachment configuration
              
              
                tystuyfzand 21802ea
              
                Fix typo
              
              
                tystuyfzand b58df9e
              
                Add a storage layer for attachments
              
              
                lunny c8f3316
              
                Add setting for minio and flags for migrate-storage
              
              
                lunny 751d721
              
                fix lint
              
              
                lunny 4be66c5
              
                Add test for minio store type on attachments
              
              
                lunny ffa008c
              
                Apply suggestions from code review
              
              
                lunny 23091b4
              
                Fix drone
              
              
                lunny 64eddce
              
                fix test
              
              
                lunny e650f32
              
                Fix test
              
              
                lunny bdca7b0
              
                display the error on console
              
              
                lunny 0f9d767
              
                Move minio test to amd64 since minio docker don't support arm64
              
              
                lunny d2974c0
              
                don't change unrelated files
              
              
                lunny d3e2973
              
                Fix lint
              
              
                lunny b9998cf
              
                Fix build
              
              
                lunny 8f43baf
              
                update go.mod and go.sum
              
              
                lunny 3dffb03
              
                Use github.com/minio/minio-go/v6
              
              
                lunny 2e4da60
              
                Remove unused function
              
              
                lunny a4d0549
              
                Upgrade minio to v7 and some other improvements
              
              
                lunny 562d3d2
              
                fix lint
              
              
                lunny 7eb927e
              
                Fix go mod
              
              
                lunny File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
          The table of contents is too big for display.
        
      Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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()) | ||
| 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 | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.