Monday, October 4, 2010

Rambling Code

I've been experimenting with Amazon S3 service for one of my latest projects and didn't see many examples out there so I thought I would share some code snippets. Being cheap I wanted trying to keep my storage costs down so I opted to use the reduced redundancy feature of S3. I was having trouble getting the change storage type function to work correctly when I discovered that the following code will work to reduce the storage space when you upload it. Here are two sample lines that will work to save you some money on S3.
To upload new objects with reduced redundancy set use the following line of code.

$result = $s3->batch()->create_object($bucket, $filename, array( 'fileUpload' =>$file, 'storage'=>'REDUCED_REDUNDANCY' ) );

To change existing objects redundancy setting .

$result = $s3->change_storage_redundancy( $bucket, $filename, 'REDUCED_REDUNDANCY' );

Here is how to list all of the objects in a bucket. Not recommended if you have lots objects.

$result = $s3->list_objects($bucket);
foreach( $result->body->Contents as $r ) {

   echo $r->Key;
}

I'm using the Simple PHP S3 Class. I hope you have found this useful.

No comments: