filesystem.go 9.4 KB

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