{"library":"nest-sftp","title":"Nest SFTP Module","description":"nest-sftp is a NestJS framework module that provides a wrapper around the `ssh2-sftp-client` library, enabling SFTP client capabilities within a NestJS application. It integrates SFTP connection management directly into the NestJS dependency injection system, allowing for easy configuration and use of SFTP operations. The current stable version is 3.1.0, which includes support for NestJS 11. While minor releases and bug fixes occur periodically, major version updates are less frequent, often coinciding with significant changes or new NestJS version support. Key differentiators include its seamless integration with Nest's module system, supporting both synchronous (`forRoot`) and asynchronous (`forRootAsync`) configuration patterns, and the ability to inject a configured `SftpClientService` directly into other services for file transfer operations.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install nest-sftp"],"cli":null},"imports":["import { SftpModule } from 'nest-sftp';","import { SftpClientService } from 'nest-sftp';","import { ConnectConfig } from 'ssh2-sftp-client';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Module, Injectable, Logger } from '@nestjs/common';\nimport { SftpModule, SftpClientService } from 'nest-sftp';\nimport { ConnectConfig } from 'ssh2-sftp-client';\n\n@Injectable()\nclass ConfigService {\n  getSftpConnectionInfo(): ConnectConfig {\n    // In a real app, fetch these from environment variables or a configuration store\n    return {\n      host: process.env.SFTP_HOST ?? 'sftp.example.com',\n      port: parseInt(process.env.SFTP_PORT ?? '22', 10),\n      username: process.env.SFTP_USERNAME ?? 'user',\n      password: process.env.SFTP_PASSWORD ?? 'password',\n      // Optional: debug logging\n      debug: console.log\n    };\n  }\n}\n\n@Injectable()\nexport class AppService {\n  private readonly logger = new Logger(AppService.name);\n  constructor(private readonly sftpClient: SftpClientService) {}\n\n  async runSftpExample(): Promise<void> {\n    try {\n      this.logger.log('Attempting to list SFTP directory...');\n      const list = await this.sftpClient.list('/remote/path');\n      this.logger.log(`Listed directory: ${JSON.stringify(list.map(f => f.name))}`);\n\n      const remoteFile = `/remote/path/test-${Date.now()}.txt`;\n      const localContent = 'Hello SFTP from NestJS!';\n      const buffer = Buffer.from(localContent);\n\n      this.logger.log(`Uploading file to ${remoteFile}`);\n      await this.sftpClient.upload(buffer, remoteFile);\n      this.logger.log('File uploaded successfully.');\n\n      this.logger.log(`Downloading file from ${remoteFile}`);\n      const downloadedBuffer = await this.sftpClient.download(remoteFile);\n      this.logger.log(`Downloaded content: ${downloadedBuffer.toString()}`);\n\n      this.logger.log(`Deleting file ${remoteFile}`);\n      await this.sftpClient.delete(remoteFile);\n      this.logger.log('File deleted successfully.');\n\n    } catch (error) {\n      this.logger.error('SFTP operation failed:', error.message);\n    }\n  }\n}\n\n@Module({\n  imports: [\n    SftpModule.forRootAsync(\n      {\n        useFactory: (configService: ConfigService) => {\n          return configService.getSftpConnectionInfo();\n        },\n        inject: [ConfigService],\n        imports: [ConfigService],\n      }\n    ),\n  ],\n  controllers: [],\n  providers: [ConfigService, AppService],\n  exports: [ConfigService, AppService]\n})\nexport class AppModule {\n  constructor(private readonly appService: AppService) {\n    // Kick off the SFTP example when the app starts\n    // This would typically be triggered by an endpoint or lifecycle hook in a real app\n    this.appService.runSftpExample();\n  }\n}\n","lang":"typescript","description":"This quickstart demonstrates registering the SftpModule asynchronously, injecting SftpClientService, and performing basic SFTP operations like listing, uploading, downloading, and deleting files.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}