Amazon s3查看上传的图像会让我下载图像


Amazon s3 viewing the uploaded image will let me download the image

我在Amazon s3上传图像,但当我试图在浏览器中查看文件时,它允许我下载图像,我该如何禁用该图像?我预期的结果应该是在浏览器中看到图像。我该怎么做呢?

我听说过设置标题,但我不熟悉。

有人能教我如何完成这个吗?

谢谢!

您是否使用AWS SDK for PHP?

您需要为您的图像设置正确的Content-Type,否则默认为application/octet-stream,这就是浏览器下载文件的原因。维基百科上有一个很好的价值列表。

// Instantiate the class
$s3 = new AmazonS3();
$response = $s3->create_object('my-bucket', 'myimgae.jpg', array(
    'contentType' => 'image/jpeg'
));
// Success?
var_dump($response->isOK());

对于S3中已有的对象,可以使用change_content_type

最后,如果要生成预签名的url,可以动态设置content-type

// Instantiate the class
$s3 = new AmazonS3();
// Get the URL
$url = $s3->get_object_url('bucket', 'myimage.jpg', '5 minutes', array(
    'response' => array(
        'content-type' => 'image/jpeg'
    )
));