• Stars
    star
    1,800
  • Rank 25,807 (Top 0.6 %)
  • Language
    PHP
  • Created almost 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The WordPress Plugin to Store Uploads on Amazon S3
S3 Uploads
Lightweight "drop-in" for storing WordPress uploads on Amazon S3 instead of the local filesystem.
Psalm coverage Build status Coverage via codecov.io
A Human Made project. Maintained by @joehoyle.

S3 Uploads is a WordPress plugin to store uploads on S3. S3 Uploads aims to be a lightweight "drop-in" for storing uploads on Amazon S3 instead of the local filesystem.

It's focused on providing a highly robust S3 interface with no "bells and whistles", WP-Admin UI or much otherwise. It comes with some helpful WP-CLI commands for generating IAM users, listing files on S3 and Migrating your existing library to S3.

Requirements

  • PHP >= 7.1
  • WordPress >= 5.3

Getting Set Up

Install Using Composer

composer require humanmade/s3-uploads

Note: Composer's autoloader must be loaded before S3 Uploads is loaded. We recommend loading it in your wp-config.php before wp-settings.php is loaded as shown below.

require_once __DIR__ . '/vendor/autoload.php';

Install Manually

If you do not use Composer to manage plugins or other dependencies, you can install the plugin manually. Download the manual-install.zip file from the Releases page and extract the ZIP file to your plugins directory.

You can also git clone this repository, and run composer install in the plugin folder to pull in its dependencies.


Once you've installed the plugin, add the following constants to your wp-config.php:

define( 'S3_UPLOADS_BUCKET', 'my-bucket' );
define( 'S3_UPLOADS_REGION', '' ); // the s3 bucket region (excluding the rest of the URL)

// You can set key and secret directly:
define( 'S3_UPLOADS_KEY', '' );
define( 'S3_UPLOADS_SECRET', '' );

// Or if using IAM instance profiles, you can use the instance's credentials:
define( 'S3_UPLOADS_USE_INSTANCE_PROFILE', true );

Please refer to this region list http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region for the S3_UPLOADS_REGION values.

Use of path prefix after the bucket name is allowed and is optional. For example, if you want to upload all files to 'my-folder' inside a bucket called 'my-bucket', you can use:

define( 'S3_UPLOADS_BUCKET', 'my-bucket/my-folder' );

You must then enable the plugin. To do this via WP-CLI use command:

wp plugin activate S3-Uploads

The plugin name must match the directory you have cloned S3 Uploads into; If you're using Composer, use

wp plugin activate s3-uploads

The next thing that you should do is to verify your setup. You can do this using the verify command like so:

wp s3-uploads verify

You will need to create your IAM user yourself, or attach the necessary permissions to an existing user, you can output the policy via wp s3-uploads generate-iam-policy

Listing files on S3

S3-Uploads comes with a WP-CLI command for listing files in the S3 bucket for debugging etc.

wp s3-uploads ls [<path>]

Uploading files to S3

If you have an existing media library with attachment files, use the below command to copy them all to S3 from local disk.

wp s3-uploads upload-directory <from> <to> [--verbose]

For example, to migrate your whole uploads directory to S3, you'd run:

wp s3-uploads upload-directory /path/to/uploads/ uploads

There is also an all purpose cp command for arbitrary copying to and from S3.

wp s3-uploads cp <from> <to>

Note: as either <from> or <to> can be S3 or local locations, you must specify the full S3 location via s3://mybucket/mydirectory for example cp ./test.txt s3://mybucket/test.txt.

Private Uploads

WordPress (and therefor S3 Uploads) default behaviour is that all uploaded media files are publicly accessible. In certain cases which may not be desireable. S3 Uploads supports setting S3 Objects to a private ACL and providing temporarily signed URLs for all files that are marked as private.

S3 Uploads does not make assumptions or provide UI for marking attachments as private, instead you should integrate the s3_uploads_is_attachment_private WordPress filter to control the behaviour. For example, to mark all attachments as private:

add_filter( 's3_uploads_is_attachment_private', '__return_true' );

Private uploads can be transitioned to public by calling S3_Uploads::set_attachment_files_acl( $id, 'public-read' ) or vica-versa. For example:

S3_Uploads::get_instance()->set_attachment_files_acl( 15, 'public-read' );

The default expiry for all private file URLs is 6 hours. You can modify this by using the s3_uploads_private_attachment_url_expiry WordPress filter. The value can be any string interpreted by strtotime. For example:

add_filter( 's3_uploads_private_attachment_url_expiry', function ( $expiry ) {
	return '+1 hour';
} );

Cache Control

