【问题标题】:Image downloading instead of displaying in browser using amazon s3 - Codeigniter使用亚马逊 s3 下载图像而不是在浏览器中显示 - Codeigniter
【发布时间】:2015-10-18 12:25:06
【问题描述】:

我正在尝试将图像上传到 S3 Amazon,当我通过表单提交时,它会成功上传,但是当我点击图像的 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();

【问题讨论】:

标签: php codeigniter amazon-s3


【解决方案1】:

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

所以,它工作得非常好。

谢谢

【讨论】:

    【解决方案2】:

    printf($url); 安装试试这个

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

    【讨论】:

    • Syed Suleman 感谢您的回复,我根据您的评论更改了代码,但它仍然下载图像而不是查看图像。
    【解决方案3】:

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

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

    【讨论】:

    • Disha,我尝试了您的代码,但它显示此错误:指定的密钥不存在。images/testingimg10.png
    • 好吧,您需要在 $client 数组中添加您的凭据
    • 是的,我尝试添加这些凭据;首先我尝试: $client = \Aws\S3\S3Client::factory(array( 'key' => 'XXXXXXXXXX', 'secret' => ''XXXXXXXXXXX' //'region' => 'XXXXXXXX', )) ;然后它显示此错误: 致命错误:未找到 Class 'Aws\S3\S3Client' 然后我厌倦了这个 $client = new S3('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXX');那么这会来指定的key不存在。images/testingimg10.png
    • 凭据必须错误才会抛出“密钥不存在”错误
    • 哈哈哈。你能给我的帖子投票吗,然后它将帮助其他面临同样问题的人。谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2018-01-22
    • 2014-06-04
    • 2011-03-01
    相关资源
    最近更新 更多