出于某种原因,我的 S3 存储桶中的文件被强制作为下载而不是内联显示,因此如果我复制图像链接并将其粘贴到地址栏中然后导航到它,它将促使我的浏览器下载它。相反,我实际上必须单击打开的图像才能转到 url。
更改从 S3 提供文件的方式的任何方法
出于某种原因,我的 S3 存储桶中的文件被强制作为下载而不是内联显示,因此如果我复制图像链接并将其粘贴到地址栏中然后导航到它,它将促使我的浏览器下载它。相反,我实际上必须单击打开的图像才能转到 url。
更改从 S3 提供文件的方式的任何方法
$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
));
您需要更改内容类型。在 S3 控制台中,右键单击对象并选择属性,然后它位于元数据下。您也可以以编程方式进行:http: //docs.amazonwebservices.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/change_content_type
您还需要为 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
));
如果你使用 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
你可以试试这个,这对我有用。
const params = {
Bucket: 'bucket-name',
Body: file,
ACL: 'public-read',
ContentType: contentType,
ContentEncoding: 'base64',
ContentDisposition: 'attachment',
};
你可以试试这个,如果你已经在浏览器上打开了任何 URL,那么 ContentType 是必须在浏览器中打开的,然后在隐身模式下尝试。
var params = {
Bucket: config.BUCKET_NAME,
Key: fileName,
ContentEncoding: 'base64',
ContentDisposition: 'inline',
ContentType: 'image/jpeg',
Body: buf
};
我发现如果我们向它传递一个空值,ContentType
它将打开文件而不是下载。
上传各种扩展名的文件时,这实际上很好。
$s3->putObject(
[
'Bucket' => ..,
'Key' => ..,
'Body' => ..,
'ContentType' => '', <---
'ACL' => 'public-read',
]);
我今天遇到了同样的问题。问题出在Content-type
属性上,当我将文件保存在 s3 中时。例如,我正在使用 Spring Boot,s3 中的内容数据保存 application/octet-stream
在 value 属性中image/jpeg
,而不是 . 因此,当我打开文件链接时,浏览器下载了文件。
这个问题的解决方案是Content-type
在s3中保存文件之前更改!
使用这两个属性强制浏览器从 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 类型
如果您以隐身方式打开它,它会起作用,但由于某种原因它不会起作用。
如果使用 Java SDK 的人正在为此寻找解决方案,您需要将内容类型设置为 ObjectMetaData。
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType("image/png");
s3client.putObject(new PutObjectRequest
(bucketName, fileName + ".png", inputStream,
objectMetadata).withCannedAcl(CannedAccessControlList.PublicRead));
真正对我有用的是 在保存 S3 文件之前将ContentType
参数设置为。image/png