deploy.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const AWS = require('aws-sdk');
  2. const fs = require("fs");
  3. const uuid = require('uuid');
  4. // 配置 AWS 访问凭证和 S3 区域
  5. AWS.config.update({
  6. accessKeyId: 'AKIA2TBT2JUNG6X3W737',
  7. secretAccessKey: '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',
  8. region: 'us-east-1',
  9. });
  10. const s3 = new AWS.S3();
  11. const bucketName = 'justlistenmusic4560.com';
  12. const filePath = 'bundle.js';
  13. const destinationPath = `${uuid.v4()}.js`;
  14. const fileContent = fs.readFileSync(filePath);
  15. const params = {
  16. Bucket: bucketName,
  17. Key: destinationPath,
  18. Body: fileContent,
  19. };
  20. s3.upload(params)
  21. .promise()
  22. .then(data => {
  23. console.log('File uploaded successfully. Location:', data);
  24. const cloudfront = new AWS.CloudFront();
  25. const params = {
  26. DistributionId: 'E1OARMP9V91HD1',
  27. InvalidationBatch: {
  28. CallerReference: Date.now().toString(),
  29. Paths: {
  30. Quantity: 1,
  31. Items: ['/*'],
  32. },
  33. },
  34. };
  35. cloudfront.createInvalidation(params)
  36. .promise()
  37. .then(data => {
  38. console.log('CloudFront cache has been successfully refreshed.', data);
  39. })
  40. .catch(err => {
  41. console.error('Error refreshing CloudFront cache:', err);
  42. });
  43. })
  44. .catch(err => {
  45. console.error('Error uploading file:', err);
  46. });