Created
April 29, 2014 16:56
-
-
Save talentedmrjones/11406047 to your computer and use it in GitHub Desktop.
NodeJS upload file to Softlayer Object Storage Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var | |
fs = require('fs'), | |
path = '/path/to/example.txt', | |
stat = fs.statSync(path), // so we can get file size | |
request = require('request'), // npm install request | |
authEndpoint = 'https://dal05.objectstorage.softlayer.net/auth/v1.0/', // region specific endpoint | |
apiKey = "your Api Key", | |
user = "your Username", | |
container = "your container", | |
filename = "example.txt" | |
; | |
// first get upload endpoint and access token | |
request.get({url:authEndpoint, headers:{"X-Auth-Key":apiKey,"X-Auth-User": user}}, function (err, res) { | |
var data = JSON.parse(res.body); | |
var objectPath = data.storage.public+'/'+container+'/'+filename; | |
// now upload the file | |
var file = fs.createReadStream(path) | |
.pipe(request.put({url: objectPath, headers:{'Content-Length': stat.size, "X-Auth-Token":res.headers['x-auth-token']}}, function(err, res, body){ | |
if(err) { | |
console.log('error', err); | |
} else { | |
console.log('status', res.statusCode); | |
} | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment