I was asked about listing everything in a DreamObjects bucket, which is practically the same thing as listing everything in an Amazon S3 bucket, just on a different server.

This isn’t as easy as we’d like, because while there is a nice Amazon S3 PHP class we can use (and I forked it and flipped it to be a DreamObjects PHP Class), telling people they have to download the S3.php file and then make this separate PHP file is a little annoying.

//include the S3 class
if (!class_exists('S3'))require_once('S3.php');

//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'MYACCESSKEY');
if (!defined('awsSecretKey')) define('awsSecretKey', 'MYSECRETKEY');

//Bucket Info
$bucketName = 'MYBUCKETNAME';

/* STOP EDITING HERE */

//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);


// Get the contents of our bucket
$bucket_contents = $s3->getBucket($bucketName);

foreach ($bucket_contents as $file){

    $fname = $file['name'];
    $furl = 'http://'.$bucketName.'.objects.dreamhost.com/'.$fname;

    //output a link to the file
    echo '<a href=\'$furl\'>$fname</a><br />';
}

I know, it’s not too terribly complicated, but it is annoying to make two files for that, and my DreamObjects listing page is very barebones. You could style it with CSS, make it show the image, make it show in a grid, or even show the file size and have it pop up in a new window.

It does highlight one of my issues with CEPH (AWS whatever) storage, and that it’s still not quite friendly enough. It dumps us back to the old days where we FTP’d our images up to a server and then manually crafted our links. Certainly there are plugins for WordPress that can help us with that, but in general it requires knowing code (or where to get the code) in order to display content.

Reader Interactions

Comments

  1. I had to do some research to figure out what this post is about. It looks like you guys have created some sort of alternative to S3, with a compatible API.

    I’m not sure how compatible your API is, but there are ways to mount S3 drives. If you could do that, then you could treat them as part of a regular old local drive and not require any messing around with PHP.

    I use the CLI tool S3CMD quite extensively. It’s a nifty little thing that lets you shunt data to and fro from S3 without any of the problems associated with doing it in PHP (memory limits in particular are easily hit in PHP, but in general I don’t think PHP is anything like as reliable as doing this stuff directly from a the CLI). I have no idea if it works with non-S3 CEPH systems though.

    Related: A little warning about storing API credentials inside PHP files … http://wptavern.com/ryan-hellyers-aws-nightmare-leaked-access-keys-result-in-a-6000-bill-overnight

%d bloggers like this: