2014-03-24 48 views
1

我使用AWS PHP SDK v2.x使用下面的代碼複製S3桶之間進行對象:如何使用AWS PHP SDK v2和CopyObject設置內容處置?

<?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'; 
} 

文件複製到S3存儲沒有任何錯誤,但我無法設置的Content-Disposition爲了強制瀏覽器下載文件而不是流式傳輸。我已經嘗試了幾個選項(註釋如上),但似乎沒有任何工作。

我甚至嘗試了Metadata數組中傳遞的價值,即使documentation說,否則但AWS Console列出了Content-Disposition下的元數據部分。

如果我手動更改AWS Console中的Content-Disposition,則瀏覽器按預期下載文件。

那麼,如何正確地將值傳遞給S3對象呢? 可以我通過它使用CopyObject()方法?

回答

7

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', 
)); 

注:該代碼是在這個職位也是在Apache許可證下的2.0版本授權。

+1

'ContentDisposition'只需要指定它的「附件」值,但是'MetadataDirective'是缺少的信息。 –