-
-
Save gionn/806a9edec81cab2348ee to your computer and use it in GitHub Desktop.
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
node_modules |
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 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