LoginSignup
0

More than 3 years have passed since last update.

AWS-SDK PHPでS3バケット上のファイルをコピーするときにContent-Typeを変更する

Last updated at Posted at 2020-10-14

S3に既に配置済みのファイルのContent-Typeを変更する際に苦戦したので、自分用のメモ

結論

copyObjectを使用する際に、オプションでContentType, MetadataDirectiveの2つを指定する。

$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1'
]);

$s3->copyObject([
    'Bucket'     => $targetBucket,
    'Key'        => "{$sourceKeyname}-copy",
    'CopySource' => "{$sourceBucket}/{$sourceKeyname}",
    'ContentType' => 'binary/octet-stream', // ※
    'MetadataDirective' => 'REPLACE', // ※
]);

MetadataDirective:REPLACEを指定しないと、S3に配置されている元ファイルのContent-Typeを引き継ぐみたいなので注意

参考URL

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0