Last active
December 23, 2015 06:49
-
-
Save kenperkins/6596776 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'); | |
// create our client for the DFW region | |
var client = pkgcloud.storage.createClient({ | |
provider: 'rackspace', | |
username: process.argv[2], | |
apiKey: process.argv[3], | |
region: 'DFW' | |
}); | |
var containerName = 'pkgcloud-example', | |
filePath = '/tmp/', fileName = 'hello.txt'; | |
createContainer(); | |
// creates a container | |
function createContainer() { | |
client.createContainer(containerName, function (err, container) { | |
handleErr(err); | |
console.log('Created container: ' + container.name); | |
uploadFile(container); | |
}); | |
} | |
// uploads a file | |
function uploadFile(container) { | |
client.upload({ | |
container: container, | |
remote: fileName, | |
local: filePath + fileName | |
}, function (err) { | |
handleErr(err); | |
console.log('Uploaded file: ' + filePath + fileName); | |
enableCdnForContainer(container); | |
}); | |
} | |
// enables CDN on the container | |
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 | |
function downloadFile(container) { | |
client.download({ | |
container: container, | |
local: filePath + fileName + '.download', | |
remote: fileName | |
}, function (err) { | |
handleErr(err); | |
console.log('Downloaded file: ' + filePath + fileName + '.download'); | |
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); | |
}); | |
} | |
// convenience method to exit on error if found | |
function handleErr(err) { | |
if (err) { | |
console.dir(err); | |
process.exit(1); | |
return; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment