sftp nodejs avec ssh2-sftp-client

08/08/2024
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
import assert from 'node:assert';

import Client from 'ssh2-sftp-client';

const sftp = new Client();

describe('sftp', () => {
    it('connect', async () => {
        await sftp.connect({
            host: 'raphaelpiccolo.com',
            port: 2222,
            username: 'foo',
            password: process.env.PASSWORD,
        });
    });
    it('cwd', async () => {
        assert.equal(await sftp.cwd(), '/');
    });
    it('list', async () => {
        const files = await sftp.list(`/`);
        assert.ok(Array.isArray(files));
    });
    it('put', async () => {
        await sftp.put(Buffer.from('test'), `/upload/test.txt`);
    });
    it('get', async () => {
        const x = await sftp.get(`/upload/test.txt`);
        assert.equal(x.toString(), 'test');
    });
    it('end', async () => {
        await sftp.end();
    });
});

Raccourcis