图像下载而不是使用亚马逊s3在浏览器中显示 - Codeigniter

时间:2015-07-28 05:27:39

标签: php codeigniter amazon-s3

我试图将图像上传到S3亚马逊,当我通过表单提交时,它会成功上传,但是当我点击图片的网址时,它会下载图片而不是在浏览器中显示图片,请检查我的代码,我等待您的回复。

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();

3 个答案:

答案 0 :(得分:2)

我追踪自己,最后我解决了这个问题。简单地说,我们需要在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.";
                }

所以,它的工作非常好。

由于

答案 1 :(得分:0)

Insted of printf($url);试试这个

echo "<a href='$url'>$url</a>";

答案 2 :(得分:0)

我在我的存储桶中使用了以下代码。试试它是否适合你。

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