如何使用AWS PHP SDK v2和CopyObject设置Content-Disposition?

Kir*_*ard 0 php amazon-s3 aws-php-sdk

我正在使用AWS PHP SDK v2.x使用以下代码在S3存储桶之间复制对象:

<?php
try {
    self::$aws->copyObject(array(
        'Bucket'     => $target_bucket,
        'Key'        => $target_key,
        'CopySource' => $source_bucket . '/' . $source_key,
        'Content-Disposition' => 'attachment',
        //'Content-Disposition' => 'attachment; filename="' . basename($target_key) . '"'
        //'ContentDisposition' => 'attachment'
        //'ContentDisposition' => 'attachment; filename="' . basename($target_key) . '"'
    ));
} catch (Aws\S3\Exception\S3Exception $e) {
    echo 'error';
}
Run Code Online (Sandbox Code Playgroud)

该文件被复制到S3存储桶没有任何错误,但我无法设置Content-Disposition该文件以强制浏览器下载文件而不是流式传输.我尝试了一些选项(上面已经说明),但似乎没什么用.

我甚至试图传递Metadata数组中的值,即使文档另有说明,但AWS控制台列出Content-Disposition了元数据部分.

如果我Content-DispositionAWS控制台中手动更改,则浏览器会按预期下载文件.

那么,我该如何正确地将值传递给S3对象?我可以使用该CopyObject()方法传递它吗?

Jer*_*lom 8

这项CopyObject行动很棘手,IMO.我认为这个拼图的缺失部分是MetadataDirective参数.请尝试以下方法:

$s3Client->copyObject(array(
    'Bucket'     => $target_bucket,
    'Key'        => $target_key,
    'CopySource' => $source_bucket . '/' . $source_key,
    'ContentDisposition' => 'attachment; filename="'.basename($target_key).'"',
    'MetadataDirective'  => 'REPLACE',
));
Run Code Online (Sandbox Code Playgroud)

注意:此帖中的代码也是在Apache License 2.0版下获得许可的.

  • `ContentDisposition` 只需要为其指定的“附件”值即可工作,但 `MetadataDirective` 是缺少的信息。 (2认同)