Last active
January 2, 2018 04:26
-
-
Save kenperkins/6595886 to your computer and use it in GitHub Desktop.
A pkgcloud storage example with Rackspace CloudFiles
This file contains hidden or 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 pkgcloud = require('pkgcloud'), | |
config = require('../config').getConfig(), | |
filed = require('filed'), | |
readline = require('readline'); | |
var rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
// Setup some variables for use in our examples | |
var containerName = 'pkgcloud-example', | |
filePath = '/Users/kenn5998/src/scratch/', | |
fileName = 'hello.txt'; | |
// create our client for the DFW region | |
var client = pkgcloud.providers.rackspace.storage.createClient({ | |
username: config.username, | |
apiKey: config.apiKey, | |
region: 'DFW' | |
}); | |
// convenience method to exit on error if found | |
function handleErr(err) { | |
if (err) { | |
console.dir(err); | |
process.exit(1); | |
return; | |
} | |
} | |
// | |
// start our example with create container | |
// | |
// | |
createContainer(); | |
// creates a container, and if successfull contintues to uploadFile | |
function createContainer() { | |
client.createContainer(containerName, function(err, container) { | |
handleErr(err); | |
console.log('Created container: ' + container.name); | |
uploadFile(container); | |
}); | |
} | |
// uploads a file, and if successful enables CDN on the container | |
function uploadFile(container) { | |
client.upload({ | |
container: container, | |
remote: fileName, | |
stream: filed(filePath + fileName) | |
}, function(err) { | |
handleErr(err); | |
console.log('Uploaded file: ' + filePath + fileName); | |
enableCdnForContainer(container); | |
}); | |
} | |
// enables CDN on the container, if successful downloads the file | |
function enableCdnForContainer(container) { | |
container.enableCdn(function(err, container) { | |
handleErr(err); | |
console.log('Enabled CDN on container: ' + container.name); | |
console.log('CDN Url: ' + container.cdnUri); | |
downloadFile(container); | |
}); | |
} | |
// downloads the file. If successful prompts before deleting | |
function downloadFile(container) { | |
client.download({ | |
container: container, | |
local: filePath + fileName + '.download', | |
remote: fileName | |
}, function(err) { | |
handleErr(err); | |
console.log('Downloaded file: ' + filePath + fileName + '.download'); | |
// prompt the user before deleting, so we can see them in the CP | |
rl.question('\nPress enter to delete container and object...', function () { | |
rl.close(); | |
deleteObject(container); | |
}); | |
}); | |
} | |
// delete the file | |
function deleteObject(container) { | |
client.removeFile(container, fileName, function(err) { | |
handleErr(err); | |
console.log('Deleted remote object: ' + fileName); | |
deleteContainer(container); | |
}); | |
} | |
// delete the container | |
function deleteContainer(container) { | |
client.destroyContainer(container, function(err) { | |
handleErr(err); | |
console.log('Deleted container: ' + container.name); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment