I have searched high and low for how to add a simple ContentType metadata to a MultiPartUploader in AWS PHP SDK. The docs specifically mention how to do it for PutObject, but have no mention of how to do it with a MultiPartUploader.
I have tried every method I could find online, but nothing is working and my file is always getting set to application/octet-stream.
Does anyone have any experience with this? Here is my code:
$uploader = new MultipartUploader($this->s3, $local_path, [
'bucket' => env('AWS_BUCKET'),
'key' => $path .'/'. $filename,
'contentType' => 'video/mp4', // have tried this
'metaData' => [ // have tried this (all well as all variations of caps)
'contentType' => 'video/mp4',
]
]);
do
{
try
{
$result = $uploader->upload();
}
catch (MultipartUploadException $e)
{
$uploader = new MultipartUploader($this->s3, $local_path, [
'state' => $e->getState(),
]);
}
}
while (!isset($result));
After reading through the MultiPartUploader class, it looks like the ContentType should be automatically set, but it is not being set.
I am using v3 of the SDK
Any advice would be greatly appreciated!
I figured it out right after I posted this question! I will post the answer so that if anyone else hits this problem, they can look here.
The page I linked in the question has the answer, just not specifically related to ContentType!
Here is the working code:
$uploader = new \Aws\S3\MultipartUploader($this->s3, $local_path, [
'bucket' => env('AWS_BUCKET'),
'key' => $path .'/'. $filename,
'before_initiate' => function(\Aws\Command $command) use ($mime_type) // HERE IS THE RELEVANT CODE
{
$command['ContentType'] = $mime_type; // video/mp4
}
]);
do
{
try
{
$result = $uploader->upload();
}
catch (\Aws\Exception\MultipartUploadException $e)
{
$uploader = new \Aws\S3\MultipartUploader($this->s3, $local_path, [
'state' => $e->getState(),
]);
}
}
while (!isset($result));
return $result ? $result['ObjectURL'] : null;
I hope this helps someone else in the future!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With