Created
December 2, 2015 06:19
-
-
Save ktbartholomew/d5a7d8749d0b0d36aca9 to your computer and use it in GitHub Desktop.
Upload file to Rackspace Cloud Files via AJAX
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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Cloud Files Public Upload</title> | |
| </head> | |
| <body> | |
| <form id="form"> | |
| <input type="file" id="file"> | |
| <input type="submit" value="Upload"> | |
| </form> | |
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> | |
| <script> | |
| $('#form').on('submit', function (e) { | |
| e.preventDefault(); | |
| // Populate this url however you see fit, using the output of temp-url.js. | |
| // You might bake it in via a server-generated template or fetch it via AJAX. | |
| var url = 'https://storage101.dfw1.clouddrive.com/v1/...'; | |
| var file = $('#file').get(0); | |
| // AJAX upload request w/ CORS | |
| $.ajax({ | |
| url: url, | |
| method: 'PUT', | |
| data: file.files[0], | |
| processData: false, | |
| contentType: false, | |
| cache: false, | |
| success: function(d){ | |
| console.log('success:', d); | |
| }, | |
| error: function(e){ | |
| console.log('error:', e); | |
| } | |
| }); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
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
| { | |
| "dependencies": { | |
| "pkgcloud": "^1.1.0", | |
| "q": "^1.4.1", | |
| "uuid4": "^1.0.0" | |
| } | |
| } |
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 Q = require('q'); | |
| var uuid = require('uuid4'); | |
| var storageClient = require('pkgcloud').storage.createClient({ | |
| provider: 'rackspace', | |
| username: process.env.RACKSPACE_USERNAME, | |
| apiKey: process.env.RACKSPACE_APIKEY, | |
| region: process.env.RACKSPACE_REGION | |
| }); | |
| Q.ninvoke(storageClient, 'getContainer', process.env.CF_UPLOAD_CONTAINER) | |
| .then(function (container) { | |
| // Set the appropriate CORS metadata on the container. You probably don't want to do this on every request. | |
| container.metadata['access-control-allow-origin'] = process.env.CF_UPLOAD_ORIGIN; | |
| return Q.ninvoke(storageClient, 'updateContainerMetadata', container); | |
| }) | |
| .then(function () { | |
| // Set the secret key used to create temporary URLs. You probably don't want to do this on every request. | |
| return Q.ninvoke(storageClient, 'setTemporaryUrlKey', process.env.CF_UPLOAD_KEY); | |
| }) | |
| .then(function () { | |
| return Q.ninvoke(storageClient, 'generateTempUrl', process.env.CF_UPLOAD_CONTAINER, uuid(), 'PUT', 600, process.env.CF_UPLOAD_KEY); | |
| }) | |
| .then(function (container) { | |
| process.stdout.write(container); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment