amazon-web-services - 在 AWS PHP 的上传功能中指定 Content-Type

标签 amazon-web-services amazon-s3 aws-php-sdk

我正在将我的代码从 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/

相关文章:

java - 为 MySQL 命令行客户端加密连接生成 SSL key

amazon-web-services - Redshift 复制命令递归扫描

mongodb - 从外部连接到 AWS DocumentDB(不是从 VPC 或 ssh 隧道)

Laravel Aws\Laravel\AwsServiceProvider::class 未找到

amazon-web-services - 如何使用 php aws sdk 复制整个 s3 文件夹?

amazon-web-services - 如何将 MFA 与 AWS CLI 结合使用?

amazon-web-services - AWS Cloudformation IoTAnalytics ServiceManagedS3 YAML

java - 如何下载位于 S3 存储桶上的整个文件夹?

python-3.x - 如何使用 asyncio 下载 s3 bucket 上的文件

amazon-s3 - AWS CloudWatch Events Rule是否支持S3存储桶/ key 名称中的任何通配符