You can define the default HTTP Cache-Control header for uploaded media using the following constant:

define( 'S3_UPLOADS_HTTP_CACHE_CONTROL', 30 * 24 * 60 * 60 );
	// will expire in 30 days time

You can also configure the Expires header using the S3_UPLOADS_HTTP_EXPIRES constant For instance if you wanted to set an asset to effectively not expire, you could set the Expires header way off in the future. For example:

define( 'S3_UPLOADS_HTTP_EXPIRES', gmdate( 'D, d M Y H:i:s', time() + (10 * 365 * 24 * 60 * 60) ) .' GMT' );
	// will expire in 10 years time

Default Behaviour

As S3 Uploads is a plug and play plugin, activating it will start rewriting image URLs to S3, and also put new uploads on S3. Sometimes this isn't required behaviour as a site owner may want to upload a large amount of media to S3 using the wp-cli commands before enabling S3 Uploads to direct all uploads requests to S3. In this case one can define the S3_UPLOADS_AUTOENABLE to false. For example, place the following in your wp-config.php:

define( 'S3_UPLOADS_AUTOENABLE', false );

To then enable S3 Uploads rewriting, use the wp-cli command: wp s3-uploads enable / wp s3-uploads disable to toggle the behaviour.

URL Rewrites

By default, S3 Uploads will use the canonical S3 URIs for referencing the uploads, i.e. [bucket name].s3.amazonaws.com/uploads/[file path]. If you want to use another URL to serve the images from (for instance, if you wish to use S3 as an origin for CloudFlare), you should define S3_UPLOADS_BUCKET_URL in your wp-config.php:

// Define the base bucket URL (without trailing slash)
define( 'S3_UPLOADS_BUCKET_URL', 'https://your.origin.url.example/path' );

S3 Uploads' URL rewriting feature can be disabled if the current website does not require it, nginx proxy to s3 etc. In this case the plugin will only upload files to the S3 bucket.

// disable URL rewriting alltogether
define( 'S3_UPLOADS_DISABLE_REPLACE_UPLOAD_URL', true );

S3 Object Permissions

The object permission of files uploaded to S3 by this plugin can be controlled by setting the S3_UPLOADS_OBJECT_ACL constant. The default setting if not specified is public-read to allow objects to be read by anyone. If you don't want the uploads to be publicly readable then you can define S3_UPLOADS_OBJECT_ACL as one of private or authenticated-read in you wp-config file:

// Set the S3 object permission to private
define('S3_UPLOADS_OBJECT_ACL', 'private');

For more information on S3 permissions please see the Amazon S3 permissions documentation.

Custom Endpoints

Depending on your requirements you may wish to use an alternative S3 compatible object storage system such as Minio, Ceph, Digital Ocean Spaces, Scaleway and others.

You can configure the endpoint by adding the following code to a file in the wp-content/mu-plugins/ directory, for example wp-content/mu-plugins/s3-endpoint.php:

<?php
// Filter S3 Uploads params.
add_filter( 's3_uploads_s3_client_params', function ( $params ) {
	$params['endpoint'] = 'https://your.endpoint.com';
	$params['use_path_style_endpoint'] = true;
	$params['debug'] = false; // Set to true if uploads are failing.
	return $params;
} );

Temporary Session Tokens

If your S3 access is configured to require a temporary session token in addition to the access key and secret, you should configure the credentials using the following code:

// Filter S3 Uploads params.
add_filter( 's3_uploads_s3_client_params', function ( $params ) {
	$params['credentials']['token'] = 'your session token here';
	return $params;
} );

Offline Development

While it's possible to use S3 Uploads for local development (this is actually a nice way to not have to sync all uploads from production to development), if you want to develop offline you have a couple of options.

  1. Just disable the S3 Uploads plugin in your development environment.
  2. Define the S3_UPLOADS_USE_LOCAL constant with the plugin active.

Option 2 will allow you to run the S3 Uploads plugin for production parity purposes, it will essentially mock Amazon S3 with a local stream wrapper and actually store the uploads in your WP Upload Dir /s3/.

Credits

Created by Human Made for high volume and large-scale sites. We run S3 Uploads on sites with millions of monthly page views, and thousands of sites.

Written and maintained by Joe Hoyle. Thanks to all our contributors.

Interested in joining in on the fun? Join us, and become human!

More Repositories

1

Custom-Meta-Boxes

Lets you easily create metaboxes with custom fields that will blow your mind.
PHP
524
star
2

Mercator

WordPress multisite domain mapping for the modern era.
PHP
505
star
3

Cavalcade

A better wp-cron. Horizontally scalable, works perfectly with multisite.
PHP
495
star
4

