Created
September 2, 2014 15:31
-
-
Save acoulton/6367ef42e3333f9ba20c to your computer and use it in GitHub Desktop.
Fix mime-type of existing S3 files
This file contains 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
{ | |
"require": { | |
"aws/aws-sdk-php": "*" | |
} | |
} |
This file contains 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 | |
use Aws\S3\S3Client; | |
use Guzzle\Service\Resource\Model; | |
ini_set('error_reporting', E_ALL); | |
require_once __DIR__.'/vendor/autoload.php'; | |
class MimeFixer | |
{ | |
protected $unknown_exts = array(); | |
protected $checked_count = 0; | |
protected $changed_count = 0; | |
public function __construct() | |
{ | |
$this->client = S3Client::factory(array( | |
'profile' => 's3_mimefixer' | |
)); | |
} | |
public function fixMimes($bucket) | |
{ | |
foreach ($this->getS3FileIterator($bucket) as $s3Object) { | |
if ($s3Object['Size'] > 0) { | |
$this->checked_count++; | |
$this->fixMime($bucket, $s3Object); | |
} | |
} | |
print "Checked ".$this->checked_count." files".PHP_EOL; | |
print "Updated ".$this->changed_count." files".PHP_EOL; | |
print_r("Unknown file extensions:"); | |
print_r($this->unknown_exts); | |
} | |
protected function getS3FileIterator($bucket) | |
{ | |
return $this->client->getIterator('ListObjects', array('Bucket' => $bucket)); | |
} | |
protected function fixMime($bucket, $s3Object) | |
{ | |
$metadata = $this->client->headObject(array('Bucket' => $bucket, 'Key' => $s3Object['Key'])); | |
$new_mime = $this->getMimeForObjectName($s3Object['Key']); | |
if ($new_mime AND ($metadata['ContentType'] !== $new_mime)) { | |
$this->setNewMimeType($bucket, $s3Object, $metadata, $new_mime); | |
} else { | |
print "✓ Matching or unknown content type for ".$s3Object['Key'].' ('.$metadata['ContentType'].')'.PHP_EOL; | |
} | |
} | |
protected function getMimeForObjectName($key) | |
{ | |
$ext = pathinfo($key, PATHINFO_EXTENSION); | |
switch ($ext) { | |
case 'jpg': | |
return 'image/jpeg'; | |
case 'htm': | |
case 'html': | |
return 'text/html'; | |
case 'm4a': | |
return 'audio/mp4'; | |
case 'mp3': | |
return 'audio/mpeg'; | |
case 'mp4': | |
return 'video/mp4'; | |
case 'flv': | |
return 'video/flv'; | |
case 'png': | |
return 'image/png'; | |
case 'xml': | |
return 'application/xml'; | |
default: | |
$this->unknown_exts[$ext][] = $key; | |
return NULL; | |
} | |
} | |
protected function setNewMimeType($bucket, $s3Object, Model $metadata, $new_mime) | |
{ | |
$this->changed_count++; | |
$key = $s3Object['Key']; | |
print "! Setting $key from ".$metadata['ContentType'].' to '.$new_mime.PHP_EOL; | |
$request = array( | |
'ACL' => 'public-read', | |
'Bucket' => $bucket, | |
'Key' => $key, | |
'CopySource' => urlencode($bucket.'/'.$key), | |
'CacheControl' => $metadata->get('CacheControl'), | |
'ContentType' => $new_mime, | |
'Metadata' => $metadata->get('Metadata'), | |
'MetadataDirective' => 'REPLACE', | |
); | |
$response = $this->client->copyObject($request); | |
} | |
} | |
$fixer = new MimeFixer(); | |
$fixer->fixMimes($argv[1]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment