1

我需要将多个文件上传到 Bluemix 中的对象存储。我正在使用 Multer 来获取文件。我遇到了 Multer 或 pkgcloud 的几个问题,从授权问题到 ENOENT 问题。我挣扎了 3 天,终于让代码工作了。如果对任何人有帮助,请发布示例。

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');
});
4

0 回答 0