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

strapi-provider-upload-aws-s3-advanced

v5.0.1

Published

AWS S3 provider for strapi upload with more advanced configuration options

Downloads

3,446

Readme

strapi-provider-upload-aws-s3-advanced

Configuration

This extends the original configurability of the provider by adding both a baseUrl, which may be your CDN URL, which replaces the endpoint returned from AWS with a custom URL, and prefix, which does exactly that: prefixes the object's path such that we do not strictly upload into the buckets root directory. This can be used to keep the bucket organized, or using a singular bucket for multiple services. Other than that you can put e.g. CloudFront Caching in front of the bucket and only expose the CloudFront URL to e.g. save traffic costs that come from direct bucket access.

Everything else follows the regular strapi-provider-upload-aws-s3 schema.

Your configuration is passed down to the provider. You can see the complete list of options here

See the using a provider documentation for information on installing and using a provider. And see the environment variables for setting and using environment variables in your configs.

To upload with ACLs, make sure that the S3 user has abilities "s3:PutObjectACL" in addition to the regular "s3:PutObject" ability. Otherwise S3 will reject the upload with "Access Denied".

If you cannot provide access key and secret, but instead use other (AWS) tools to authenticate to your bucket, omit providerOptions.accessKeyId and providerOptions.secretAccessKey. For more, see https://github.com/zoomoid/strapi-provider-upload-aws-s3-advanced/pull/14.

Example

./config/plugins.js

module.exports = ({ env }) => ({
  // ...
  upload: {
    provider: "aws-s3-advanced",
    providerOptions: {
      accessKeyId: env("AWS_ACCESS_KEY_ID"),
      secretAccessKey: env("AWS_ACCESS_SECRET"),
      region: env("AWS_REGION"),
      params: {
        bucket: env("AWS_BUCKET"), // or "Bucket", @aws-sdk requires capitalized properties, but the convention for this file is lowercased, but the plugin understands both
        acl: env("AWS_BUCKET_ACL"), // or "ACL", see above
      },
      baseUrl: env("CDN_BASE_URL"), // e.g. "https://cdn.example.com", this is stored in strapi's database to point to the file
      prefix: env("BUCKET_PREFIX"), // e.g. "strapi-assets". If BUCKET_PREFIX contains leading or trailing slashes, they are removed internally to construct the URL safely
    },
  },
  // ...
});

If using strapi >= 4.0.0, please use the below config:

./config/plugins.js

module.exports = ({ env }) => ({
  // ...
  upload: {
    config: {
      provider: "strapi-provider-upload-aws-s3-advanced",
      providerOptions: {
        accessKeyId: env("AWS_ACCESS_KEY_ID"),
        secretAccessKey: env("AWS_ACCESS_SECRET"),
        region: env("AWS_REGION"),
        params: {
          bucket: env("AWS_BUCKET"), // or "Bucket", @aws-sdk requires capitalized properties, but the convention for this file is lowercased, but the plugin understands both
          acl: env("AWS_BUCKET_ACL"), // or "ACL", see above
        },
        baseUrl: env("CDN_BASE_URL"), // e.g. "https://cdn.example.com", this is stored in strapi's database to point to the file
        prefix: env("BUCKET_PREFIX"), // e.g. "strapi-assets". If BUCKET_PREFIX contains leading or trailing slashes, they are removed internally to construct the URL safely
      },
    },
  },
  // ...
});

If you need to extend the configuration of the S3 client with additional properties, put them into providerOptions.params. The params object is spread into the S3 configuration at initialization, so it will accept any additional configuration this way.

Note: If you are migrating from a pre-4.0.0 version (i.e. v3.6.8 or earlier), the files relation will include aws-s3-advanced as the provider. Previously, the prefix "strapi-upload-provider" was assumed to always be present for upload provider plugins. This is no longer the case in >= 4.0.0, hence when uploading with the newer version of this provider, strapi will insert new files with the full provider package name, i.e., strapi-provider-upload-aws-s3-advanced. See Migration for details on the required manual work.

Image Previews

To allow the thumbnails to properly populate, add the below config to

./config/middlewares.js

module.exports = ({ env }) => [
  // ...
  {
    name: "strapi::security",
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          "connect-src": ["'self'", "https:"],
          "img-src": ["'self'", "data:", "blob:", `${env("CDN_BASE_URL")}`],
          "media-src": ["'self'", "data:", "blob:", `${env("CDN_BASE_URL")}`],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  // ...
];

Migration

v5.0.0

You don't need to do anything on your end except updating the dependencies.

v5.0.0 rewrites the entire provider in Typescript and introduces Unit Tests, among deprecating NodeJS versions lower than v14. Specifically the last aspect requires a new major version. Other than that, nothing changed in terms of configuration surface.

v4.1.x

To allow for an empty baseUrl (#9), we made some adjustments to the way the configuration is parsed: in your plugins.js, env("CDN_BASE_URL") uses strapi's helper function for parsing ENV variables. If any second argument is omitted, undefined is returned. Thus, if your ENV does not contain any value for CDN_BASE_URL, you are good to go. Undefined baseUrl causes the plugin to prepend the cannonic default endpoint of your storage provider, e.g., https://mystoragebucket.s3.amazonaws.com.

If instead you defined CDN_BASE_URL to be "", the env helper returns that empty string. Previously, this was treated as the same case as using undefined. In some scenarios however you might not want this, e.g., local development. Thus, we now check explicitly for undefinedness instead of the prior truthiness.. If you defined CDN_BASE_URL to be an empty string and relied upon the prepending of the cannonical default endpoint, change your ENV variable either explicitly to the endpoint's URL or make it undefined.

v3.x to v4.0.x

Strapi now uses the full package name as provider name, as seen in the configuration of the provider in the Example section above. This means that the relation will include different provider names when using the newer version of this provider with strapi >= 4.0.0 on data from pre-4.0.0. In particular, you will find that the pre-4.0.0 files will have the provider aws-s3-advanved, while the newer ones will have strapi-provider-aws-s3-advanved. If you're not going to change the existing files in your CDN, you will not need to take any actions. The provider attribute is only used for mapping the handler for creating or deleting files to the handlers defined in this provider. Files will remain readable with the old provider and new files will be added with the new provider name. Only if you want to delete old files from the new provider, you will be required to adapt the files table.

In strapi >= 4.0.0, only SQL databases are officially supported, so we will only provide queries for the supported backends:

PostgreSQL

UPDATE files SET provider = 'strapi-provider-upload-aws-s3-advanced' WHERE provider = 'aws-s3-advanced';

MySQL

UPDATE `files` SET `provider` = `strapi-provider-upload-aws-s3-advanced` WHERE `provider` = `aws-s3-advanced`;

SQLite

UPDATE files SET provider = 'strapi-provider-upload-aws-s3-advanced' WHERE provider = 'aws-s3-advanced';

Resources

Links