AWS S3 Image Uploader with NodeJS and s3-uploader module - node.js

I am trying to use this module https://www.npmjs.com/package/s3-uploader but for me it's not clear how to implement it. How to do a POST to my router ?
.post('/upload', ensureAuthenticated, (req, res, next) => {
let files = req.files;
let client = new Upload(process.env.AWS_BUCKET_NAME, {
aws: {
path: process.env.AWS_BUCKET_PATH,
region: process.env.AWS_BUCKET_REGION,
acl: 'public-read'
},
cleanup: {
versions: true,
original: false
},
original: {
awsImageAcl: 'private'
},
versions: [{
maxHeight: 1040,
maxWidth: 1040,
format: 'jpg',
suffix: '-large',
quality: 80,
awsImageExpires: 31536000,
awsImageMaxAge: 31536000
},{
maxWidth: 780,
aspect: '3:2!h',
suffix: '-medium'
},{
maxWidth: 320,
aspect: '16:9!h',
suffix: '-small'
},{
maxHeight: 100,
aspect: '1:1',
format: 'png',
suffix: '-thumb1'
},{
maxHeight: 250,
maxWidth: 250,
aspect: '1:1',
suffix: '-thumb2'
}]
});
client.upload(files.file.path, {}, function(err, versions, meta) {
if (err) { throw err; }
versions.forEach(function(image) {
console.log(image.width, image.height, image.url);
// 1024 760 https://my-bucket.s3.amazonaws.com/path/110ec58a-a0f2-4ac4-8393-c866d813b8d1.jpg
});
});
}
inside the .post I copied and paste the code they have in NPM website and changed the variables values to mine values.
I am getting this error:
Error: Command failed: identify -format "name=
size=%[size]
format=%m
colorspace=%[colorspace]
height=%[height]
width=%[width]
orientation=%[orientation]
" /images/logo.png
/bin/sh: identify: command not found
at ChildProcess.exithandler (child_process.js:206:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:877:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:498:12)
I found a related but not fully helpful topic in this thread:
Node.js S3-uploader issue

Here is the solution:
The only thing that I did was installing imagemagick on my localhost.
You have to run this in your terminal.
cd $HOME
brew install imagemagick
When it finish you can run this to see the version and details
brew info imagemagick
If you dont have brew then you need to install it.
Thank you

Related

error jest-worker just run in cpanel (adonisjs + nextjs + reactjs)

hello i got problem when deploying adonisjs + nextjs + reactjs in cpanel. i got the log error like this
events.js:291
throw er; // Unhandled 'error' event
^
Error: spawn /opt/alt/alt-nodejs12/root/usr/bin/node EAGAIN
at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
at onErrorNT (internal/child_process.js:470:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (internal/child_process.js:274:12)
at onErrorNT (internal/child_process.js:470:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'EAGAIN',
code: 'EAGAIN',
syscall: 'spawn /opt/alt/alt-nodejs12/root/usr/bin/node',
path: '/opt/alt/alt-nodejs12/root/usr/bin/node',
spawnargs: [
'/home/test123/nodevenv/repositories/testproject/12/lib/node_modules/jest-worker/build/workers/processChild.js'
]
}
my server.js
'use strict'
const { Ignitor } = require('#adonisjs/ignitor')
new Ignitor(require('#adonisjs/fold'))
.appRoot(__dirname)
.fireHttpServer()
.catch(console.error)
my next.config.js
'use strict'
const next = require('next')
const { withPlugins } = require('next-compose-plugins')
const withOffline = require('next-offline')
require('dotenv').config()
const nextWithOfflineConfig = {
dontAutoRegisterSw: false,
generateInDevMode: false,
workboxOpts: {
swDest: './next/.next/service-worker.js',
runtimeCaching: [
{
urlPattern: /^https?.*/,
handler: 'NetworkFirst',
options: {
cacheName: 'https-calls',
networkTimeoutSeconds: 15,
expiration: {
maxEntries: 150,
maxAgeSeconds: 1 * 24 * 60 * 60, // 1 day
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
],
debug: false,
},
}
const nextEnv = {
env: {
APP_KEY: process.env.APP_KEY,
}
}
module.exports = withPlugins([
[withOffline, { nextWithOfflineConfig }],
[nextEnv]
])
package.json
"next": "^10.0.9",
"next-compose-plugins": "^2.2.1",
"next-offline": "^5.0.3",
"next-page-transitions": "^1.0.0-beta.2",
"next-pwa": "^5.2.21",
"react": "^17.0.2",
cpanel dependencies
node 12.22.9
npm 6.14.0
i already try
rm -rf node_modules
rm package-lock.json yarn.lock
npm cache clear --force
npm install
but still not solve the issue
any solutions?
I'm going to guess your server is running CloudLinux, as this is when this error usually comes up?
It's down to the limits on the number of processes and threads you can create inside an LVE. If you edit your next.conf.js and add the experimental section:
module.exports = {
experimental: {
workerThreads: false,
cpus: 4
}
};
This disables worker threads, and sets a CPU count of 4 so it limits the processes created - you might still need to adjust this depending on the limits inside the account you're running the build from.
You can read a bit more about it here in the blog I wrote after helping a customer with the same issue: https://kdaws.com/2022/10/fixing-next-js-error-spawn-errno-11-on-cpanel-and-plesk-with-cloudlinux/
Thanks,
Karl

sp-request : trying get file details from sharepoint returns ECONNREFUSED

I have the following code that successfully uploads test.sspkg to my sharepoint app catalog. Now I'm trying to use sp-request to prove that it's actually there.
But I'm getting an ECONNREFUSED error.
Error message
This shows the error:
[14:24:15] INFO: Checking if file (test.sppkg) is checked out
[14:24:15] INFO: File checkout type: 0
[14:24:17] Published file 1482ms
[14:24:17] And we're done...
trying to retrieve: https://mytenant.sharepoint.com/sites/JJTest/_api/web/GetFileByServerRelativeUrl(#FileUrl)/$value?#FileUrl='%2Fsites%2FJJTest%2FAppCatalog%2Ftest.sppkg'
errored out
{ GotError: connect ECONNREFUSED 127.0.0.1:443
at onError (/src/myplugins/node_modules/got/dist/source/request-as-event-emitter.js:140:29)
at ClientRequest.request.on.error (/src/myplugins/node_modules/got/dist/source/request-as-event-emitter.js:157:17)
at ClientRequest.emit (events.js:203:15)
at ClientRequest.EventEmitter.emit (domain.js:448:20)
at ClientRequest.origin.emit.args (/src/myplugins/node_modules/#szmarczak/http-timer/dist/source/index.js:43:20)
at TLSSocket.socketErrorListener (_http_client.js:401:9)
at TLSSocket.emit (events.js:198:13)
at TLSSocket.EventEmitter.emit (domain.js:448:20)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14) name: 'RequestError', code: 'ECONNREFUSED' }
Package
This is the node package I'm using to call the SP API:
"dependencies": {
"sp-request": "^3.0.0"
}
Code:
const sprequest = require('sp-request');
await new Promise((resolve, reject) => {
gulp
.src(folderLocation)
.pipe(
spsync({
username: uname,
password: pwd,
site: siteCatalogUrl + "/",
libraryPath: catalogName,
publish: true,
verbose: true
}))
.on("finish", () => {
var spr = sprequest.create({ username: uname, password: pwd });
//https://mytenant.sharepoint.com/sites/JJTest/_api/web/getFileByServerRelativeUrl('/sites/JJTest/AppCatalog/test.sppkg')
var filepath = `${siteCatalogUrl}/_api/web/GetFileByServerRelativeUrl(#FileUrl)/$value?#FileUrl='${encodeURIComponent("/sites/JJTest/AppCatalog/test.sppkg")}'`;
console.log('trying to retrieve: ' + filepath);
var retrieveFileUrl = filepath;
spr.get(filepath, {
encoding:null
})
.then(data => {
expect(fileContent.equals(data.body)).is.true;
resolve();
})
.catch(data => {
console.log('errored out')
console.log(data)
reject();
});
resolve();
});
});
What I've tried:
I have tried just copying parts of the output from my console directly into the browser. Specifically, this is the output i get from my debug print statement:
trying to retrieve: https://mytenant.sharepoint.com/sites/JJTest/_api/web/GetFileByServerRelativeUrl/(#FileUrl)/$value?#FileUrl='%2Fsites%2FJJTest%2FAppCatalog%2Ftest.sppkg'
errored out
Pasting this:
https://mytenant.sharepoint.com/sites/JJTest/_api/web/GetFileByServerRelativeUrl('/sites/JJTest/AppCatalog/test.sppkg')
into a browser as the URL works. It returns the details of the file to me.
Can you tell me where I've gone wrong? maybe the syntax of substituting the #FileUrl?
I am using gulp version 3.9.1. And I just did a install latest of chai and sp-request.
But tried changing the specific versions to:
"sp-request": "^2.1.3"
"gulp": "^3.9.1",
"gulp-spsync-creds": "2.3.8",
"chai": "3.5.0"
And now, it's happy! it worked!

Error: EACCES: permission denied, open .audit.json

I was trying to implement the winston logger along with winston-daily-rotate-file in my angular application with node.js.
code worked fine in local windows environment, but while i was moving to higher env in linux, I am getting the below permission error while starting my application by executing "node server.js"
Please help whether issue is with permission or something else
'[FileStreamRotator] Failed to store log audit at:' '//.audit.json' 'Error:' { Error: EACCES: permission denied, open '//.audit.json'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.writeFileSync (fs.js:1299:33)
at Object.FileStreamRotator.writeAuditLog (/apps/app/node_modules/file-stream-rotator/FileStreamRotator.js:244:12)
at Object.FileStreamRotator.addLogToAudit (/apps/app/node_modules/file-stream-rotator/FileStreamRotator.js:318:27)
at EventEmitter.<anonymous> (/apps/app/node_modules/file-stream-rotator/FileStreamRotator.js:432:34)
at emitOne (events.js:116:13)
at EventEmitter.emit (events.js:211:7)
at /apps/app/node_modules/file-stream-rotator/FileStreamRotator.js:478:20
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
errno: -13,
code: 'EACCES',
syscall: 'open',
path: '//.audit.json' }
part of Server.js
const winston = require('winston');
const winstonRotator = require('winston-daily-rotate-file');
const { combine, timestamp, label, printf } = winston.format;
var rotatorOptions = {
level: 'info',
filename: './logs/app-%DATE%.log',
datePattern: 'MM-DD-YYYY',
handleExceptions: true,
zippedArchive: false,
maxsize: '10m',
maxFiles: '15d',
};
const logger =winston.createLogger({
format: combine(
label({ label: 'APP' }),
timestamp(),
printf(info => {return `${info.timestamp} [${info.label}] ${info.level}:
${info.message}`; })
),
'transports': [ new winstonRotator(rotatorOptions) ]
});
OS : Red Hat Enterprise Linux Server release 7.5 (Maipo)

python-shell from a node application

Question:
How do I run my python script in my node app?
This works:
From command line, I run this, and it works.
python generatePersonTerraform.py -s http://localhost:8080/api/person/239/exportPersonGeneration
Code that does not work in Node server.js
var PythonShell = require('python-shell');
...
var runPythonRoutine = function (request, response) {
var PythonShell = require('python-shell');
var options = {
mode: 'text',
pythonPath: 'python',
pythonOptions: ['-s'],
scriptPath: '.',
args: ['http://localhost:8080/api/person/135/exportPersonGeneration']
};
PythonShell.run('generatePersonTerraform.py', options, function (err, results) {
console.log(err);
});
}
Error:
at PythonShell.parseError (/root/my-app/node_modules/python-shell/index.js:191:17)
at terminateIfNeeded (/root/my-app/node_modules/python-shell/index.js:98:28)
at ChildProcess.<anonymous> (/root/my-app/node_modules/python-shell/index.js:89:9)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:194:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
executable: 'python',
options: [ '-s' ],
script: 'generatePersonTerraform.py',
args: [ 'http://localhost:8080/api/person/239/exportPersonGeneration' ],
exitCode: 1 }
Note
I have been trying to use https://www.npmjs.com/package/python-shell
EDIT 1
I changed the options to:
var options = {
mode: 'text',
pythonPath: 'python',
pythonOptions: [],
scriptPath: '.',
args: ['-s', 'http://localhost:8080/api/serviceType/135/exportPluginGeneration']
};
and got this error:
at PythonShell.parseError (/root/my-app/node_modules/python-shell/index.js:191:17)
at terminateIfNeeded (/root/my-app/node_modules/python-shell/index.js:98:28)
at ChildProcess.<anonymous> (/root/my-app/node_modules/python-shell/index.js:89:9)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:194:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
executable: 'python',
options: null,
script: 'generatePersonTerraform.py',
args:
[ '-s',
'http://localhost:8080/api/person/135/exportPersonGeneration' ],
exitCode: 0 }
But, it also ran and worked, it just stalls the node app. So, if I figure out how to make it not stall then I am all good. The python script seems to build all the files it is supposed to.
'-s' should be in args, not pythonOptions.

Loopback 3 with loopback-component-storage hang on POST file

I've a very simple server running under loopback 3 and seeing this issue again: https://github.com/strongloop/loopback-component-storage/issues/9
As the gists and examples don't work any longer I would like to reopen that issue.
I've created a very simple API templated server and added the following code:
module.exports = function (File) {
File.upload = function (ctx, options, cb) {
if (!options) options = {};
ctx.req.params.container = 'common';
console.log("DO");
File.app.models.Storage.upload(ctx.req, ctx.result, options, function (err, fileObj) {
console.log("FILE");
cb(fileObj);
});
};
File.remoteMethod(
'upload',
{
description: 'Uploads a file',
accepts: [
{arg: 'ctx', type: 'object', http: {source: 'context'}},
{arg: 'options', type: 'object', http: {source: 'query'}}
],
returns: {
arg: 'fileObject', type: 'object', root: true
},
http: {verb: 'post'}
}
);
};
The problem is now, that while posting via POSTMAN to the upload function, I see the following behavior in the console:
DO
Error: Request aborted
at IncomingMessage.<anonymous> (...../server/node_modules/formidable/lib/incoming_form.js:120:19)
at emitNone (events.js:86:13)
at IncomingMessage.emit (events.js:188:7)
at abortIncoming (_http_server.js:383:9)
at socketOnClose (_http_server.js:377:3)
at emitOne (events.js:101:20)
at Socket.emit (events.js:191:7)
at TCP._handle.close [as _onclose] (net.js:504:12)
FILE
And Postman returns an empty response...
I'm totally lost as a beginner here at this step!
What is my fault here?
Thx for any input
OMG, it seems that I've found the answer:
https://github.com/strongloop/loopback-component-storage/issues/86
Just use Header "Accept" with value "multipart/form-data", not setting "Content-type". That works for me from Postman

Resources