2015-11-02 140 views
1

我正在嘗試向我的用戶提供下載文件選項。我正在致力於AWS EC2AWS PHP SDK V2.8。我可以在我的網站上顯示圖像。我根據問題force-download-with-php-on-amazon-s3嘗試,但沒有成功。這個問題的大部分答案都很古老。我正在使用下面的代碼進行上傳從AWS S3下載文件到客戶端系統

try { 
    $result = $s3->putObject(array(
     'Bucket' => $bucketName, 
     'ACL' => 'authenticated-read', 
     'Key' => "s3112.png", 
     'ServerSideEncryption' => 'AES256', 
     'SourceFile' => $filepath, 
     'ContentType' => mime_content_type($filepath), 
     'debug' => [ 
      'logfn' => function ($msg) { 
       echo $msg . "\n"; 
      }, 
      'stream_size' => 0, 
      'scrub_auth' => true, 
      'http' => true, 
     ], 
    )); 
} catch (S3Exception $e) { 
    echo $e->getMessage() . "\n"; 
} 

這是什麼嘗試。

header('Content-disposition: attachment; filename=s3112.png'); 
header('Content-type: image/png'); 
readfile("https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.png"); 

//header("Location: https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.png"); 
//// Location redirection to a MP3 that lets the browser decide what to do. 
//header("Location: https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.png"); 

我試着用

<a href="https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.png" download> 

enter image description here

,但沒有成功。任何幫助欣賞。

回答

1

如果該對象具有公開閱讀權限,則應該只能鏈接到該對象。 您還可以使用bucket policy來設置對存儲桶中所有對象的讀取訪問權限。

你也可以重定向到對象的S3 presigned URL

$cmd = $s3Client->getCommand('GetObject', [ 
    'Bucket' => 'my-bucket', 
    'Key' => 'testKey' 
]); 

$request = $s3Client->createPresignedRequest($cmd, '+20 minutes'); 

// Get the actual presigned-url 
$presignedUrl = (string) $request->getUri(); 

header('Location: ' . $presignedUrl); 
+0

,你在我的問題檢查我使用'AES256'用於加密我的文件。所以如果有任何方式下載它們。 – urfusion

+0

S3服務器端加密適用於數據「靜止」,不應影響如何檢索對象。 「加密數據的解密不需要您的努力,當您獲取加密對象時,我們會提取並解密密鑰,然後用它來解密您的數據。」 (來源https://aws.amazon.com/blogs/aws/new-amazon-s3-server-side-encryption/) –