Search code examples
androidibm-cloudobject-storage

How to access files stored in Bluemix Object Storage using Android?


I am working on an Android app where I want to fetch data from cloud. I stored my data in Bluemix Object Storage but not getting any help on how to access the data in simple way.

I also tried AWS S3 storage service. It has a simple console where I can get the URL of the file to access it from the Android app like this.

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

Is there any such way to get file URL in Bluemix?


Solution

  • Depending on how you architected your application, there are several potential solutions to your problem.

    At this time, there is not an Object Storage Android Client SDK released. Here is where you can find the current SDKs for Object Storage:

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

    Depending on the backend you set up, you could easily set up a proxy router that securely connects to your Object Storage service instance and routes the output to a URL that can be used by your Android application.

    For instance using pkgcloud and a Node.js backend:

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