首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node - Bluemix对象存储

Node - Bluemix对象存储
EN

Stack Overflow用户
提问于 2016-10-17 16:54:06
回答 1查看 380关注 0票数 1

我想用我的Bluemix帐户尝试对象存储。

前端正在给我发送base64编码的图像。

我想知道如何存储文件(通过编写新的临时文件),并将其上传到对象存储上?

我通过在参数中提供路径成功上传了一个文件,但这里只有base64数据。

有没有人有什么要说的?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-10-18 15:29:47

使用对象存储节点SDK:

https://github.com/ibm-bluemix-mobile-services/bluemix-objectstorage-serversdk-nodejs

或者你也可以使用pkgcloud

代码语言:javascript
复制
/*
 * Copyright 2016 IBM Corp.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */


(function (module) {
    var pkgcloud = require('pkgcloud'),
        fs = require('fs');

    function ObjectStorage(container, credentials) {
        this.container = container;

        this.config = {
            provider: 'openstack',
            useServiceCatalog: true,
            useInternal: false,
            keystoneAuthVersion: 'v3',
            authUrl: credentials.auth_url,
            tenantId: credentials.projectId,
            domainId: credentials.domainId,
            username: credentials.username,
            password: credentials.password,
            region: credentials.region
        };

        this.client = pkgcloud.storage.createClient(this.config);
    }

    ObjectStorage.prototype.validate = function () {
        return new Promise(function (resolve, reject) {
            this.client.auth(function (error) {
                if (error) {
                    return reject(error);
                }

                resolve();
            });
        }.bind(this));
    };

    ObjectStorage.prototype.makeContainer = function () {
        return new Promise(function (resolve, reject) {

            this.client.createContainer({name: this.container}, function (error) {
                if (error) {
                    return reject(error);
                }

                return resolve();
            });

        }.bind(this));
    };

    ObjectStorage.prototype.uploadFile = function (path, name) {
        return new Promise(function (resolve, reject) {

            var myPicture = fs.createReadStream(path);

            var upload = this.client.upload({
                container: this.container,
                remote: name
            });

            upload.on('error', function (error) {
                reject(error);
            });

            upload.on('success', function (file) {
                resolve(file);
            });

            myPicture.pipe(upload);
        }.bind(this));
    };

    module.exports = ObjectStorage;
})(module);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40082116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档