2

我正在尝试使用IBM Bluemix Cloud的对象存储服务,但我无法从我的 nodejs 服务器发送图像。我怎样才能做到这一点?按照我的服务器代码:

unirest
    .post(MY_CONTAINER + new_fname)
    .headers({'Content-Type': 'multipart/form-data', 'X-Auth-Token': token})
    .field({ 'max_file_count': 1 })
    .field({ 'max_file_size': 1 })
    .attach({ 'file': file.originalname, 'relative file': streamFile })
    .end(function (resp) {
            //response
            console.log(resp.status);
            console.log(resp.body);
        });

主要问题是找到使用 API 将图像(png 或 jpg)发送到 bluemix 存储的正确方法(我已将其上传到我们的服务器)。

4

2 回答 2

5

我使用pkgcloud-bluemix-objectstorage修复了之前使用 v2 并已更改为使用 v3 的 OpenStack 身份验证错误。

这是链接 Bluemix - 对象存储 - node.js - pkgcloud - openstack 返回 401

@libik 写了一个例子。

var pkgcloud = require('pkgcloud-bluemix-objectstorage');

var fs = require('fs');

// Create a config object
var config = {};
// Specify Openstack as the provider
config.provider = "openstack";
// Authentication url
config.authUrl = 'https://identity.open.softlayer.com/';
config.region= 'dallas';
// Use the service catalog
config.useServiceCatalog = true;
// true for applications running inside Bluemix, otherwise false
config.useInternal = false;
// projectId as provided in your Service Credentials
config.tenantId = '234567890-0987654';
// userId as provided in your Service Credentials
config.userId = '098765434567890';
// username as provided in your Service Credentials
config.username = 'admin_34567890-09876543';
// password as provided in your Service Credentials
config.password = 'sdfghjklkjhgfds';

**//This is part which is NOT in original pkgcloud. This is how it works with newest version of bluemix and pkgcloud at 22.12.2015. 
//In reality, anything you put in this config.auth will be send in body to server, so if you need change anything to make it work, you can. PS : Yes, these are the same credentials as you put to config before. 
//I do not fill this automatically to make it transparent.
config.auth = {
    forceUri  : "https://identity.open.softlayer.com/v3/auth/tokens", //force uri to v3, usually you take the baseurl for authentication and add this to it /v3/auth/tokens (at least in bluemix)    
    interfaceName : "public", //use public for apps outside bluemix and internal for apps inside bluemix. There is also admin interface, I personally do not know, what it is for.
    "identity": {
        "methods": [
            "password"
        ],
        "password": {
            "user": {
                "id": "098765434567890", //userId
                "password": "sdfghjklkjhgfds" //userPassword
            }
        }
    },
    "scope": {
        "project": {
            "id": "234567890-0987654" //projectId
        }
    }
};**

//console.log("config: " + JSON.stringify(config));

// Create a pkgcloud storage client
var storageClient = pkgcloud.storage.createClient(config);


// Authenticate to OpenStack
storageClient.auth(function (error) {
    if (error) {
        console.error("storageClient.auth() : error creating storage client: ", error);
    } else {
        
        //OK
        var new_fname = dir + "__" + file.originalname;
        var readStream = fs.createReadStream('uploads/' + file.filename);
        var writeStream = storageClient.upload({
            container: 'chat-files',
            remote: new_fname
        });

        writeStream.on('error', function(err) {
            // handle your error case
            console.log("concluido o upload com erro!");
            console.log(err);
        });

        writeStream.on('success', function(file) {
            // success, file will be a File model
            console.log("concluido o upload com sucesso!");
        });

        readStream.pipe(writeStream);                
    }
});

于 2016-01-27T12:23:36.483 回答
3

@JeffSloyer 编写了一个 Node.js 示例应用程序来将文件上传到 Bluemix 中的一个对象存储实例。

你可以在这里找到代码:

https://github.com/IBM-Bluemix/node-file-upload-swift

上面的代码无法使用 Open Stack Swift v3 进行身份验证,因此我对 skipper-openstack 模块进行了修改以使用pkgcloud-bluemix-objectstorage

https://github.com/adasilva70/skipper-openstack.git#adasilva70-patch-1

克隆 Jeff 的存储库并按照 README.md 文件中的说明运行代码。确保使用以下文件修改 package.json 文件以获取我的更改:

{
  "name": "node-file-upload-swift",
  "version": "0.0.0",
  "dependencies": {
    "bower": "^1.7.1",
    "cf-deployment-tracker-client": "*",
    "cfenv": "^1.0.3",
    "dotenv": "^1.2.0",
    "express": "~4.x",
    "skipper": "^0.5.8",
    "skipper-openstack": "git+https://github.com/adasilva70/skipper-openstack.git#adasilva70-patch-1",
    "stream": "0.0.2",
    "underscore": "^1.8.3"
  },
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "postinstall": "bower install --allow-root --config.interactive=false"
  }
}
于 2016-01-26T04:21:41.623 回答