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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4751360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:14:51  来源:igfitidea点击:

amazon s3 - image downloading instead of displaying in browser

phpamazon-s3

提问by drs

This is driving me mad. I am uploading images to S3 using the php SDK. Whenever I browse to the image URL, the browser downloads the image opposed to displaying it.

这让我发疯。我正在使用 php SDK 将图像上传到 S3。每当我浏览到图像 URL 时,浏览器都会下载与显示图像相反的图像。

I think its something to do with content type.

我认为这与内容类型有关。

        // Prepare to upload the file to S3 bucket.
        $s3->create_object($bucket, $file_name, array(
                'contentType' => 'binary/octet-stream',
                'acl' => AmazonS3::ACL_PUBLIC
        ));

Can you help?

你能帮我吗?

thanks

谢谢

采纳答案by drs

            $s3->create_object($bucket, $file_name, array(
                    'fileUpload' => $resized_image,
                    'contentType' => $_FILES['image']['type'],
                    'acl' => AmazonS3::ACL_PUBLIC
            ));

回答by jschorr

Your content type is wrong indeed. It needs to be image/jpeg for JPGs, for instance. See this site for a list: http://en.wikipedia.org/wiki/Internet_media_type

您的内容类型确实是错误的。例如,对于 JPG,它需要是 image/jpeg。请参阅此站点以获取列表:http: //en.wikipedia.org/wiki/Internet_media_type

回答by MrEyes

Working on your own, seemingly valid, assumption that it is the content type:

自己工作,看似有效,假设它是内容类型:

You need to set the correct content type for the image being upload, the following list contains all the most common types

您需要为正在上传的图像设置正确的内容类型,以下列表包含所有最常见的类型

* image/gif: GIF image
* image/jpeg: JPEG JFIF image
* image/png: Portable Network Graphics
* image/svg+xml: SVG vector image
* image/tiff: Tag Image File Format
* image/vnd.microsoft.icon: ICO image

So a rework of your sample code for a png upload:

因此,重新编写用于 png 上传的示例代码:

// Prepare to upload the file to S3 bucket.
$s3->create_object($bucket, $file_name, array(
            'contentType' => 'image/png',
            'acl' => AmazonS3::ACL_PUBLIC
));

回答by hakiko

If you are working URL images use

如果您正在使用 URL 图片

'ContentType'  => mime_content_type($absolutePathToImage),

instead of

代替

$_FILES['image']['type']