Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rackspace cloud files: Image Upload to rackspace cloud files using PHP

I am doing a project where user can upload images like profile image or image for their image gallary. right now its uploading all the images to my server.

Now, i want to upload all this image to my rackspace cloud files directly using php script. For example:

  1. user select a file
  2. press submit with some information
  3. the selected file will be uploaded to the rackspace server and return the file location.
  4. then the file location along with other information will be saved in my database.
  5. then i will show the file or image from that location.

so do u have any idea, exactly how i can do this?

i am using:

  • Codeigniter framework
  • jQuery as javascript library

thanks in advance for any answer.

like image 515
M.M.H.Masud Avatar asked Feb 06 '10 20:02

M.M.H.Masud


People also ask

How do I upload files to Rackspace?

To upload files to one of your containers, first log in to the Cloud Control Panel. In the top navigation bar, select Storage > Files and click on the name of the container to which you want to upload files.

What is Rackspace Cloud Files?

Rackspace Cloud Files™ is an affordable, redundant, scalable, and dynamic storage service. The core storage system is designed to provide a secure, network-accessible way to store an unlimited number of files. Each file can be as large as 5 gigabytes.

Where is Rackspace data stored?

Its data centers are located in Amsterdam (Netherlands), Virginia (USA), Chicago (USA), Dallas (USA), London (UK), Frankfurt (Germany), Hong Hong (China), Kansas City (USA), New York City (USA), San Jose (USA), Shanghai (China), Queenstown (Singapore), and Sydney (Australia). Rackspace Technology, Inc.


2 Answers

Update: Although the method that I describe below will still work, it is not possible to upload files directly using tokens (thanks to DesignerGuy for that info). You can read more about how to do that here. I should also mention that services like transloadit now make this sort of thing trivially easy using jquery inserts into HTML forms. Still the method that I describe below still works and probably gives you the most control.

You cannot load files -directly- to Rackspace Files, without trying something like embedding your Rackspace credentials into the form. Even if this were possible it would not be a good idea. (i.e. the user could add massive content using your credentials that have nothing to do with your application). So to get what you want, which is essentially to have the file stored at Rackspace, and the location of the file stored in your server you need to modify your work flow. It needs to be:

  1. Form uploads file to -your server-
  2. -your server- calculated the container and filename that the file will use
  3. -your server- uploads the temporary file to rackspace files

If you want the file to be publically downloadable...

  1. -your server- enables the file to be served via the CDN, making a public URL for the file that your user can access.
  2. when your server creates html it embeds the CDN urls and the user magically gets the files

If you want the file to be only downloadable by -certain- users....

  1. -your server- authenticates with rackspace and downloads the file temporarily
  2. -your server- serves the file AND the HTML both from your server

You do not pay for bandwidth between -your server- and rackspace files, if -your server- is also a rackspace cloud instance or a regular Rackspace Managed Server (or at least that is what customer support has told me). An important fact when calculating the rates between Amazon and Rackspace.

So you can either use Rackspace Files as a massive hard-drive for your server, which gives you control over access, or you can use it as a massive public distribution network... But in both cases you might need to change the order of your steps to give you what you want...

You might also look at a simple example of using php to upload a file gathered from an HTML form to rackspace files.

like image 192
ftrotter Avatar answered Oct 08 '22 05:10

ftrotter


A lot of these answers are old and looking at Orbit's answer I think there must have been many changes to the API since then. And since ftrotter already explained about trying to upload directly, I will show you an updated version (PHP 5.3 style) of how to send the files from the PHP tmp directory.

First we'll assume you already installed the API using composer.

First you'll need to include their autoloader and then add the use namespace.

require 'vendor/autoload.php';

use OpenCloud\Rackspace as Rackspace;

Then you simply setup the client instance

// first setup the client using your portal username and API key
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
    'username' => 'YOUR-USERNAME',
    'apiKey'   => 'YOUR-API-KEY'
));

$region = 'DFW'; // can be ORD or various others and must be set

// now get the container
$container = $client->objectStoreService('cloudFiles', $region)
    ->getContainer('YOUR-CONTAINER');

Now that you have your container you can simply setup your array of files and upload them. Here's a quick and dirty way:

if (isset($_FILES)) {
    $files = array();
    foreach ($_FILES as $file) {
        if (0 === $file['error']) {
            $files[] = array(
                'name' => $file['name'],
                'path' => $file['tmp_name']
            );
        }
    }

    if (count($files)) {
        // upload files to Rackspace Cloud Files container
        $result = $container->uploadObjects($files);
    }
}

Now you your files will keep their existing names when they end up in the container, and you can get the public URL path for your container right from within the Rackspace Cloud portal.

like image 41
Yes Barry Avatar answered Oct 08 '22 03:10

Yes Barry