使用具有适当内容类型的 AWS CLI 复制到 S3

Tsc*_*eck 6 content-type amazon-s3 amazon-web-services aws-cli

我正在将静态 AngularJS 网页部署到 S3 存储桶。我使用 Jenkins,使用 AWS CLI 通过 Shell 复制文件。Groovy 中的具体命令如下所示:

// after build, files are located in frontend/target/myfrontend/
sh 'aws s3 cp frontend/target/myfrontend/ $FrontendAddress --recursive'
Run Code Online (Sandbox Code Playgroud)

我在/resources文件夹中有 SVG 图形。该图像在img标签内未正确显示,因为它的内容类型默认设置为binary/octet-stream。应该是这样image/svg+xml

如何使用aws s3 cp命令复制我的 SVG 文件,并设置正确的内容类型?

Tsc*_*eck 12

更改 *.svg 文件内容类型的命令是:

aws s3 cp --content-type image/svg+xml --acl public-read $FrontendAddress/resources/ $FrontendAddress/resources/ --metadata-directive REPLACE --exclude "*" --include "*.svg" --recursive
Run Code Online (Sandbox Code Playgroud)

  • 官方文档如下:“如果使用 REPLACE,则复制的对象将仅具有 CLI 命令指定的元数据值。请注意,如果您使用以下任何参数:--content-type, content-language 、--content-encoding、--content-disposition、--cache-control 或 --expires,如果您希望复制的对象具有指定的值,则需要为非多部分副本指定 --metadata-directive REPLACE元数据值。” (https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html) (4认同)

小智 7

aws s3 sync _site/ s3://BUCKET --delete --exclude '*.svg'
aws s3 sync _site/ s3://BUCKET --delete --exclude '*' --include '*.svg' --content-type 'image/svg+xml'
Run Code Online (Sandbox Code Playgroud)