S3

如何將您可發佈的 Electron 應用程式產出物發佈到 Amazon S3

S3 目標會將您的 Make 產出物發佈到 Amazon S3 儲存貯體。

安裝

npm install --save-dev @electron-forge/publisher-s3

使用方式

要使用 @electron-forge/publisher-s3,請將它新增至您 Forge 設定中的 publishers 陣列

forge.config.js
module.exports = {
  // ...
  publishers: [
    {
      name: '@electron-forge/publisher-s3',
      config: {
        bucket: 'my-bucket',
        public: true
      }
    }
  ]
};

設定選項記錄在 PublisherS3Config

驗證

建議遵循 Amazon AWS 指南,並設定共用認證指南或適當的環境變數。但是,如果無法這麼做,發佈者設定允許設定 accessKeyIdsecretAccessKey 設定選項。

金鑰管理

根據預設,S3 發佈者會將其物件上傳至 {prefix}/{platform}/{arch}/{name} 金鑰,其中

  • {prefix}config.folder 選項的值(預設為您 package.json 中的 "name" 欄位)。

  • {platform} 是您要發佈的產出物的目標平台。

  • {arch} 是您要發佈的產出物的目標架構。

  • {name} 是您要發佈的產出物的檔案名稱。

如果您在同一個版本(例如同時發佈 ia32x64 Windows 產出物)的相同平台上多次執行發佈命令,您的上傳可能會在 S3 儲存貯體中被覆寫。

若要避免此問題,您可以使用 keyResolver 選項以程式方式產生 S3 金鑰。

forge.config.js
{
  name: '@electron-forge/publisher-s3',
  config: {
    // ...
    keyResolver: (filename, platform, arch) => {
      return `some-prefix/${platform}/${arch}/${filename}`
    }
    // ...
  }
}

從 S3 自動更新

您可以設定 Electron 的內建 autoUpdater 模組來使用 S3 發佈者發佈的產出物。這是一個分為兩個步驟的過程

首先,您必須設定 @electron-forge/publisher-s3 以將您的檔案發佈到與自動更新程式相容的版面配置,並使用 @electron-forge/maker-zip + @electron-forge/maker-squirrel 來建置您的應用程式。

forge.config.js
module.exports = {
  // ...
  makers: [
    {
      name: '@electron-forge/maker-zip',
      config: (arch) => ({
        // Note that we must provide this S3 URL here
        // in order to support smooth version transitions
        // especially when using a CDN to front your updates
        macUpdateManifestBaseUrl: `https://my-bucket.s3.amazonaws.com/my-app-updates/darwin/${arch}`
      })
    },
    {
      name: '@electron-forge/maker-squirrel',
      config: (arch) => ({
        // Note that we must provide this S3 URL here
        // in order to generate delta updates
        remoteReleases: `https://my-bucket.s3.amazonaws.com/my-app-updates/win32/${arch}`
      })
    }
  ],
  publishers: [
    {
      name: '@electron-forge/publisher-s3',
      config: {
        bucket: 'my-bucket',
        public: true
      }
    }
  ]
};

正確設定 Forge 後,第二個步驟是在您的應用程式主程序內設定 autoUpdater 模組。最簡單的形式如下所示,但您可能想要掛接其他事件,以向您的使用者顯示 UI,或詢問他們是否要立即更新您的應用程式。

main.js
const { updateElectronApp, UpdateSourceType } = require('update-electron-app');

updateElectronApp({
  updateSource: {
    type: UpdateSourceType.StaticStorage,
    baseUrl: `https://my-bucket.s3.amazonaws.com/my-app-updates/${process.platform}/${process.arch}`
  }
});

上次更新時間