如何使用Android访问存储在Bluemix Object Storage中的文件?

时间:2016-06-26 04:25:02

标签: android ibm-cloud object-storage

我正在开发一款Android应用,我希望从云中获取数据。 我将数据存储在Bluemix Object Storage中,但没有获得有关如何以简单方式访问数据的任何帮助。

我也尝试过AWS S3存储服务。它有一个简单的控制台,我可以从Android应用程序获取文件的URL,如下所示。

https://s3-ap-southeast-1.amazonaws.com/com.myapp/540.mp4

有没有这种方法可以在Bluemix中获取文件URL?

2 个答案:

答案 0 :(得分:0)

根据您构建应用程序的方式,有几种可能的解决方案可以解决您的问题。

目前还没有发布Object Storage Android Client SDK。您可以在此处找到对象存储的当前SDK:

https://github.com/ibm-bluemix-mobile-services

根据您设置的后端,您可以轻松设置代理路由器,该路由器可安全地连接到您的对象存储服务实例,并将输出路由到可供Android应用程序使用的URL。

例如使用pkgcloud和Node.js后端:

<强> routes.js

var vcap_objectstorage = require('../utils/vcap')('Object-Storage'),
    objectstorage = require('../modules/objectstorage');

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

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

    app.use(router);
}

<强> objectstorage.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);
                }
            });
        }
    };
};

答案 1 :(得分:0)

如何使用IBM Object Storage =&gt; Auth =&gt;创建容器=&gt;上传对象

var pkgcloud = require('pkgcloud');

var config = {
provider: 'openstack',
useServiceCatalog: true,
useInternal: false,
keystoneAuthVersion: 'v3',
authUrl: 'https://identity.open.softlayer.com',
tenantId: 'XXXXXXXXXXXXXX', //projectId from credentials
domainId: 'XXXXXXXXXXXXX',
username: 'XXXXXXXXXXXXXXX',
password: 'XXXXXXXXXXXXXXXX',
region: 'dallas' //dallas or london region
};

var storageClient = pkgcloud.storage.createClient(config);

storageClient.auth(function(err) {
if (err) {
console.error('error'+err);
}
else {
console.log(storageClient._identity);
}
});

var fs = require('fs');
storageClient.createContainer({
name: 'XXXXXXX'
}, function (err, container) {

if (err) {
console.error(err);
}
else {
var myFile = fs.createReadStream('/home/amit/Downloads/cloud-app-developer.png');
var upload = storageClient.upload({
container: container.name,
remote: 'cloud-app-developer.png'
});
upload.on('error', function(err) {
console.error(err);
});
upload.on('success', function(file) {
console.log(file.toJSON());
});
myFile.pipe(upload);
}
});