aws icon indicating copy to clipboard operation
aws copied to clipboard

S3 putObject does not set Content-Type

Open martinssipenko opened this issue 2 years ago • 1 comments

In contrast to the official AWS SDK Async AWS does not set content type when uploading files to S3, is this by design?

martinssipenko avatar Mar 16 '22 08:03 martinssipenko

I noticed this few weeks ago. I do believe, this is not the responsibility of the SDK to arbitrary change attributes. This could be added in the SimpleS3Client. But should be left empty (and let the app decide the right ContentType)

Here is my code:

use Symfony\Component\Mime\MimeTypes;

$mimeType = new MimeTypes();

$responses[] = $this->s3Client->putObject([
  'Bucket' => $this->bucket,
  'Key' => $file->getRelativePathname()
  'Body' => fopen($file->getRealPath(), 'rb'),
  'ACL' => 'public-read',
  'CacheControl' => 'max-age=31536000',
  'ContentType' => $mimeType->getMimeTypes($file->getExtension())[0] ?? $mimeType->guessMimeType($file->getRealPath()) ?? 'application/octet-stream',
]);

note: I also noticed that AWS has a bug when uploading the object in multiparts (big objects). In such situations, AWS automatically adds empty ContentEncoding attributes, which breaks clients (like chrome/firefox browsers). You need to disable chunk uploads via the configuration sendChunkedBody=false.

jderusse avatar Mar 16 '22 09:03 jderusse