简体   繁体   中英

Amazon S3 files uploaded using AWS SDK for PHP is always “application/octet-stream”?

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:

$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)

Try to add 'contentType' key to your options array when creating object and use 'body' instead of 'fileUpload'. For example:

$s3->create_object('MY_BUCKET', 'xml_file.xml', array(
                                        'body' => '<xml>Valid xml content</xml>',
                                        '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?

I have just run into this situation where, when uploading some files, the Content-Type was not automatically detected. I couldn't figure out what the cause was until I stepped through the code watching the SDK execution. I discovered that the CFMimeTypes::get_mimetype() function, which is used to automatically determine the Content-Type, uses a case sensitive comparison to determine the MIME type. Because of this only lowercase extensions will match. If your file extension is upper or mixed case, then it will not match and fallback to the 'application/octet-stream' MIME type.

To fix change the line:

'fileUpload'    => $_FILES['file']['tmp_name']

to

'fileUpload'    => strtolower( $_FILES['file']['tmp_name'] )

I am using the SDK v 1.5.8.2, but there doesn't look to be any changes to this detection in the 1.5.9 or 1.5.10 releases that have both come out in the past week. I consider this to be a bug and will be filing it as such.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM