silverstripe / silverstripe-s3

Silverstripe module to store assets in S3 rather than on the local filesystem (SS4/SS5 only)
BSD 3-Clause "New" or "Revised" License
20 stars 25 forks source link

Module does not work when running locally via Docker #26

Closed dirtybirdnj closed 5 years ago

dirtybirdnj commented 5 years ago

I've installed this module on a 4.3 site and I'm stuck at the following error. I cannot run dev/build... this is the error I get when I try to do that:

[Emergency] Uncaught Aws\Exception\CredentialsException: Error retrieving credentials from the instance profile metadata server. (cURL error 28: Connection timed out after 1001 milliseconds (see http://curl.haxx.se/libcurl/c/libcurl-errors.html))

After some googling it seems that the module is behaving as-if installed on an EC2 instance... the error seems to indicate that it's trying to get information about the EC2 instance it's running on but I'm running it locally via docker.

Things I have tried:

  1. Adding these fields to my .env file

    AWS_REGION = "us-east-1"
    AWS_BUCKET_NAME = "bucket-name"
    AWS_ACCESS_KEY_ID="XXXXX"
    AWS_SECRET_ACCESS_KEY="YYYYY"
  2. Creating a ~/.aws/credentials file with the following contents:

    [default]
    AWS_ACCESS_KEY_ID = XXXX
    AWS_SECRET_ACCESS_KEY = YYYY
  3. Adding the ENV values directly to docker-compose.yml

services: db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: web_db MYSQL_USER: devuser MYSQL_PASSWORD: devpass ports:

Nothing I do seems to change how the client is being created... please advise if there's something I'm doing wrong or something missing from the docs (highly unlikely).

obj63mc commented 5 years ago

You will need to update your yaml config to make sure the aws credentials are being used.

In your /app/_config/app.yml or whatever yml config file you are using for your project, add something like the following -

---
Only:
  envvarset: AWS_BUCKET_NAME
After:
  - '#assetsflysystem'
---
SilverStripe\Core\Injector\Injector:
  Aws\S3\S3Client:
    constructor:
      configuration:
        region: '`AWS_REGION`'
        version: latest
        credentials:
          key: '`AWS_ACCESS_KEY_ID`'
          secret: '`AWS_SECRET_ACCESS_KEY`'

By default it is expecting you to be on some type of amazon system but outside if you have the following in your config you will be all set. One thing you can do is also setup a separate yml file that is ignored from git like your .env file that contains this info as well separate from your main apps config (this is what we do locally)

dirtybirdnj commented 5 years ago

This worked! Thank you @obj63mc for the lightning fast response ⚡️

Is there any downside to leaving this .yml file in when deploying? Is it basically "overkill" to have this in prod becasue the module is set up to operate this way by default... and having the .yml file basically is more descriptive and explicit about how it's configured?

obj63mc commented 5 years ago

Since I host on heroku and not amazon such as say on EC2, I am not sure if the environment variables are set for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY by default, if those two variables are not set then you would probably get a connection error when trying to access the bucket.

If those environment variables are set at your hosting provider, you can definitely leave it checked in. The main thing is that when hosting on amazon EC2 by example, as long as you have your servers configured to your S3 buckets then you shouldn't need to specify that information.