Skip to content

Instantly share code, notes, and snippets.

@bromanko
Created February 20, 2012 16:28
Show Gist options
  • Save bromanko/1870000 to your computer and use it in GitHub Desktop.
Save bromanko/1870000 to your computer and use it in GitHub Desktop.
Cloudfiles Copy Container
var cloudfiles = require('cloudfiles');
request = require('request');
// CloudFiles client configuration
var configCloudFiles = {
auth: {
username: '',
apiKey: '',
host: 'auth.api.rackspacecloud.com'
}
};
// Get the container arguments
var sourceContainerName = '';
var destContainerName = '';
if (process.argv.length > 2) sourceContainerName = process.argv[2];
if (sourceContainerName == '') return console.error('Invalid source container');
if (process.argv.length > 3) destContainerName = process.argv[3];
if (destContainerName == '') return console.error('Invalid destination container');
// Create the client and auth
var client = cloudfiles.createClient(configCloudFiles);
client.setAuth(function() {
// Get the source container
client.getContainer(sourceContainerName, function (err, sourceContainer) {
if (err) return console.error(err);
// Create the destination container
client.createContainer(destContainerName, function (err, destContainer) {
// Get all the files in the source container
sourceContainer.getFiles(function (err, files) {
if (err) return console.error(err);
var completed = 0,
total = files.length;
files.forEach(function(file) {
console.log('Copying ' + file.fullPath);
var params = {
url: encodeURI(file.fullPath),
method: 'COPY',
headers: {
'X-Auth-Token': client.config.authToken,
'Destination': encodeURI('/' + destContainerName + '/' + file.name)
}
};
request(params, function(err, res, body) {
++completed;
console.log('COPY (' + completed + ' of ' + total + ') complete: ' + params.url);
if (err) {
console.error(err);
} else if (res.statusCode != 201) {
console.error('Hmm, the copy failed: ' + res.statusCode);
}
});
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment