azput.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* eslint-disable camelcase */
  2. const { BlobServiceClient } = require('@azure/storage-blob');
  3. const fs = require('node:fs');
  4. const path = require('node:path');
  5. const blobServiceClient = BlobServiceClient.fromConnectionString(process.env.ELECTRON_ARTIFACTS_BLOB_STORAGE);
  6. const args = require('minimist')(process.argv.slice(2));
  7. let { prefix = '/', key_prefix = '', _: files } = args;
  8. if (prefix && !prefix.endsWith(path.sep)) prefix = path.resolve(prefix) + path.sep;
  9. function filenameToKey (file) {
  10. file = path.resolve(file);
  11. if (file.startsWith(prefix)) file = file.substr(prefix.length - 1);
  12. return key_prefix + (path.sep === '\\' ? file.replace(/\\/g, '/') : file);
  13. }
  14. let anErrorOccurred = false;
  15. function next (done) {
  16. const file = files.shift();
  17. if (!file) return done();
  18. const key = filenameToKey(file);
  19. const [containerName, ...keyPath] = key.split('/');
  20. const blobKey = keyPath.join('/');
  21. console.log(`Uploading '${file}' to container '${containerName}' with key '${blobKey}'...`);
  22. const containerClient = blobServiceClient.getContainerClient(containerName);
  23. const blockBlobClient = containerClient.getBlockBlobClient(blobKey);
  24. blockBlobClient.uploadFile(file)
  25. .then((uploadBlobResponse) => {
  26. console.log(`Upload block blob ${blobKey} successfully: https://artifacts.electronjs.org/${key}`, uploadBlobResponse.requestId);
  27. })
  28. .catch((err) => {
  29. console.error(err);
  30. anErrorOccurred = true;
  31. })
  32. .then(() => next(done));
  33. }
  34. next(() => {
  35. process.exit(anErrorOccurred ? 1 : 0);
  36. });