Upload file from sftp to upload folder and use it on strapi admin

System Information
  • Strapi Version:
    3.2.4
  • Operating System:
    ubonto 20.04
  • Database:
    sqlite
  • Node Version:
    12.04
  • NPM Version:
    6.04
  • Yarn Version:
    1.1

i want to Upload file from sftp to upload folder and use it on strapi admin
but i cant !
for example i want upload a bunch of musics and use music in strapi admin

1 Like

How exactly are you trying to upload them through sftp? Why don’t you use API?

beacuse i have bunch of music that when i want to use api it needs a longer time its hard to me to upload all files with default media uploader in strapi but when i use sftp it is upload as soon as possible

Got it. From where do you want to upload them? From another cloud server or from your localhost?

Let’s asume you want to copy them from localhost, in that case you can use “scp” command:

scp /www/music/*  your_ssh_user@your_external_host:/var/www/strapi-project/public/music

Where /www/music/ is your local folder with all the music and /var/www/strapi-project/public/music is your remote folder where you want to copy it.

Same will work if you want to copy from a remote host to another remote host:

scp your_ssh_user@first_external_host:/www/music/*  your_ssh_user@second_external_host:/var/www/strapi-project/public/music

Also, I would recommend you to serve music directly through Nginx (in case you don’t need some authorization/roles from strapi)

copy my music directltly into upload folder by a filemanager that i used sftp to connect to a same server that i run strapi and copy my musics to /upload file in my app and then use it on strapi admin

yes i was copy my music into /upload but strapi doesnt recongnize my music in strapi admin madia libraty

All file references are stored in DB inside upload_file table/collection.

So let’s assume you copied a file called test.mp3 to /upload now, you should create a new record in DB for reference like this:
image

Now it is visible in Strapi, and I can use it:
image

To automate that process, I would recommend using a watcher, that detects all the changes inside a folder, so you could trigger a function that creates them in strapi DB. You can find a lot of modules that provide that functionality to watch for changes in a specific folder, in your case /upload/music.

1 Like

Hi! i’m new to strapi and with an issue related with this topic.

I’m building CSV files with exported data and I place them into /upload folder with the next line:

fs.writeFileSync(rootDir + ‘/public/uploads/’ + fileName + ‘.csv’, csvContent);

According to your suggestion, my pending task is to use a watcher to build the code to write on the DB but can’t find any documentation about it.
Can you please provide more info to do that?

Thank you so much.

I Tried @sunnyson idea. But it fails.

Error Message: UnhandledPromiseRejectionWarning: Error: The model upload_file can’t be foun

var stats = fs.statSync(rootDir + ‘/public/uploads/’ + fileName);

strapi.query(‘upload_file’).create({
name: fileName,
alternativeText: “”,
caption: “”,
hash: uuid.v4(),//random hash
ext: “.csv”,
mime: “text/csv”,
size: stats.size,
url: “/uploads/” + fileName,
provider: “local”,
width: null,
heught: null
});

To upload a file from your machine/server and to avoid REST requests, please take a look at the following example:

const mime = require('mime-types'); //used to detect file's mime type
const fs = require('fs');
const rootDir = process.cwd();
const fileName = 'test.csv';
const filePath = `${rootDir}/public/uploads/${fileName}`
const stats = fs.statSync(filePath);

//uploading it directly to upload services.
await strapi.plugins.upload.services.upload.upload({
    data:{}, //mandatory declare the data(can be empty), otherwise it will give you an undefined error.
    files: {
		path: filePath, 
		name: fileName,
		type: mime.getType(filePath), // mime type of the file
		size: stats.size,
	},
});

Take a look that I uses a third party library called mime-types. That one is already installed in your project as it is used by Strapi.

2 Likes

Thanks for helping!
Do you know where can i get documentation related to the upload service?
Google links looks broken from last week and cannot find nothing related to this.