filesystem.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // DownloadPartMaxTime defines the maximum time allowed, in seconds, to download a single chunk (5MB).
  104. // 0 means no timeout. Ignored for non-multipart downloads.
  105. DownloadPartMaxTime int `json:"download_part_max_time,omitempty"`
  106. }
  107. // GCSFsConfig defines the configuration for Google Cloud Storage based filesystem
  108. type GCSFsConfig struct {
  109. Bucket string `json:"bucket,omitempty"`
  110. // KeyPrefix is similar to a chroot directory for local filesystem.
  111. // If specified then the SFTP user will only see objects that starts
  112. // with this prefix and so you can restrict access to a specific
  113. // folder. The prefix, if not empty, must not start with "/" and must
  114. // end with "/".
  115. // If empty the whole bucket contents will be available
  116. KeyPrefix string `json:"key_prefix,omitempty"`
  117. CredentialFile string `json:"-"`
  118. Credentials *kms.Secret `json:"credentials,omitempty"`
  119. // 0 explicit, 1 automatic
  120. AutomaticCredentials int `json:"automatic_credentials,omitempty"`
  121. StorageClass string `json:"storage_class,omitempty"`
  122. }
  123. // AzBlobFsConfig defines the configuration for Azure Blob Storage based filesystem
  124. type AzBlobFsConfig struct {
  125. Container string `json:"container,omitempty"`
  126. // Storage Account Name, leave blank to use SAS URL
  127. AccountName string `json:"account_name,omitempty"`
  128. // Storage Account Key leave blank to use SAS URL.
  129. // The access key is stored encrypted based on the kms configuration
  130. AccountKey *kms.Secret `json:"account_key,omitempty"`
  131. // Optional endpoint. Default is "blob.core.windows.net".
  132. // If you use the emulator the endpoint must include the protocol,
  133. // for example "http://127.0.0.1:10000"
  134. Endpoint string `json:"endpoint,omitempty"`
  135. // Shared access signature URL, leave blank if using account/key
  136. SASURL *kms.Secret `json:"sas_url,omitempty"`
  137. // KeyPrefix is similar to a chroot directory for local filesystem.
  138. // If specified then the SFTPGo user will only see objects that starts
  139. // with this prefix and so you can restrict access to a specific
  140. // folder. The prefix, if not empty, must not start with "/" and must
  141. // end with "/".
  142. // If empty the whole bucket contents will be available
  143. KeyPrefix string `json:"key_prefix,omitempty"`
  144. // The buffer size (in MB) to use for multipart uploads.
  145. // If this value is set to zero, the default value (1MB) for the Azure SDK will be used.
  146. // Please note that if the upload bandwidth between the SFTPGo client and SFTPGo server is
  147. // greater than the upload bandwidth between SFTPGo and Azure then the SFTP client have
  148. // to wait for the upload of the last parts to Azure after it ends the file upload to SFTPGo,
  149. // and it may time out.
  150. // Keep this in mind if you customize these parameters.
  151. UploadPartSize int64 `json:"upload_part_size,omitempty"`
  152. // How many parts are uploaded in parallel
  153. UploadConcurrency int `json:"upload_concurrency,omitempty"`
  154. // Set to true if you use an Azure emulator such as Azurite
  155. UseEmulator bool `json:"use_emulator,omitempty"`
  156. // Blob Access Tier
  157. AccessTier string `json:"access_tier,omitempty"`
  158. }
  159. // CryptFsConfig defines the configuration to store local files as encrypted
  160. type CryptFsConfig struct {
  161. Passphrase *kms.Secret `json:"passphrase,omitempty"`
  162. }
  163. // SFTPFsConfig defines the configuration for SFTP based filesystem
  164. type SFTPFsConfig struct {
  165. Endpoint string `json:"endpoint,omitempty"`
  166. Username string `json:"username,omitempty"`
  167. Password *kms.Secret `json:"password,omitempty"`
  168. PrivateKey *kms.Secret `json:"private_key,omitempty"`
  169. Fingerprints []string `json:"fingerprints,omitempty"`
  170. // Prefix is the path prefix to strip from SFTP resource paths.
  171. Prefix string `json:"prefix,omitempty"`
  172. // Concurrent reads are safe to use and disabling them will degrade performance.
  173. // Some servers automatically delete files once they are downloaded.
  174. // Using concurrent reads is problematic with such servers.
  175. DisableCouncurrentReads bool `json:"disable_concurrent_reads,omitempty"`
  176. // The buffer size (in MB) to use for transfers.
  177. // Buffering could improve performance for high latency networks.
  178. // With buffering enabled upload resume is not supported and a file
  179. // cannot be opened for both reading and writing at the same time
  180. // 0 means disabled.
  181. BufferSize int64 `json:"buffer_size,omitempty"`
  182. }
  183. // Filesystem defines filesystem details
  184. type Filesystem struct {
  185. Provider FilesystemProvider `json:"provider"`
  186. S3Config S3FsConfig `json:"s3config,omitempty"`
  187. GCSConfig GCSFsConfig `json:"gcsconfig,omitempty"`
  188. AzBlobConfig AzBlobFsConfig `json:"azblobconfig,omitempty"`
  189. CryptConfig CryptFsConfig `json:"cryptconfig,omitempty"`
  190. SFTPConfig SFTPFsConfig `json:"sftpconfig,omitempty"`
  191. }