cf-to-tf

CLI tool for generating Terraform configuration and state for existing CloudFormation resources
JavaScript
410
star
5

WordPress-Importer

In-development rewrite of the WordPress (WXR) Importer
PHP
358
star
6

network-media-library

Network Media Library plugin for WordPress Multisite
PHP
285
star
7

Colors-Of-Image

A PHP Library for getting colors from images DEPRECATED
PHP
265
star
8

tachyon

Faster than light image resizing service that runs on AWS. Super simple to set up, highly available and very performant.
JavaScript
237
star
9

react-wp-scripts

Integrate create-react-app with your WordPress theme/plugin.
JavaScript
235
star
10

Gaussholder

Fast and lightweight image previews, using Gaussian blur
PHP
189
star
11

hm-gutenberg-tools

Useful helpers, components or tools for building things with Gutenberg
JavaScript
186
star
12

page-for-post-type

Allows you to set a page as the base URL for a post type, much like you can set a page for your blog posts.
PHP
185
star
13

aws-ses-wp-mail

An AWS SES wp_mail() drop-in
PHP
182
star
14

feelingrestful-theme

Theme for feelingrestful.com
JavaScript
176
star
15

WPThumb

⚠️ UNMAINTAINED ⚠️ On demand image resizing for WordPress
PHP
174
star
16

hm-rewrite

HM_Rewrite is a wrapper for the WordPress WP Rewrite system.
PHP
160
star
17

Restsplain

WordPress REST API documentation generator
JavaScript
152
star
18

asset-manager-framework

A framework for overriding the WordPress media library with an external asset provider, such as a DAM
PHP
151
star
19

modular-page-builder

Modular page builder for WordPress
JavaScript
149
star
20

coding-standards

Human Made coding standards for modern code
PHP
145
star
21

Salty-WordPress

A flavorful way to manage your entire WordPress stack.
SaltStack
127
star
22

publication-checklist

Run checks and enforce conditions before posts are published. Built and designed for the WordPress block editor.
JavaScript
117
star
23

hm-core

Nuclear reactor
PHP
105
star
24

Falcon

Connect WordPress to your inbox, and party like it's '88
PHP
104
star
25

roles-to-taxonomy

WordPress plugin to store user roles and levels in a taxonomy
PHP
88
star
26

wp-simple-saml

WordPress Simple SAML plugin
PHP
86
star
27

Workflows

Powerful workflows for WordPress
PHP
84
star
28

hm-base

Standard project layout for Human Made Projects.
PHP
82
star
29

repress

Connect your Redux store to the WordPress REST API.
JavaScript
81
star
30

ai-plugin

An AI integration layer for WordPress
PHP
78
star
31

tachyon-plugin

WordPress plugin for Tachyon
PHP
78
star
32

smart-media

Smart Media enhancements for WordPress
PHP
76
star
33

authorship

A modern approach to author attribution in WordPress.
PHP
66
star
34

go-anonymize-mysqldump

Allows you to pipe data from mysqldump or an SQL file and anonymize it.
Go
60
star
35

block-editor-components

Reusable components, hooks and helper functions for the WordPress block editor(s).
JavaScript
59
star
36

aws-rekognition

A lightweight plugin to add keywords to WordPress image uploads via automatic feature detection. Requires S3 Uploads.
PHP
56
star
37

hm-dev

Even a Poet needs tools!
PHP
55
star
38

query-monitor-flamegraph

Flamegraphs for Query Monitor
JavaScript
54
star
39

wp-remote-cli

Manage your WordPress sites using WP Remote and WP-CLI
PHP
50
star
40

wp-stripe

WordPress Plug-in to manage donations made via the Stripe credit card payment solution
PHP
50
star
41

Cavalcade-Runner

Daemon for Cavalcade, a scalable WordPress jobs system.
PHP
49
star
42

repeatable-posts

A WordPress plugin that enables the creation of repeating posts
PHP
48
star
43

altis-cms

CMS Module for Altis
PHP
47
star
44

Backdrop

Backdrop is a simple library that does one thing: allows you to run one-off tasks in the background.
PHP
47
star
45

clean-html

PHP
45
star
46

WordPress-Objects

Some classes for WordPress to have "real" OO objects for WordPress data types
PHP
43
star
47

react-oembed-container

React container for rendering oembed scripts within HTML string content.
JavaScript
43
star
48

Static-Page

Static Page offloading
PHP
40
star
49

react-wp-ssr

Server-side rendering for React-based WordPress plugins and themes.
PHP
38
star
50

hm-handbook-theme

HM Handbook Theme
PHP
37
star
51

