2017-04-05 31 views
1

使用NodeJS腳本從IBM Bluemix Object Storage下載文件時出現問題,問題是需要很長時間才能從存儲中下載文件,100K文件需要大約21秒,大約8秒鐘等待第一個塊並休息讀取所有塊使用NodeJS從IBM Bluemix Object Storage下載文件

我正在使用該服務來存儲圖像並使用NodeJS腳本從存儲中讀取這些圖像,並通過HTML下載圖像img標籤,所以我在這裏做錯了什麼?

的代碼的NodeJS:

app.get("/ostore/image/:filename", function(request, response) { 
    response.set('Content-Type', 'image/jpg'); 
    response.set('cache-control', 'max-age=604800'); 
    response.set('Last-Modified', 'Sat, 05 Dec 2015 03:17:48 GMT'); 
    var credentials = app.appEnv.services['Object-Storage'][0].credentials 
    var pkgcloud = require("pkgcloud"); 
    var client = pkgcloud.storage.createClient({ 
     provider: 'openstack', 
     username: credentials.userId, 
     password: credentials.password, 
     authUrl: credentials.auth_url, 
     tenantId: credentials.projectId, 
     region: credentials.region, 
     version: "2" 
    }); 
    client.download({ 
     container: app.storageContainer, 
     remote: request.params.filename, 
     stream: response 
    }, function() { 
     response.end('done'); 
    }); 
}); 

回答

1

嘗試是這樣的:

routes.js

var vcap_os = require(__dirname + '/../utils/vcap')('Object-Storage'), 
    os = require(__dirname + '/../modules/object-storage'); 

module.exports = function(app) { 
    var router = app.loopback.Router(); 

    // proxy for object storage service 
    router.get('/api/Products/image/:container/:file', function(req, res) { 
     os(vcap_os.credentials).download(req.params.container, req.params.file, function(download) { 
      download.pipe(res); 
     }); 
    }); 

    app.use(router); 
} 

模塊/對象storage.js

var pkgcloud = require('pkgcloud'); 

module.exports = function(creds) { 
    var config = { 
     provider: 'openstack', 
     useServiceCatalog: true, 
     useInternal: false, 
     keystoneAuthVersion: 'v3', 
     authUrl: creds.auth_url, 
     tenantId: creds.projectId, 
     domainId: creds.domainId, 
     username: creds.username, 
     password: creds.password, 
     region: creds.region 
    }; 

    return { 
     download: function(container, file, cbk) { 
      var client = pkgcloud.storage.createClient(config); 
      client.auth(function (error) { 
       if(error) { 
        console.error("Authorization error for storage client (pkgcloud): ", error); 
       } 
       else { 
        var request = client.download({ 
         container: container, 
         remote: file 
        }); 

        cbk(request); 
       } 
      }); 
     } 
    }; 
}; 
+0

嗨@adevedo,這是否解決了您的問題? – joe

+1

我無法按原樣使用您的代碼,pkgcloud庫現在導致bluemix存儲服務發生身份驗證錯誤,我必須將其更改爲pkgcloud-bluemix-objectstorage,但我從您的代碼中瞭解如何在響應中調用管道而不是直接將響應流傳遞給下載對象,以及如何在調用下載之前調用身份驗證,我不知道這是問題還是問題出在pkgcloud本身上,而是隨着調用管道的更改以及更改pkgcloud ,下載api工作正常,謝謝 – adevedo

0

使用joe的答案,並且很少修改,服務現在工作正常,我必須更改庫pkgcloud,因爲它導致bluemix身份驗證問題,我切換到包裝程序包pkgcloud-bluemix-objectstorage,my代碼看起來像這樣:

app.get('/ostore/image/:filename', (request, response) => { 
    const credentials = app.appEnv.services['Object-Storage'][0].credentials; 
    const config = {}; 
    config.provider = 'openstack'; 
    config.authUrl = 'https://identity.open.softlayer.com/'; 
    config.region = credentials.region; 
    config.useServiceCatalog = true; 
    config.useInternal = true; 
    config.tenantId = credentials.projectId; 
    config.userId = credentials.userId; 
    config.username = credentials.username; 
    config.password = credentials.password; 
    config.auth = { 
     forceUri: 'https://identity.open.softlayer.com/v3/auth/tokens', 
     interfaceName: 'public', 
     identity: { 
      methods: [ 
       'password' 
      ], 
      password: { 
       user: { 
        id: credentials.userId, 
        password: credentials.password 
       } 
      } 
     }, 
     scope: { 
      project: { 
       id: credentials.projectId 
      } 
     } 
    }; 

    const client = pkgcloud.storage.createClient(config); 
    client.auth(error => { 
     if (error) { 
      console.error('Authorization error for storage client (pkgcloud): ', error); 
     } else { 
      const download = client.download({ 
       container: app.storageContainer, 
       remote: request.params.filename 
      }); 
      download.on('response', res => { 
       delete res.headers['content-type']; 
       delete res.headers['last-modified']; 
       res.headers['Last-Modified'] = 'Sat, 05 Dec 2015 03:17:48 GMT'; 
       res.headers['Cache-Control'] = 'public, max-age=2592000'; 
       res.headers['Expires'] = new Date(Date.now() + 2592000000).toUTCString(); 
       res.headers['Content-Type'] = 'image/jpg'; 
      }).pipe(response); 
     } 
    }); 
}); 
相關問題