{"library":"s3-folder-upload","title":"S3 Folder Upload","description":"s3-folder-upload is a Node.js utility designed for efficiently uploading local static file directories to Amazon S3 buckets. The package is currently stable at version `2.3.5`, with minor releases deployed on an as-needed basis to introduce new features, fix bugs, or update dependencies, rather than adhering to a strict release schedule. It utilizes the official AWS SDK internally, ensuring robust and reliable integration with S3 services. Key differentiators include its flexible credential management, allowing configuration via environment variables, direct parameters, or AWS IAM role credentials, and providing granular control over S3 object properties such as `ACL`, `CacheControl`, and `Expires` headers, which can be applied globally or per file. The module also supports optional CloudFront invalidation, streamlining cache updates after deployments. Additionally, it offers a command-line interface (CLI) for quick and programmatic deployments, making it well-suited for integration into CI/CD pipelines or for local development workflows managing static assets.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install s3-folder-upload"],"cli":{"name":"s3-folder-upload","version":null}},"imports":["import s3FolderUpload from 's3-folder-upload'","const s3FolderUpload = require('s3-folder-upload')"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import s3FolderUpload from 's3-folder-upload';\nimport path from 'path';\nimport fs from 'fs';\n\n// Ensure environment variables are set or provide credentials directly\nconst AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID ?? 'YOUR_AWS_ACCESS_KEY_ID';\nconst AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY ?? 'YOUR_AWS_SECRET_ACCESS_KEY';\nconst AWS_REGION = process.env.AWS_REGION ?? 'us-east-1';\nconst AWS_BUCKET_NAME = process.env.AWS_BUCKET_NAME ?? 'YOUR_S3_BUCKET_NAME';\n\nconst directoryName = path.resolve('./public'); // Example: your static files are in a 'public' folder\n\n// Create a dummy 'public' directory and file for demonstration if it doesn't exist\nif (!fs.existsSync(directoryName)) {\n  fs.mkdirSync(directoryName, { recursive: true });\n  fs.writeFileSync(path.join(directoryName, 'index.html'), '<h1>Hello S3!</h1>');\n  fs.writeFileSync(path.join(directoryName, 'style.css'), 'body { background-color: lightblue; }');\n  console.log(`Created dummy static files in '${directoryName}'.`);\n}\n\nconst credentials = {\n  accessKeyId: AWS_ACCESS_KEY_ID,\n  secretAccessKey: AWS_SECRET_ACCESS_KEY,\n  region: AWS_REGION,\n  bucket: AWS_BUCKET_NAME\n};\n\nconst options = {\n  useFoldersForFileTypes: false, // Upload files directly to the root of the bucket/uploadFolder\n  uploadFolder: 'app-statics',   // Optional: Upload into a subfolder within the S3 bucket\n  ACL: 'public-read',           // Default ACL for uploaded objects\n  CacheControl: 'public, max-age=31536000' // Default Cache-Control\n};\n\n// Optional CloudFront invalidation settings\nconst invalidation = {\n  awsDistributionId: process.env.CLOUDFRONT_DISTRIBUTION_ID ?? 'YOUR_CLOUDFRONT_DISTRIBUTION_ID',\n  awsInvalidationPath: '/*', // Invalidate all paths\n};\n\nasync function uploadStatics() {\n  if (!AWS_BUCKET_NAME || !AWS_ACCESS_KEY_ID || !AWS_SECRET_ACCESS_KEY) {\n    console.error('AWS credentials and bucket name must be provided via environment variables or directly.');\n    process.exit(1);\n  }\n\n  try {\n    console.log(`Starting S3 upload from ${directoryName} to bucket ${credentials.bucket}...`);\n    await s3FolderUpload(directoryName, credentials, options, invalidation);\n    console.log('S3 upload and optional CloudFront invalidation completed successfully!');\n  } catch (error) {\n    console.error('Error during S3 upload:', error);\n    process.exit(1);\n  }\n}\n\nuploadStatics();\n","lang":"javascript","description":"Demonstrates uploading a local directory to S3 with explicit credentials and optional configurations, including CloudFront invalidation. Creates dummy files for easy testing.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}