orphan-command

WP-CLI command to list and delete orphan WordPress entities and metadata.
PHP
37
star
52

block-editor-ssr

Server Side render your interactive React Gutenberg blocks
JavaScript
36
star
53

wp-flags

Flags: WordPress admin-controlled user-based feature flags
PHP
34
star
54

hm-gtm

Google Tag Manager template tags and settings tool
PHP
32
star
55

trafficator

Traffic generator for local analytics testing
JavaScript
31
star
56

amf-wordpress

Use another WordPress site as source for your media library.
PHP
30
star
57

webpack-helpers

Reusable Webpack configuration components & related helper utilities.
JavaScript
29
star
58

hm-redirects

Fast and scalable redirects plugin for WordPress
PHP
28
star
59

hm-top-posts

WordPress Plugin: Top Posts by Google Analytics
PHP
28
star
60

asset-loader

PHP utilities for WordPress to aid including dynamic Webpack-generated assets in themes or plugins.
PHP
27
star
61

comment-popularity

Allow visitors to vote on comments
PHP
25
star
62

Unpublish

A plugin for unpublishing content.
PHP
25
star
63

hm-pattern-library

Juniper is the web style guide and pattern library for Human Made projects.
HTML
24
star
64

local-cognito

Local AWS Cognito test server
JavaScript
23
star
65

hm-platform

HM Hosting platform required plugins
PHP
23
star
66

altis

Altis Meta Package
22
star
67

hm-content-import

Migration framework for WordPress, attempts to reduce overhead in migrating content from differing data sources
PHP
22
star
68

wp-redis-predis-client

An alternative Redis client for use with WP Redis. Enables TLS connections.
PHP
22
star
69

hm-backup

The core backup engine that powers BackUpWordPress & WP Remote
PHP
21
star
70

plugin-tester

Simple Docker image for running unit tests for WordPress plugins
Dockerfile
21
star
71

aws-analytics

AWS Pinpoint analytics for WordPress
JavaScript
20
star
72

Mercator-GUI

A GUI component for Mercator domain mapping
PHP
19
star
73

altis-core

Core Module for Altis
PHP
19
star
74

rest-api-white-paper

A WordPress REST API White Paper by Human Made
17
star
75

aws-xray

HM Platform AWS X-Ray Integration
PHP
17
star
76

wp-encrypted-uploads

Upload encrypted files to WordPress, and serve them decrypted in real-time after checking user capability.
PHP
17
star
77

gutenberg-starter-kit

A plugin skeleton for creating gutenberg blocks and plugins.
JavaScript
17
star
78

memcache-object-cache

PHP
16
star
79

rest-sessions

Log in and out of WordPress using the REST API.
PHP
16
star
80

wp-api-demo

WP API Demo install
PHP
16
star
81

Mercator-Redirect

Redirect handler for mapped domains
PHP
15
star
82

linter-bot

Automatically run the HM Coding Standards on any repository.
JavaScript
15
star
83

job-agency

Get the workers working!
PHP
15
star
84

hm-messages

A simple error / success messages API for WordPress
PHP
14
star
85

audit-log

Tamper resistant, off-site audit logging for WordPress
PHP
14
star
86

altis-local-server

Local Server module for Altis
PHP
14
star
87

P2-By-Email

For those who like to interact with P2 by email.
PHP
14
star
88

HM-Portfolio

A WordPress Portfolio Framework Plugin, aimed at WordPress developers who don't want to duplicate the Portfolio backend.
PHP
14
star
89

Sideload-on-publish

Automatically sideload images when publishing posts/comments to ensure things don't get broken.
PHP
13
star
90

Notify-Humans

If Then, Then That for your WordPress applications.
PHP
13
star
91

hm-accounts

PHP
13
star
92

HM-Related-Posts

Related Posts (from HM Core) + Meta box for manually overriding dynamic selected posts.
PHP
12
star
93

bulk-lighthouse

Run bulk lighthouse tests with pagespeed insights API.
JavaScript
12
star
94

hmn-handbook

The theme for the old handbook site. No longer used.
PHP
12
star
95

Slackbot

PHP
12
star
96

altis-enhanced-search

Enhanced Search Module for Altis
PHP
12
star
97

WordPress-Menu-Exporter

A simple plugin which enables you to only export the WordPress menus
PHP
12
star
98

MEXP-Resource-Space

WordPress Media Explorer ResourceSpace extension
PHP
11
star
99

experiments

Web Experimentation framework based on Altis Analytics
JavaScript
11
star
100

gutenberg-widgets

A Gutenberg plugin to add blocks for widget areas
JavaScript
11
star