How to use sftp upload provider in Strapi v4

Hey there,

for the larger part of the day if been trying to get the strapi-provider-upload-sftp-v2 to work with Strapi v4.
I installed it with npm install and added the settings (and folders) in:

./extensions/upload/config/settings.json

{
  "provider": "sftp",
  "providerOptions": {
    "host": "<host>",
    "port": "<port>",
    "user": "<sftp-username>",
    "password": "<password>",
    "basePath": "<base-path>",
    "baseUrl": "<base-url>"
  }
}

then I changed the ‘providers’ parameter from “sftp” to “strapi-provider-upload-sftp” as noted in the Strapi docs (Upload - Strapi Developer Docs).

I then added the configuration below in ./config/plugins.js

module.exports = ({ env }) => ({
  upload: {
    provider: 'strapi-provider-upload-sftp-v2',
    providerOptions: {
      host: "host-string",
      port: "22",
      user: "user-string",
      password: "password-string",
      basePath: "/",
      baseUrl: ""
    }
  }

I then moved the module from node_modules into the ./providers folder (I had to create) and changed the dependency in package.json to register it as a local provider (Upload - Strapi Developer Docs).

  "dependencies": {
 ....
    "strapi-provider-upload-sftp-v2": "file:providers/strapi-provider-upload-sftp-v2"
}

All to no avail.
What am I missing here?

Any help would be highly appreciated !

I finally figured it out. In case someone stumbles across this:

in ./config/plugins.js

module.exports = ({ env }) => ({
  upload: {
    config: {
      provider: 'strapi-provider-upload-sftp-v2',
      providerOptions: {
        host: `${process.env.SFTP_HOST}`,
        port: `${process.env.SFTP_PORT}`,
        user: `${process.env.SFTP_USER}`,
        password: `${process.env.SFTP_PASSWORD}`,
        basePath: `${process.env.SFTP_BASEPATH}`,
        baseUrl: `${process.env.ASSET_URL}`
      },
      breakpoints: {
        xlarge: 1920,
        large: 1000,
        medium: 750,
        small: 500,
        xsmall: 64
      },
    }
  },
});

the breakpoints argument is optional.

./extensions/upload/config/settings.json is not needed and you don’t need to put the files in ./providers

2 Likes

Thanks! Gonna try that!
So there is no need to create a provider folder? That also means that the change done to `package.json``s dependencies also is not necessary?

Shouldn’t env('SFTP_HOST') be enough instead of ${process.env.SFTP_HOST}? After all env is passed to the function body…

UPDATE:
This works great! And for the middlewares.js, one simply adds the URL of the host, too:

module.exports = [
  'strapi::errors',
  {
    name: 'strapi::security',
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          'connect-src': ["'self'", 'https:'],
          'img-src': ["'self'", 'data:', 'blob:', 'your.domain.tld'],
          'media-src': ["'self'", 'data:', 'blob:', 'your.domain.tld'],
          // OR using env vars:
          //'img-src': ["'self'", 'data:', 'blob:', `${process.env.SFTP_BASEURL}`],
          //'media-src': ["'self'", 'data:', 'blob:', `${process.env.SFTP_BASEURL}`],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  [...]
]

Then thumbnails are shown in the media library in strapi’s backend.

Thank you, @Hoja !

is possible to use a .ppk key or other solution?
my host require auth with ssh key for sftp
thanks