繁体   English   中英

如何将文件上传到Bluemix中的对象存储(由nodejs)

[英]How to upload file to Object Storage in Bluemix (by nodejs)

我正在尝试在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存储的正确方法(我已经将它上传到我们的服务器)。

我使用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); } }); 

@JeffSloyer编写了一个Node.js示例应用程序,用于将文件上传到Bluemix中的Object Storage实例。

你可以在这里找到代码:

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"
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM