How to Upload Files directly to Amazon S3 bucket within WordPress admin

This article will show you how to use WordPress Amazon S3 Smart Upload plugin to upload files straight to S3 bucket without using local storage.

  1. Create an S3 bucket & get access key
  2. Configure S3 bucket under our plugin’s settings page
  3. Upload files directly to your S3 bucket

Requirement:

Create an S3 bucket & get access key

First of all, you need to create an Amazon S3 bucket storing your media file uploads.

Secondly, generate an access key that gives our plugin permission to access and upload files to this bucket.

Enable CORS on S3 bucket

Due to upload files directly from local instead of website server, you need to enable Cross-Origin Resource Sharing (CORS) on your S3 bucket.

To do so, go to your S3 bucket and switch to the Permission tab.

Navigate to the bottom of the page, you will see the Cross-origin resource sharing (CORS) option. Simply click on “Edit” button to add the new rules.

Then copy our custom configuration below to the editor page and save it.

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "HEAD",
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [
            "ETag"
        ],
        "MaxAgeSeconds": 3000
    }
]

* refers to all external domains. Alternatively, you can specify a particular domain (http://example.com) instead.

Once finished, go back to your WordPress site and configure your S3 bucket.

Configure S3 bucket

After activating our plugin, navigate to the plugin’s settings page under Media >> Upload to S3 menu from your admin dashboard.

You will see the guide to configuring your S3 access keys in wp-config.php file.

Put the following code at the top of your wp-config.php file and replace your AWS keys accordingly.

define('SSU_PROVIDER', 'aws');
define('SSU_KEY', 'your-access-key');
define('SSU_SECRET', 'your-access-secret');
define('SSU_BUCKET', 'your-bucket');
define('SSU_FOLDER', 'your-bucket-folder'); // upload files to specific folder under the bucket (optional)
define('SSU_REGION', 'your-bucket-region');
define(‘SSU_CAPABILITY’, ‘manage_options’); // determine who can upload files to the bucket (optional)

When done, reload this page and start uploading your files.

Upload files to Amazon S3 bucket

The left sidebar of this page displays an upload button allowing you to select files to upload from your local.

All files uploaded via our plugin will be private by default. That means no one can access these files directly even though you grant public access to your bucket.

Tick “Show it under media library” if you want to add the upload files to your media library.

Once added, these S3 files can be managed like other WordPress media files. For example, you can select and insert them into content through Add Media while editing any page or post. What’s more, removing the file’s copy in the Media Library won’t affect its original version on the S3 bucket.

The right sidebar shows your S3 bucket folder structure. Simply right-click on the filename to copy the file URL as well as presenting it under Media Library.

Since version 1.2.0, once you define (‘SSU_FOLDER’, ‘your-bucket-folder’); in your wp-config.php file, our plugin only displays all files under this folder.

That means you can easily view your file uploads without having to browse through all the bucket directories. Tick the corresponding checkbox to display the entire directories under your bucket as usual.

Lasted updated on November 24, 2020