首页 > 解决方案 > 使用 ansibles s3_sync 或相关模块设置 s3 对象的 Content-Type 和 Content-Encoding 标头

问题描述

使用 ansible 的s3_sync模块(下面的任务)没有指定 Content-Type 标头,因此浏览器无法解读。我想知道如何使用mime_type选项或其他 ansible 模块设置 Content-Type 标头。

  - name: S3 | sync gzip assets in build directory
    s3_sync:
      aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}"
      aws_secret_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}"
      bucket: "{{ s3_bucket }}"
      file_root: "{{ s3_sync_dir }}"
      mime_map:
        .gz: application/gzip
        .json: application/text
        .png: image/png
        .ico: image/vnd.microsoft.icon
        .svg: image/svg+xml
      file_change_strategy: force
      permission: public-read
      cache_control: "public, max-age=31536000"
      include: "*"
      region: us-east-1

在此处输入图像描述

我想做类似的事情:

  mime_map:
    .js.gz: 
        headers: "Content-Encoding=application/gzip, Content-Type=application/js"
    .css.gz: 
        headers: "Content-Encoding=application/gzip, Content-Type=text/css"
    .json: application/text
    .png: image/png
    .ico: image/vnd.microsoft.icon
    .svg: image/svg+xml

请注意:文件被散列到浏览器缓存中,因此任何解决方案都需要模式匹配,例如:files: **/*.js.gz, headers: "Content-Type=application/javascript"

标签: amazon-s3ansible

解决方案


推荐阅读