AWS S3显示内联而非强制下载文件

Cl'*_*Cl' 57 amazon-s3 amazon-web-services

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

有任何方法可以改变从S3提供文件的方式

gre*_*aty 41

您需要更改Content-Type.在S3控制台中,右键单击该对象,然后选择"属性",然后选择"元数据"下的"属性".您也可以通过编程方式执行此操作:http://docs.amazonwebservices.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/change_content_type

  • 我明白了,我之前使用过 $s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) ,但出于某种原因,我记得做了我想通过使用存储桶策略来实现的相同操作 (2认同)
  • @Cl'您还可以[在`putObject()`方法中设置内容类型](http://unDesigned.org.za/2007/10/22/amazon-s3-php-class/documentation#putObject) (如`putObjectFile()` 现在已成为遗产)。 (2认同)

Neo*_*Neo 36

$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
    ));
Run Code Online (Sandbox Code Playgroud)

  • @insivika 尝试在隐身浏览器中打开。我遇到了同样的问题,当我以匿名模式打开时它对我有用。 (4认同)
  • 感谢'ContentType'=&gt;'image / jpeg',// &lt;-这就是您所需要的! (3认同)
  • 'ContentType' =&gt;'image/jpeg',仍然会下载 url 而不是在浏览器中显示 (3认同)

Ant*_*ley 13

如果您使用的是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
    }
)
Run Code Online (Sandbox Code Playgroud)

您也可以在 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>/
Run Code Online (Sandbox Code Playgroud)

或者,如果您想一次修改一个对象,您可能需要使用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
Run Code Online (Sandbox Code Playgroud)


Ali*_*van 9

您还需要为内联而不是附件指定内容处置。

$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
));
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用。对于那些希望使用任何类型的文件使用此代码的人,请务必使用 'ContentType' =&gt; mime_content_type($file) (3认同)

Sid*_*hra 6

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

在此输入图像描述

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

在此输入图像描述