Amazon S3的静态站点上载程序

项目详细描述

S3sup-Amazon S3的静态站点上载程序

如果您主持的话,s3sup可能比其他s3同步解决方案(例如s3sync)更好 S3上的静态站点。主要功能包括:

  • mime类型检测,大多数文件的Content-Type设置正确。
  • 通过维护 已上载文件的内容/元数据校验和。
  • 重要http头的层次结构配置(例如Cache-Control 以及Content-Disposition)在一组文件或单独文件上。

正在将新站点上载到s3的演示:

其他功能:

  • 上传顺序以防止HTML文件引用静态资产 尚未上传。
  • 删除在本地项目目录中不再存在的S3文件。 也可以使用--nodelete命令行选项或 preserve_deleted_files配置文件密钥,如果您希望它们保持不变 在附近。

开始

按照以下步骤使用s3sup将新站点上载到s3。

1)将s3sup.toml文件添加到本地静态站点根目录中

在本地静态站点目录中,为 第一次使用:

s3sup init

然后编辑在./s3sup.toml创建的框架配置文件。为了一个 配置设置指南,请参阅下面的部分。验证 s3sup将为 单个文件,使用:

s3sup inspect <filepath>

2)配置AWS凭据

可以使用any method that the underlying boto3 library supports配置aws凭据。 对于大多数用户来说,这意味着在 ~/.aws/credentials遵循此模板:

[default]
aws_access_key_id=foo
aws_secret_access_key=bar

或者,可以将凭据作为环境变量提供 AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY

3)运行s3sup

检查s3sup认为需要对s3进行哪些更改。一开始,这个 将指示所有文件都是新文件,需要上载:

s3sup status

然后将更改上载到s3:

s3sup push

安装

可以使用pip安装s3sup。请注意s3sup仅支持python 3:

pip3 install s3sup

尚未提供特定于分发的包,请使用pip 安装方法或从下载源 GitHub

要查看可用的命令,请使用--help

运行s3sup
$ s3sup --help
Usage: s3sup [OPTIONS] COMMAND [ARGS]...

  s3sup - Amazon S3 static site uploader

Options:
  --help  Show this message and exit.

Commands:
  init     Create a skeleton s3sup.toml in the current directory.
  inspect  Show calculated metadata for individual files.
  push     Synchronise local static site to S3.
  status   Show S3 changes that will be made on next push.

每个命令还提供一个--help

$ s3sup push --help
Usage: s3sup push [OPTIONS]

  Synchronise local static site to S3.

  Use --dryrun to test behaviour without changes actually being made to S3.
  Or use "s3sup status".

  This command has two other aliases: upload or sync.

Options:
  -v, --verbose          Output more informational messages than normal.
  -p, --projectdir TEXT  Specify local s3sup static site directory (containing
                         s3sup.toml). By default the current directory is
                         used.
  -n, --nodelete         Do not delete any files on S3, add/modify operations
                         only. Alternatively set "preserve_deleted_files" in
                         s3sup.toml.
  -d, --dryrun           Simulate changes to be made. Do not modify files on
                         S3.
  --help                 Show this message and exit.

配置文件指南

s3sup需要根目录中的s3sup.toml配置文件 你的静态站点。必须至少包含[aws]部分:

可选:全局配置部分

这些设置必须放在s3sup.toml文件的顶部,否则 将发生架构验证错误(toml将它们解释为属于 否则上一节):

Configuration key Required Default Type Expected value
^{} Optional ^{} Boolean Setting to ^{} will prevent files being deleted from S3 when they are deleted in the local project. This can be useful if your site has a long cache lifetime and you don't want cached pages to reference stylesheets/JavaScript files that suddenly disappear. This feature only prevents file deletions, it doesn't prevent file contents being overwritten if updated locally. This can also be achieved by supplying ^{} as a command line option.
^{} Optional ^{} String Specify the character encoding of text files within this s3sup project. The charset is appended to ^{} header. This can also be overriden on a ^{} basis.

必需:[aws]

与AWS相关的配置设置:

Configuration key Required Default Type Expected value
^{} Required N/A String AWS region that the S3 bucket is located in. E.g. 'eu-west-1'.
^{} Required N/A String Name of the S3 bucket. E.g. 'mywebsitebucketname'
^{} Optional Bucket root String S3 sub path where the local project should be uploaded to, without a leading slash. E.g. 'staging/'. By default the local project is uploaded to the root of the S3 bucket.

可选:一个或多个[[path_specific]]

可以包括一个或多个[[path_specific]]节。每个 [[path_specific]]节必须包含一个path规范,其中 该节中的指令应适用于。path可以是 项目目录中文件的路径,或匹配的正则表达式 多条路径。

如果多个[[path_specific]]条目匹配同一路径,则指令是 从所有匹配的[[path_specific]]项组合,最后一个匹配 [[path_specific]]配置文件中的指令项 等效的指令键。

除了path,还可以设置以下指令:

Configuration key Required Default Type Expected value
^{} Required N/A String Regular expression matching multiple relative paths, or an individual relative path to a file.
^{} Optional ^{} String S3 access permissions for the matching paths. One of: private, public-read, authenticated-read.
^{} Optional ^{} String S3 storage resiliency class. One of: STANDARD, REDUCED_REDUNDANCY, STANDARD_IA, ONEZONE_IA, INTELLIGENT_TIERING or GLACIER.
^{} Optional None String Instead of serving the file when a client requests this path, instruct S3 to respond with a redirect to the specified URL instead. Note that URLs must be absolute, e.g. 'https://www.example.com/new_home'.
^{} Optional ^{} String Set HTTP header value to control cache lifetime. A short default cache lifetime is set to provide basic origin shielding should an explicit one not be set.
^{} Optional None String Set HTTP header value to control whether the browser should display the file contents or provide a download dialog to the user
^{} Optional Automatic String Override Content-Type HTTP header value. Only override this if absolutely necessary as s3sup MIME type detection normally sets this header correctly.
^{} Optional ^{} String Manually specify the character encoding of text containing files. This is appended to ^{} HTTP header. Usually setting ^{} in the global configuration section is adequate but this directives allows control on a path level.
^{} Optional Automatic String Set Content-Encoding HTTP header value. Only override this if absolutely necessary as s3sup detects encoding automatically. For wide browser support, it is recommended to store content uncompressed in S3 and then use dynamic gzip compression in a CDN layer.

使用s3sup inspect <filename>检查s3sup将设置的属性 基于您的配置设置和默认值。

可选:[mimetype_overrides]

可选,通常不是必需的。提供文件扩展名到的手动映射 mime类型,它将优先于任何自动mime类型检测 s3sup做的。这对于非常现代的mime类型非常有用,例如是的。

[mimetype_overrides]
'.woff3' = 'font/woff3'
'.oml' = 'application/oml'

示例配置文件

以下配置示例:

  • 默认设置缓存生存期为2分钟的所有文件。
  • 在PDF上设置更长的缓存生存期,并设置响应头以触发 浏览器下载而不是显示文件。

示例s3sup.toml

[aws]
region_name = 'eu-west-1'
s3_bucket_name = 'mys3websitebucket'


[[path_specific]]
path = '^.*$'
Cache-Control = 'max-age=120'

[[path_specific]]
path= '^/assets/download/[0-9]+.pdf$'
Content-Disposition = 'attachment'
Cache-Control = 'max-age=360'

最新Python第三方库