为什么我的s3 sync命令重新上传所有文件而不是修改过的文件?

时间:2018-04-04 03:04:19

标签: amazon-s3

aws s3 sync --acl public-read ~/Projects/website/dist/ s3://aws-website-XX --profile XX

即使文件存在于S3中,也会重新上传所有文件,本地没有变化。

如果我在网站快速启动S3上压缩和上传,它就可以实现我想要的效果。

但我希望通过CLI提出相同的行为。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我要把这个放在这里;这不是OP问题的100%答案,但对于任性的旅行者来说可能有帮助:

首先,我遇到了这个问题,因为我通过webpack将我的软件包,功能部件和组件分成了自己的捆绑包-AWS开始在每个构建版本上载每个文件,这使得开发速度更慢,成本更高(免费运行一晚上的狂欢编码。

在我的webpack.config.js中,我对部署到我的AWS开发环境(通过调用deploy脚本)的编译器发出钩子;看起来像这样:

plugins: [
  new BundleTracker({path: __dirname, filename: 'webpack-stats.json'}),
  {
    apply: (compiler) => {
      compiler.hooks.afterEmit.tap('AfterEmitPlugin', (compilation) => {
        exec('npm run deploy', (err, stdout, stderr) => {
          if (stdout) process.stdout.write(stdout);
          if (stderr) process.stderr.write(stderr);
        });
      });
    }
  },
  new HtmlWebpackPlugin({
    template: './dist/template.html'
  }),
],

*这是我的变通办法,可以使我的--watch true代码在下面的active-dev脚本中自动部署。

我的脚本块如下:

  "scripts": {
    "active-dev": "webpack --mode production --watch true",
    "build": "webpack --mode production",
    "deploy": "npm run deploy-index-bundle && npm run deploy-dist-no_bundles && npm run deploy-bundles",
    "deploy-bundles":"npm run deploy-package-bundles && npm run deploy-feature-bundles && npm run deploy-component-bundles",
    "deploy-dist-no_bundles": "aws s3 sync dist s3://BUCKETNAME --acl public-read --exclude bundles/*",
    "deploy-index-bundle": "aws s3 sync dist s3://BUCKETNAME --acl public-read --exclude * --include bundles/site-index.*",
    "deploy-package-bundles": "aws s3 sync dist s3://BUCKETNAME --exclude * --include bundles/npm.* --acl public-read --size-only",
    "deploy-feature-bundles": "aws s3 sync dist s3://BUCKETNAME --exclude * --include bundles/features.* --acl public-read --size-only",
    "deploy-component-bundles": "aws s3 sync dist s3://BUCKETNAME --exclude * --include bundles/components.* --acl public-read --size-only",
    "deploy-dev": "webpack --mode development && npm run deploy",
    "deploy-prod": "webpack --mode production && npm run deploy"
  },

使用我的deploy脚本,我叫它下面的四个;他们基本上每个都处理自己的捆绑软件,deploy-dist-no_bundles负责处理根目录/静态文件

因为我对上传的内容非常清楚deploy-index-bundle用于上传由我在Webpack中的入口点创建的站点索引文件。 main.js是默认值,因此,如果您的站点由于无法加载而失败,则可以更新入口点或deploy-index-bundle脚本。

上面的

@John Rotenstein 在评论中给出了我需要的带有--size-only标志的最后一部分(因为webpack正在版本化)。

现在,当我deploy时,我仅发送已更改的内容,并规避了导致问题的Webpack版本控制,但如果软件包确实发生更改,则仍会根据大小进行同步。


完全无关的webpack配置,适合厌倦了谷歌搜索的任何人

我的optimization块使用正则表达式将node_modulescomponentsfeatures文件夹中的所有文件分开

optimization: {
  moduleIds: 'hashed',
  runtimeChunk: 'single',
  splitChunks: {
    chunks: 'all',
    maxInitialRequests: Infinity,
    minSize: 0,
    cacheGroups: {
      vendor: {
        test: /[\\/]node_modules[\\/]/,
        name(module) {
          const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
          return `npm.${packageName.replace('@', '')}`;
        },
      },
      components: {
        test: /[\\/]src[\\/]components[\\/]/,
        name(module) {
          const componentName = module.context.match(/[\\/]src[\\/]components[\\/](.*?)([\\/]|$)/)[1];
          return `components.${componentName}`;
        },
      },
      features: {
        test: /[\\/]src[\\/]features[\\/]/,
        name(module) {
          const featuresName = module.context.match(/[\\/]src[\\/]features[\\/](.*?)([\\/]|$)/)[1];
          return `features.${featuresName}`;
        },
      }
    },
  },
},

html-webpack-plugin动态地将所有脚本导入标签添加到我的template.html中(我将模板用于静态代码第三方css和bootstrap导入),从而生成index.html

我的输入框指向创建的index.html

entry: {
  'site-index':'./src/index.jsx',
},

输出块的作用更多。我使用[contenthash]对主版本进行版本控制,这导致每次构建后都会为用户刷新我的网站,但是我故意不使用chunkFilename中的哈希值来解决上述关于re -提交相同/但版本不同的代码。

output: {
    // filename: 'main.js',
    filename: 'main.myAPP.[contenthash].bundle.js',
    chunkFilename: 'bundles/[name].myAPP.bundle.js',
    publicPath: '/',
    path: path.resolve(__dirname, "./dist"),
},

最后说明: aws s3 sync API,特别是--include / --exclude,需要进行大量工作。我遇到了很多问题,哪里有斜杠,什么时候斜杠。

我强烈建议将aws s3 sync--debug--dryrun一起使用以锁定正确的组合。

  • 注意双斜杠//
  • 该模式也在桶路径上运行,对于我的用例,我只是忽略了这一点(对于include始终为False)
  • 一个包含将覆盖所有排除(这就是为什么我从排除所有内容开始)

答案 1 :(得分:-2)

如果文件超过1000个。您应该使用--page-size选项

aws s3 sync --acl public-read ~/Projects/website/dist/ s3://aws-website-XX --profile XX --page-size 9999999