<p>You can hack some env-var functionality into your S3 sources by sprinkling <code>craft()-&gt;config-&gt;parseEnvironmentString()</code> into <em>S3AssetSourceType.php</em>...</p>

<p>I've historically used this to get per-env <em>paths</em> &mdash; i.e. parsing vars in <code>getBaseUrl()</code>, <code>getPathPrefix()</code>, and <code>getS3Path()</code> &mdash; rather then <em>credentials</em>. (I'm usually okay with storing those credentials in the db, because I almost always create an IAM user/policy just for the CMS.)</p>

<p>However, the concept should be the same. Off the top of my head, I think in order to get it to parse credentials and other settings, you'd have to override <code>getSettings()</code>, along the lines of:</p>

<pre><code>/**
 * @inheritDoc BaseSavableComponentType::getSettings()
 *
 * @return BaseModel
 */
public function getSettings()
{

    if (!isset($this-&gt;_settings))
    {

        $this-&gt;_settings = parent::getSettings();

        foreach ($this-&gt;_settings-&gt;getAttributes() as $name =&gt; $value)
        {
            if (is_string($value))
            {
                $this-&gt;_settings-&gt;setAttribute(
                    $name,
                    craft()-&gt;config-&gt;parseEnvironmentString($value)
                );
            }
        }

    }

    return $this-&gt;_settings;

}
</code></pre>

<p>Either you'll need to remember to restore this hack whenever you update Craft, or if you wanted to be super-fancy you could hook onto the <code>updates.onEndUpdate</code> event to re-write that method into the correct file.</p>

CC BY-SA 3.0
2017-09-29T03:38:10.560Z
2017-09-29T03:38:10.560Z
Michael Rog
<p>Not really.</p>

<p>In Craft 2 <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/assetsourcetypes/S3AssetSourceType.php#L410-L422" rel="nofollow noreferrer">S3 settings</a> ultimately get stored in the database.</p>

<p>Normally the answer would be "you can write a plugin that adds its own Asset Source Type and changes that behavior", but in Craft 2, it is not easy to add custom Asset Source Types, either.</p>

<p>Craft 3 has solved both of those problems.  You can register custom <a href="https://github.com/craftcms/docs/blob/master/en/volume-types.md#registering-custom-volume-types" rel="nofollow noreferrer">Asset Volume Types</a> as well as <a href="https://github.com/craftcms/docs/blob/adf409846292dc7f35f79e0a19dbdc4defd33e20/en/configuration.md#overriding-volume-settings" rel="nofollow noreferrer">override Volume settings</a>.</p>

CC BY-SA 3.0
2017-09-27T18:36:16.170Z
2017-09-27T18:36:16.170Z
Brad Bell
<p>For a Craft 2 S3 asset source, access keys are expected to be entered in from the Control Panel. Is there a way to supply the access key and secret through environment variables instead?</p>

<assets><s3>
How to store S3 credentials in environment variables?
# How to store S3 credentials in environment variables?

(Asked by: username_0 on 2017-09-27)

For a Craft 2 S3 asset source, access keys are expected to be entered in from the Control Panel. Is there a way to supply the access key and secret through environment variables instead?

**Tags:** assets, s3


---

## 2 answers

### Answer by username_1 on 2017-09-29. Score: 1

You can hack some env-var functionality into your S3 sources by sprinkling craft()->config->parseEnvironmentString() into S3AssetSourceType.php...
I've historically used this to get per-env paths — i.e. parsing vars in getBaseUrl(), getPathPrefix(), and getS3Path() — rather then credentials. (I'm usually okay with storing those credentials in the db, because I almost always create an IAM user/policy just for the CMS.)
However, the concept should be the same. Off the top of my head, I think in order to get it to parse credentials and other settings, you'd have to override getSettings(), along the lines of:
/**
 * @inheritDoc BaseSavableComponentType::getSettings()
 *
 * @return BaseModel
 */
public function getSettings()
{

    if (!isset($this->_settings))
    {

        $this->_settings = parent::getSettings();

        foreach ($this->_settings->getAttributes() as $name => $value)
        {
            if (is_string($value))
            {
                $this->_settings->setAttribute(
                    $name,
                    craft()->config->parseEnvironmentString($value)
                );
            }
        }

    }

    return $this->_settings;

}

Either you'll need to remember to restore this hack whenever you update Craft, or if you wanted to be super-fancy you could hook onto the updates.onEndUpdate event to re-write that method into the correct file.


---

### Answer by username_2 on 2017-09-27. Score: 1

Not really.

In Craft 2 [S3 settings](https://github.com/pixelandtonic/Craft-Release/blob/master/app/assetsourcetypes/S3AssetSourceType.php#L410-L422) ultimately get stored in the database.

Normally the answer would be "you can write a plugin that adds its own Asset Source Type and changes that behavior", but in Craft 2, it is not easy to add custom Asset Source Types, either.

Craft 3 has solved both of those problems. You can register custom [Asset Volume Types](https://github.com/craftcms/docs/blob/master/en/volume-types.md#registering-custom-volume-types) as well as [override Volume settings](https://github.com/craftcms/docs/blob/adf409846292dc7f35f79e0a19dbdc4defd33e20/en/configuration.md#overriding-volume-settings).


---

