104

出于某种原因,我的 S3 存储桶中的文件被强制作为下载而不是内联显示,因此如果我复制图像链接并将其粘贴到地址栏中然后导航到它,它将促使我的浏览器下载它。相反,我实际上必须单击打开的图像才能转到 url。

更改从 S3 提供文件的方式的任何方法

4

13 回答 13

68
$client->putObject(array(
        'Bucket'     => 'buckname',
        'Key'        => $destination,
        'SourceFile' => $source,
        'ContentType' =>'image/jpeg', //<-- this is what you need!
        'ACL'          => 'public-read'//<-- this makes it public so people can see it
    ));
于 2014-09-24T04:38:16.640 回答
53

您需要更改内容类型。在 S3 控制台中,右键单击对象并选择属性,然后它位于元数据下。您也可以以编程方式进行:http: //docs.amazonwebservices.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/change_content_type

于 2013-01-04T05:32:36.447 回答
16

您还需要为 inline 而不是 attachment 指定 Content Disposition 。

$client->putObject(array(
    'Bucket'     => 'buckname',
    'Key'        => $destination,
    'SourceFile' => $source,
    'ContentType' =>'image/jpeg', //<-- this is what you need!
    'ContentDisposition' => 'inline; filename=filename.jpg', //<-- and this !
    'ACL'          => 'public-read'//<-- this makes it public so people can see it
));
于 2018-04-05T09:52:07.340 回答
14

如果你使用 python,你可以设置ContentType 如下

s3 = boto3.client('s3')

mimetype = 'image/jpeg' # you can programmatically get mimetype using the `mimetypes` module
s3.upload_file(
    Filename=local_path,
    Bucket=bucket,
    Key=remote_path,
    ExtraArgs={
        "ContentType": mimetype
    }
)

您也可以在 aws cli 中实现相同的目的。注意*.jpeg检查s3文档

aws s3 cp \
      --exclude "*" \
      --include "*.jpeg" \
      --content-type="image/jpeg"  \
      --metadata-directive="REPLACE" \
      --recursive \
      --dryrun \
       s3://<bucket>/<path>/ \
       s3://<bucket>/<path>/

或者,如果您想一次修改一个对象,您可能需要使用s3api copy-object

aws s3api copy-object \
    --content-type="image/jpeg" \
    --metadata-directive="REPLACE" \
    --copy-source "<bucket>/<key>" \
    --bucket "<bucket>" \
    --key "<key>" \
    --acl public-read
于 2019-05-04T20:15:21.757 回答
4

您也可以从 AWS S3 控制台更改内容类型。

在此处输入图像描述

然后你会看到侧面弹出,用它来手动更改它。

在此处输入图像描述

于 2020-05-28T12:08:59.977 回答
4

你可以试试这个,这对我有用。

const params = {
          Bucket: 'bucket-name',
          Body: file,
          ACL: 'public-read',
          ContentType: contentType,
          ContentEncoding: 'base64',
          ContentDisposition: 'attachment',
        };
于 2020-03-13T12:20:45.940 回答
2

你可以试试这个,如果你已经在浏览器上打开了任何 URL,那么 ContentType 是必须在浏览器中打开的,然后在隐身模式下尝试。

var params = {
            Bucket: config.BUCKET_NAME,
            Key: fileName,
            ContentEncoding: 'base64',
            ContentDisposition: 'inline',
            ContentType: 'image/jpeg',
            Body: buf
        };
于 2019-10-17T11:13:58.713 回答
2

我发现如果我们向它传递一个空值,ContentType它将打开文件而不是下载。

上传各种扩展名的文件时,这实际上很好。

$s3->putObject(
[
    'Bucket' => ..,
    'Key' => ..,
    'Body' => ..,
    'ContentType' => '', <---
    'ACL' => 'public-read',
]);
于 2020-09-24T13:56:00.817 回答
1

我今天遇到了同样的问题。问题出在Content-type属性上,当我将文件保存在 s3 中时。例如,我正在使用 Spring Boot,s3 中的内容数据保存 application/octet-stream在 value 属性中image/jpeg,而不是 . 因此,当我打开文件链接时,浏览器下载了文件。

这个问题的解决方案是Content-type在s3中保存文件之前更改!

于 2021-07-29T00:32:27.707 回答
0

使用这两个属性强制浏览器从 s3 内联文件:

“响应内容类型”和“响应内容处置”

您可以在生成签名 URL 时在 getCommand 中使用它们

$cmd = $s3Client->getCommand('GetObject', [
        'Bucket' => $bucket_name,
        'Key' => $filename,
        'ResponseContentType' =>  get_mime_type($filename),
        'ResponseContentDisposition' => 'inline; filename='.$user_filename
    ]);
   

get_mime_type 只是我使用文件名返回 mime 类型的自定义函数

这是我遇到同样问题时使用的,我的 s3 存储桶中有数百万个文件,我无法在控制台中单独更改 mime 类型

于 2021-10-18T19:13:30.870 回答
0

如果您以隐身方式打开它,它会起作用,但由于某种原因它不会起作用。

于 2020-12-28T12:21:56.403 回答
0

如果使用 Java SDK 的人正在为此寻找解决方案,您需要将内容类型设置为 ObjectMetaData。

ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType("image/png");

s3client.putObject(new PutObjectRequest
    (bucketName, fileName + ".png", inputStream, 
     objectMetadata).withCannedAcl(CannedAccessControlList.PublicRead));
于 2020-01-04T05:29:14.633 回答
0

真正对我有用的是 在保存 S3 文件之前将ContentType参数设置为。image/png

于 2022-02-10T22:58:58.347 回答