【问题标题】:Download File from IBM Bluemix Object Storage using NodeJS使用 NodeJS 从 IBM Bluemix Object Storage 下载文件
【发布时间】:2017-04-05 18:30:40
【问题描述】:

我在使用 NodeJS 脚本从 IBM Bluemix Object Storage 下载文件时遇到问题,问题是从存储中下载文件需要很长时间,100K 文件大约需要 21 秒,大​​约8 秒等待第一个块,然后休息以读取所有块

我正在使用该服务来存储图像并使用 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');
    });
});

【问题讨论】:

    标签: node.js ibm-cloud object-storage


    【解决方案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);
    }
    

    modules/object-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);
                    }
                });
            }
        };
    };
    

    【讨论】:

    • 我无法按原样使用您的代码,pkgcloud 库现在导致 bluemix 存储服务出现身份验证错误,我不得不将其更改为 pkgcloud-bluemix-objectstorage,但我从您的代码中了解到在响应上调用管道而不是将响应流直接传递给下载对象以及如何在调用下载之前调用身份验证,我不知道这是问题还是 pkgcloud 本身的问题,但与更改调用管道和更改 pkgcloud,下载 api 工作正常,谢谢
    【解决方案2】:

    使用来自 joe 的答案,并且经过少量修改,该服务现在运行良好,我不得不更改库 pkgcloud,因为它会导致 bluemix 出现身份验证问题,我切换到包装包 pkgcloud-bluemix-objectstorage,我的代码现在看起来像这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多