Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LoopBack Storage Service, upload directly (without request object)

I want to upload a file to a storage container using the Loopback storage service.

But the .upload() method expects a request object. But what if I want to upload an existing file that's not coming from a submitted form?

Do I need to fake a POST request in order to make the upload handler happy? Seems like there must be a better way.

like image 708
Arne Avatar asked Oct 12 '15 09:10

Arne


1 Answers

As @RohitArkani hinted, the file app-cloud.js contains a version 1 example of file upload which uses storageService.uploadStream(container, file, [options], Callback).

var s3 = new StorageService({
  provider: 'amazon',
  key: "your-amazon-key",
  keyId: "your-amazon-key-id"
});

var fs = require('fs');
var path = require('path');
var stream = s3.uploadStream('con1', 'test.jpg');
fs.createReadStream(path.join(__dirname, 'test.jpg')).pipe(stream);

It seems (but see the comment) that in version 2, you get the storage service via

var ds = loopback.createDataSource({
  connector: require('loopback-storage-service'),
  provider: 'amazon',
  key: '...',
  keyId: '...'
});
var Container = ds.createModel('container');

instead. Then, call Container.uploadStream(...).

like image 112
serv-inc Avatar answered Oct 21 '22 02:10

serv-inc