deploy.js 1.4 KB

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