filesystem.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package sdk
  2. import "github.com/drakkan/sftpgo/v2/kms"
  3. // FilesystemProvider defines the supported storage filesystems
  4. type FilesystemProvider int
  5. // supported values for FilesystemProvider
  6. const (
  7. LocalFilesystemProvider FilesystemProvider = iota // Local
  8. S3FilesystemProvider // AWS S3 compatible
  9. GCSFilesystemProvider // Google Cloud Storage
  10. AzureBlobFilesystemProvider // Azure Blob Storage
  11. CryptedFilesystemProvider // Local encrypted
  12. SFTPFilesystemProvider // SFTP
  13. )
  14. // GetProviderByName returns the FilesystemProvider matching a given name
  15. // to provide backwards compatibility, numeric strings are accepted as well
  16. func GetProviderByName(name string) FilesystemProvider {
  17. switch name {
  18. case "0", "osfs":
  19. return LocalFilesystemProvider
  20. case "1", "s3fs":
  21. return S3FilesystemProvider
  22. case "2", "gcsfs":
  23. return GCSFilesystemProvider
  24. case "3", "azblobfs":
  25. return AzureBlobFilesystemProvider
  26. case "4", "cryptfs":
  27. return CryptedFilesystemProvider
  28. case "5", "sftpfs":
  29. return SFTPFilesystemProvider
  30. }
  31. // TODO think about returning an error value instead of silently defaulting to LocalFilesystemProvider
  32. return LocalFilesystemProvider
  33. }
  34. // Name returns the Provider's unique name
  35. func (p FilesystemProvider) Name() string {
  36. switch p {
  37. case LocalFilesystemProvider:
  38. return "osfs"
  39. case S3FilesystemProvider:
  40. return "s3fs"
  41. case GCSFilesystemProvider:
  42. return "gcsfs"
  43. case AzureBlobFilesystemProvider:
  44. return "azblobfs"
  45. case CryptedFilesystemProvider:
  46. return "cryptfs"
  47. case SFTPFilesystemProvider:
  48. return "sftpfs"
  49. }
  50. return "" // let's not claim to be
  51. }
  52. // ShortInfo returns a human readable, short description for the given FilesystemProvider
  53. func (p FilesystemProvider) ShortInfo() string {
  54. switch p {
  55. case LocalFilesystemProvider:
  56. return "Local"
  57. case S3FilesystemProvider:
  58. return "AWS S3 (Compatible)"
  59. case GCSFilesystemProvider:
  60. return "Google Cloud Storage"
  61. case AzureBlobFilesystemProvider:
  62. return "Azure Blob Storage"
  63. case CryptedFilesystemProvider:
  64. return "Local encrypted"
  65. case SFTPFilesystemProvider:
  66. return "SFTP"
  67. }
  68. return ""
  69. }
  70. // ListProviders returns a list of available FilesystemProviders.
  71. func ListProviders() []FilesystemProvider {
  72. return []FilesystemProvider{
  73. LocalFilesystemProvider, S3FilesystemProvider,
  74. GCSFilesystemProvider, AzureBlobFilesystemProvider,
  75. CryptedFilesystemProvider, SFTPFilesystemProvider,
  76. }
  77. }
  78. // S3FsConfig defines the configuration for S3 based filesystem
  79. type S3FsConfig struct {
  80. Bucket string `json:"bucket,omitempty"`
  81. // KeyPrefix is similar to a chroot directory for local filesystem.
  82. // If specified then the SFTP user will only see objects that starts
  83. // with this prefix and so you can restrict access to a specific
  84. // folder. The prefix, if not empty, must not start with "/" and must
  85. // end with "/".
  86. // If empty the whole bucket contents will be available
  87. KeyPrefix string `json:"key_prefix,omitempty"`
  88. Region string `json:"region,omitempty"`
  89. AccessKey string `json:"access_key,omitempty"`
  90. AccessSecret *kms.Secret `json:"access_secret,omitempty"`
  91. Endpoint string `json:"endpoint,omitempty"`
  92. StorageClass string `json:"storage_class,omitempty"`
  93. // The buffer size (in MB) to use for multipart uploads. The minimum allowed part size is 5MB,
  94. // and if this value is set to zero, the default value (5MB) for the AWS SDK will be used.
  95. // The minimum allowed value is 5.
  96. // Please note that if the upload bandwidth between the SFTP client and SFTPGo is greater than
  97. // the upload bandwidth between SFTPGo and S3 then the SFTP client have to wait for the upload
  98. // of the last parts to S3 after it ends the file upload to SFTPGo, and it may time out.
  99. // Keep this in mind if you customize these parameters.
  100. UploadPartSize int64 `json:"upload_part_size,omitempty"`
  101. // How many parts are uploaded in parallel
  102. UploadConcurrency int `json:"upload_concurrency,omitempty"`
  103. }
  104. // GCSFsConfig defines the configuration for Google Cloud Storage based filesystem
  105. type GCSFsConfig struct {
  106. Bucket string `json:"bucket,omitempty"`
  107. // KeyPrefix is similar to a chroot directory for local filesystem.
  108. // If specified then the SFTP user will only see objects that starts
  109. // with this prefix and so you can restrict access to a specific
  110. // folder. The prefix, if not empty, must not start with "/" and must
  111. // end with "/".
  112. // If empty the whole bucket contents will be available
  113. KeyPrefix string `json:"key_prefix,omitempty"`
  114. CredentialFile string `json:"-"`
  115. Credentials *kms.Secret `json:"credentials,omitempty"`
  116. // 0 explicit, 1 automatic
  117. AutomaticCredentials int `json:"automatic_credentials,omitempty"`
  118. StorageClass string `json:"storage_class,omitempty"`
  119. }
  120. // AzBlobFsConfig defines the configuration for Azure Blob Storage based filesystem
  121. type AzBlobFsConfig struct {
  122. Container string `json:"container,omitempty"`
  123. // Storage Account Name, leave blank to use SAS URL
  124. AccountName string `json:"account_name,omitempty"`
  125. // Storage Account Key leave blank to use SAS URL.
  126. // The access key is stored encrypted based on the kms configuration
  127. AccountKey *kms.Secret `json:"account_key,omitempty"`
  128. // Optional endpoint. Default is "blob.core.windows.net".
  129. // If you use the emulator the endpoint must include the protocol,
  130. // for example "http://127.0.0.1:10000"
  131. Endpoint string `json:"endpoint,omitempty"`
  132. // Shared access signature URL, leave blank if using account/key
  133. SASURL *kms.Secret `json:"sas_url,omitempty"`
  134. // KeyPrefix is similar to a chroot directory for local filesystem.
  135. // If specified then the SFTPGo user will only see objects that starts
  136. // with this prefix and so you can restrict access to a specific
  137. // folder. The prefix, if not empty, must not start with "/" and must
  138. // end with "/".
  139. // If empty the whole bucket contents will be available
  140. KeyPrefix string `json:"key_prefix,omitempty"`
  141. // The buffer size (in MB) to use for multipart uploads.
  142. // If this value is set to zero, the default value (1MB) for the Azure SDK will be used.
  143. // Please note that if the upload bandwidth between the SFTPGo client and SFTPGo server is
  144. // greater than the upload bandwidth between SFTPGo and Azure then the SFTP client have
  145. // to wait for the upload of the last parts to Azure after it ends the file upload to SFTPGo,
  146. // and it may time out.
  147. // Keep this in mind if you customize these parameters.
  148. UploadPartSize int64 `json:"upload_part_size,omitempty"`
  149. // How many parts are uploaded in parallel
  150. UploadConcurrency int `json:"upload_concurrency,omitempty"`
  151. // Set to true if you use an Azure emulator such as Azurite
  152. UseEmulator bool `json:"use_emulator,omitempty"`
  153. // Blob Access Tier
  154. AccessTier string `json:"access_tier,omitempty"`
  155. }
  156. // CryptFsConfig defines the configuration to store local files as encrypted
  157. type CryptFsConfig struct {
  158. Passphrase *kms.Secret `json:"passphrase,omitempty"`
  159. }
  160. // SFTPFsConfig defines the configuration for SFTP based filesystem
  161. type SFTPFsConfig struct {
  162. Endpoint string `json:"endpoint,omitempty"`
  163. Username string `json:"username,omitempty"`
  164. Password *kms.Secret `json:"password,omitempty"`
  165. PrivateKey *kms.Secret `json:"private_key,omitempty"`
  166. Fingerprints []string `json:"fingerprints,omitempty"`
  167. // Prefix is the path prefix to strip from SFTP resource paths.
  168. Prefix string `json:"prefix,omitempty"`
  169. // Concurrent reads are safe to use and disabling them will degrade performance.
  170. // Some servers automatically delete files once they are downloaded.
  171. // Using concurrent reads is problematic with such servers.
  172. DisableCouncurrentReads bool `json:"disable_concurrent_reads,omitempty"`
  173. // The buffer size (in MB) to use for transfers.
  174. // Buffering could improve performance for high latency networks.
  175. // With buffering enabled upload resume is not supported and a file
  176. // cannot be opened for both reading and writing at the same time
  177. // 0 means disabled.
  178. BufferSize int64 `json:"buffer_size,omitempty"`
  179. }
  180. // Filesystem defines filesystem details
  181. type Filesystem struct {
  182. Provider FilesystemProvider `json:"provider"`
  183. S3Config S3FsConfig `json:"s3config,omitempty"`
  184. GCSConfig GCSFsConfig `json:"gcsconfig,omitempty"`
  185. AzBlobConfig AzBlobFsConfig `json:"azblobconfig,omitempty"`
  186. CryptConfig CryptFsConfig `json:"cryptconfig,omitempty"`
  187. SFTPConfig SFTPFsConfig `json:"sftpconfig,omitempty"`
  188. }