npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

publish-to-s3

v1.1.0

Published

Upload your dist folder to S3 with a single command

Downloads

3,508

Readme

☁️publish-to-s3

English | 简体中文

Effortlessly deploy your static site to any S3 bucket with a single script.

Best Practices

This script can be easily integrated with services/applications like GitHub Action, Jenkins, Vercel, Cloudflare Pages, etc., to achieve S3-based static website hosting. You only need to call this script after the build process. Although many of these services already offer site hosting, their networks do not cover all regions globally. Therefore, when optimizing your static site, you might find it more suitable to use your own CDN.

This script is suitable for static sites that do not use SSR (Server-Side Rendering). These sites can be independently built on the frontend, and the build output usually contains only static resources like .html, .css, .js, images, etc. Common static blog generators like Hexo, Astro, Jekyll, and static documentation generators like VitePress, Docusaurus can all be perfectly supported. Non-Node.js static sites can also use this script. The main function of the script is to upload these static resources to S3.

The script uses AWS SDK and supports all standard S3 protocols.

Features

  • Overwrite upload: This script does not contain any deletion logic; it simply uploads all local files to S3.
  • Multi-threaded, concurrent uploads to improve upload efficiency.
  • Retry on failure: Triggers retransmission of failed file uploads, with a maximum of 5 retries.
  • Multiple environment variable configuration methods, supporting both .env files and real environment variables.
  • Run directly via npm scripts without modifying the program code, supporting non-Node.js projects.

Local Usage

Step 1: Configure Environment Variables

This script is configured via environment variables, supporting both .env files and real environment variables. See example .env.example.

Note: If your source code is public, make sure to ignore the .env file in your .gitignore. Otherwise, everyone will be able to see your private information.

List of Environment Variables

# S3 configuration. Different cloud providers have different requirements, please refer to the respective documentation.
S3_REGION=oss-cn-shanghai
S3_ACCESS_KEY_ID=YourAccessKey
S3_SECRET_ACCESS_KEY=YourSecretAccessKey
S3_ENDPOINT=https://endpoint.example.com  
S3_BUCKET_NAME=target-bucket
# Specify the build output directory of your static site, usually 'dist' doesn't need modification.
LOCAL_FOLDER_PATH=./dist
# Specify the directory in S3 where the build output should be uploaded. Defaults to the root directory.
S3_DESTINATION_PATH=

Step 2: Manually Run the Script

Ensure you have Node.js and npm installed, then run the following in the directory where you need to upload the ./dist directory:

# Using npm
npx publish-to-s3@latest
# Or using pnpm
pnpx publish-to-s3@latest

npm will automatically download and run this script. Observe the console output:

◐ Preparing to upload files to the S3 bucket "magma-ink-production"                   1:37:32 AM
ℹ All files in "C:\Projects\MagmaInk\dist" will be uploaded to "."                     1:37:32 AM
ℹ Found 626 files to upload                                                            1:37:33 AM
✔ Uploaded archive\tag\ffmpeg\index.html to magma-ink-production in 1219ms            1:37:34 AM
✔ Uploaded archive\tag\cloudreve\index.html to magma-ink-production in 1217ms         1:37:34 AM
✔ Uploaded archive\tag\windows\index.html to magma-ink-production in 1217ms           1:37:34 AM
✔ Uploaded archive\tag\动画\index.html to magma-ink-production in 1213ms              1:37:34 AM
✔ Uploaded archive\category\DEBUG\index.html to magma-ink-production in 1211ms        1:37:34 AM
✔ Uploaded archive\tag\mysql\index.html to magma-ink-production in 1218ms             1:37:34 AM
✔ Uploaded archive\tag\http\index.html to magma-ink-production in 1212ms              1:37:34 AM
✔ Uploaded archive\tag\2020\index.html to magma-ink-production in 1237ms              1:37:34 AM
✔ Uploaded archive\tag\wordpress\index.html to magma-ink-production in 1236ms         1:37:34 AM
...
✔ Uploading completed in 11646ms.

If everything is correct, you will see your static site in S3.

Usage with Continuous Deployment Services

You can use GitHub Action, Jenkins, Vercel, Cloudflare Pages, and other continuous deployment services/automation applications to easily achieve S3-based static website hosting. When your Git is pushed, these services will automatically pull your source code and execute the commands you set.

You should configure environment variables for the project, usually available in the build settings interface.

# You can configure a build command like this:
pnpm run build && pnpx publish-to-s3@latest