Question:
According to the docs, contentType is optional and it will attempt to determine the correct mime-type based on the file extension. However, it never seemed to guess the mime-type and always defaults to application/octet-stream
Here’s my code:
1 2 3 4 5 6 7 |
$s3 = new AmazonS3(); $opt = array( 'fileUpload' => $_FILES['file']['tmp_name'], 'storage' => Amazons3::STORAGE_REDUCED); $r = $s3->create_object('mybucket', $_FILES['file']['name'], $opt); |
Here’s a screenshot of my AWS console:
How do you actually automatically set the proper Content Type without setting contentType option, or do you really have to set it manually?
Additional info: If I download a file from the console (that I originally uploaded through SDK) then upload it again using the console, the proper Content-Type is set (ex: image/gif for GIF files instead of application/octet-stream)
Answer:
Try to add ‘contentType’ key to your options array when creating object and use ‘body’ instead of ‘fileUpload’. For example:
1 2 3 4 5 |
$s3->create_object('MY_BUCKET', 'xml_file.xml', array( 'body' => ' 'contentType' => 'text/xml' )); |
Successfully creates file with text/xml content type. As I know Content-Type isn’t set automatically when uploading through SDK. But where’s the problem to derminate mime type from PHP side and set the ‘contentType’ property?