我正在将我的代码从 AWS PHP SDK1 迁移到 SDK2 ( https://github.com/aws/aws-sdk-php )。
我有一个图片上传器。
在我之前的版本中,我会像这样指定我的图像的 Content-Type:
$response = $this->s3->create_object(
$bucket,
$key,
array(
'fileUpload'=>$file_resource,
'contentType'=>$mime,
'acl' => AmazonS3::ACL_PUBLIC,
)
);
这是我的新版本:
$response = $this->s3->upload(
$bucket,
$key,
$file_resource,
'public-read',
array('params' => array('Metadata' => array('ContentType'=>$mime)))
);
我尝试了 ContentType 的不同拼写,在 S3 站点中,它将名称修改为“x-amz-meta-contenttype”,而“Content-Type”的值是默认的“binary/octet-stream”。
我也尝试过使用 EntityBody 功能,但结果相同:
$response = $this->s3->upload(
$this->bucket,
$to,
EntityBody::factory($file_resource),
'public-read',
array('params' => array('Metadata' => array('ContentType'=>$mime)))
);
如何在这个新 API 中设置内容类型?
编辑:我在文档中看到某处:
The AWS SDK for PHP will attempt to automatically determine the most appropriate Content-Type header used to store the object. If you are using a less common file extension and your Content-Type header is not added automatically, you can add a Content-Type header by passing a ContentType option to the operation.
首先,我正在上传简单的图像,但根据我的 S3 仪表板,它们被上传为“二进制/八位字节流”。关于他们的第二点,我尝试了许多带有“ContentType”的数组组合,我不确定为什么它不起作用......
最佳答案
最后,这组选项有效:
array('params' => array('ContentType' => $mime))
不需要元数据
关于amazon-web-services - 在 AWS PHP 的上传功能中指定 Content-Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21892352/