1

I had a requirement to upload multiple files to object storage in Bluemix. I was using Multer for getting the files. I faced several issues either with Multer or pkgcloud ranging from authorization issues to ENOENT problems. I struggled for 3 days and finally got the code working. Posting the example if it helps anyone.

var config = {
        provider: 'openstack',
        useServiceCatalog: true,
        useInternal: false,
        keystoneAuthVersion: 'v3',
        authUrl: vcapCredential.objectStorage_credential.auth_url,
        tenantId: vcapCredential.objectStorage_credential.projectId,    
        domainId: vcapCredential.objectStorage_credential.domainId,
        username: vcapCredential.objectStorage_credential.userId,
        password: vcapCredential.objectStorage_credential.password,
        region: vcapCredential.objectStorage_credential.region
    };
var multer  = require('multer');
var upload = multer({  dest: './uploads'  });
app.post('/upload', upload.any(),   function(req, res){
    var filelist = req.files;
    var storageClient = pkgcloud.storage.createClient(config);
    storageClient.auth(function(err) {
        if (err) {
            console.error('err :' + err.message);
        }
        else {
            console.log('success' + storageClient._identity);
        }
    });

    storageClient.createContainer({
        name: 'container-name'
        }, function (err, container) {
        if (err) {
            console.error('err1 :' +err.message);
        }
        else {
            for(var i in filelist) {
                console.log('filelist[i].originalname :' +filelist[i].originalname)
                var myFile = fs.createReadStream(filelist[i].path);
                var upload = storageClient.upload({
                    container: container.name,
                    remote:  filelist[i].originalname
                });
                upload.on('error', function(err) {
                    console.error(err);
                });
                upload.on('success', function(file) {
                    console.log(file.toJSON());
                });
                myFile.pipe(upload);
            }
        }
    });
    res.send('success');
});
Indrani
  • 27
  • 7
  • Try **Skipper** its more like **multer** but easy to implement. I can help you out with Skipper if you want. Refer http://stackoverflow.com/questions/24905839/uploading-multiple-files-with-sails-js-0-10-and-skipper-using-dropzone-js – Siddhartha Chowdhury Dec 28 '16 at 10:50
  • Thanks. We had tried using Skipper first by looping through the files. But somehow it didn't work. So we thought of using Multer instead. Though we use Skipper for uploading single files to object storage. – Indrani Dec 30 '16 at 07:31

0 Answers0