Reputation: 145
I am trying to use the Object Storage Service at IBM Bluemix Cloud, but I can't send images from my nodejs server. How can I do this? Follow my server code:
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);
});
The main problem is to find the right way to send an image (png or jpg) to the bluemix storage using the API (I've aready uploaded it to our server).
Upvotes: 2
Views: 2713
Reputation: 66
I used the pkgcloud-bluemix-objectstorage to fix the OpenStack authentication bug that previously used the v2 and has been changed to use the v3.
here's the link Bluemix - object storage - node.js - pkgcloud - openstack returns 401
the @libik writes an example.
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);
}
});
Upvotes: 5
Reputation: 4590
@JeffSloyer wrote a Node.js sample application to upload files to an Object Storage instance in Bluemix.
You can find the code here:
https://github.com/IBM-Bluemix/node-file-upload-swift
The code above fails to authenticate using Open Stack Swift v3, so I made a modification to the skipper-openstack module to use pkgcloud-bluemix-objectstorage:
https://github.com/adasilva70/skipper-openstack.git#adasilva70-patch-1
Clone Jeff's repository and follow the instructions in the README.md file to run the code. Make sure you modify the package.json file with the one below to get my changes:
{
"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"
}
}
Upvotes: 3