List and upload to Amazon S3 with PHP

If you don't already have an Amazon AWS acccount, head to http://aws.amazon.com and sign up.

Once you are signed in, on the top right corner "My Account / Console" dropdown, select Security Credentials. OR from "My Account" page, on the left sidebar, select Security Credentials. Go to the Access Credentials heading and you may have to create a new key if one is not already listed.

Copy the Access Record ID and the Secret Access Key, you will need both in a short minute.

As well, grab the S3 class for PHP from tpyo's Github repository here: https://github.com/tpyo/amazon-s3-php-class

To Start:

require_once('S3.php');
$s3 = new S3('Access Key ID', 'Secret Access Key');

# This is the location on your web server of the file that you want uploaded.
$fileData = '/tmp/myUploadedFile.zip'

# Where are we going to save this on S3 and what do we want to name the file?
$s3LocationName = 'folder1/subfolder/myUploadedFile.zip'

To Upload a file:

$s3->putBucket('myBucketName', S3::ACL_PUBLIC_READ);

if ($s3->putObject($s3->inputFile($fileData), 'myBucketName', $s3LocationName)) {
	echo "Successful Upload";
} else {
	echo "Unsuccessful Upload";
}

To list all files in an S3 bucket:

$bucket_contents = $s3->getBucket('myBucketName');
# print_r($bucket_contents); # Would list everything available.

foreach ($bucket_contents as $file){
	if (!$file['size'])
		echo $file['name'];
}

Uploading to S3 and having the files automatically set to Public READ, and set to Reduced Redundancy storage.

if ($s3->putObject($s3->inputFile($fileData), 'myBucketName', $s3LocationName, S3::ACL_PUBLIC_READ, array(), array(), 'REDUCED_REDUNDANCY')) {
	echo "Successful Upload";
} else {
	echo "Unsuccessful Upload";
}

Uploading to S3 and having the files automatically set to Public READ, using Standard Storage.

if ($s3->putObject($s3->inputFile($fileData), 'myBucketName', $s3LocationName, S3::ACL_PUBLIC_READ)) {
	echo "Successful Upload";
} else {
	echo "Unsuccessful Upload";
}

Print