filesystem.go 9.9 KB

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