Created
July 31, 2014 14:16
-
-
Save fm/88e63319be9b909b1199 to your computer and use it in GitHub Desktop.
Script to upload tweetbot images to Rackspace's Cloud Files CDN and then tweet the URL. Special thanks to Scott Smith, Jesse Noller and Mikey Salazar.
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
<?php | |
require 'vendor/autoload.php'; //requires composer, for more information visit developer.rackspace.com | |
use OpenCloud\Rackspace; | |
// Instantiate a Rackspace client. | |
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array( | |
'username' => 'XXX', | |
'apiKey' => 'XXX' | |
)); | |
// 2. Obtain an Object Store service object from the client. | |
$region = 'DFW'; // change this to the region where your container is | |
$objectStoreService = $client->objectStoreService(null, $region); | |
// 3. Get container. | |
$container = $objectStoreService->getContainer('SET CONTAINER NAME'); | |
// 4. Generate Random File Name | |
$seed = str_split('abcdefghijklmnopqrstuvwxyz' | |
.'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
.'0123456789'); | |
shuffle($seed); // probably optional since array_is randomized; this may be redundant | |
$rand = ''; | |
foreach (array_rand($seed, 5) as $k) $rand .= $seed[$k]; | |
$path = $_FILES['media']['name']; | |
$ext = pathinfo($path, PATHINFO_EXTENSION); //Grab original extension | |
// 5. Upload an object to the container. | |
$localfile = $_FILES['media']['tmp_name']; | |
$remoteFileName = $rand . "." . $ext; | |
$fileData = fopen($localfile, 'r'); | |
$container->uploadObject($remoteFileName, $fileData); | |
// Note that while we call fopen to open the file resource, we do not call fclose at the end. | |
// The file resource is automatically closed inside the uploadObject call. | |
$outputFilename = 'http://img.domain.com/' . basename($remoteFileName); //Remember: to use a custom URL, you must setup a CNAME to point to your CDN URL | |
$response = array(url=>$outputFilename); | |
echo json_encode($response); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment