Skip to content

Instantly share code, notes, and snippets.

@newbdez33
Created November 26, 2012 01:08
Show Gist options
  • Save newbdez33/4146075 to your computer and use it in GitHub Desktop.
Save newbdez33/4146075 to your computer and use it in GitHub Desktop.
amazon s3 get file size/change content type (v1/v2)
//3. Deal with s3/file map
/*use Aws\Common\Aws;
use Aws\Common\Enum\Region;
$aws = Aws::factory(array(
'key' => AWS_ACCESS_KEY,
'secret' => AWS_SECRET_ACCESS_KEY,
'region' => Region::US_EAST_1,
'scheme' => 'http' //https will fail, WTF??!
));
$s3 = $aws->get('s3');*/ //SDK V2
$s3 = new AmazonS3(array(
'key' => AWS_ACCESS_KEY,
'secret' => AWS_SECRET_ACCESS_KEY,
));
$sqs = new AmazonSQS(array(
'key' => AWS_ACCESS_KEY,
'secret' => AWS_SECRET_ACCESS_KEY,
));
try {
//get file meta data
//$file_meta = $s3->headObject( array('Bucket'=>AWS_S3_BUCKET, "Key"=>$key) );
//$file_meta->get('ContentLength')>0
$file_size = $s3->get_object_filesize(AWS_S3_BUCKET, $key, false);
//set file content-type
$file_ext = pb_getFileExt($file_name);
$content_type = $mime_types[$file_ext];
if($content_type) {
$s3->change_content_type(AWS_S3_BUCKET, $key, $content_type);
// $s3->setDefaultHeaders(array("Content-Type"=> $content_type));
// $s3->copyObject(array(
// "Bucket"=>AWS_S3_BUCKET,
// "Key"=>$key,
// "CopySource"=>urlencode(AWS_S3_BUCKET.'/'.$key),
// "MetadataDirective"=>"REPLACE"
// ));
}
} catch(Exception $e) {
$ret['msg'] = $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment