我正在尝试上传图片到Amazon S3,当我通过表单提交时,它会上传成功,但是当我点击图片的url时,它会下载图像而不是在浏览器中显示图像,请检查我的代码,我等待您的回复。
if ($s3->putObjectFile($fileTempName, "bucket/images", $fileName,
S3::ACL_PUBLIC_READ)) {
echo "We successfully uploaded your file.";
}else{
echo "Something went wrong while uploading your file... sorry.";
}
// Get the contents of our bucket
$url = "http://bucket.s3.amazonaws.com/images/".$fileName;
printf($url);
exit();
我自己跟踪,最后我解决了这个问题。简单地说,我们需要在putObjectFile()参数中添加[array $metaHeaders = array()], [string $contentType = null],通过此图像将在浏览器中打开而不是下载。因此,现在我更改了代码:
$fileName = $_FILES['file']['name'];
$fileTempName = $_FILES['file']['tmp_name'];
$contentType = $_FILES['file']['type'];
$metaHeaders = array();
if($s3->putObjectFile($file, "bucket", $filename, S3::ACL_PUBLIC_READ, $metaHeaders, $contentType)){
echo "We successfully uploaded your file.";
}
else{
echo "Something went wrong while uploading your file... sorry.";
}
所以,它工作得非常好。
谢谢
代替printf($url);
try this
echo "<a href='$url'>$url</a>";
我为我的桶使用了以下代码。如果它适合你,试试吧。
<?php $client = 'Aws'S3'S3Client::factory(array(
'key' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'region' => 'XXXXXXXXXXXXXXXXX',
));
function getDocFromS3($file, $doc_type){
global $client;
$bucket_dir = getBucketName($doc_type);
/*$result = $client->getObject(array(
'Bucket' => $bucket_dir,
'Key' => $file
));*/
$file = "{$bucket_dir}/{$file}";
$result = $client->get($file);
$url = $result->getURL();
return $url;
}
function getBucketName($doc_type){
if(DEV || LCD){
$bucket_name = 'public-estify-dev';
} else {
$bucket_name = 'public-estify';
}
return $bucket_name.'/evm/docs/'.$doc_type;
} ?>