azput.js 1.7 KB

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