craftcms / aws-s3

Amazon S3 volume type for Craft CMS.
https://plugins.craftcms.com/aws-s3
MIT License
61 stars 28 forks source link

Rekognition returns a 400 if using an environment variable for Subfolder #107

Open mattbloomfield opened 3 years ago

mattbloomfield commented 3 years ago

Description

When you setup your Asset Volume in settings, if you are using an environment variable for the Subfolder field Rekognition will fail. If you replace the variable with the exact text it represents it will succeed.

Error:

error: "Error executing "DetectFaces" on "https://rekognition.us-east-1.amazonaws.com";

AWS HTTP error: Client error: POST https://rekognition.us-east-1.amazonaws.com resulted in a 400 Bad Request response:

{"__type":"InvalidS3ObjectException","Code":"InvalidS3ObjectException","Message":"Unable to get object metadata from S3. (truncated...)

InvalidS3ObjectException (client): Unable to get object metadata from S3. Check object key, region and/or access permissions. - {"__type":"InvalidS3ObjectException","Code":"InvalidS3ObjectException","Message":"Unable to get object metadata from S3. Check object key, region and/or access permissions."}"

Steps to reproduce

  1. Create a new Asset Volume under Settings > Assets > New Volume
  2. Give it a name and handle
  3. Give it a public URL
  4. Choose Amazon S3 for volume type
  5. I use Environment Variables for all the S3 specific values (using Manual). These do not throw any errors. This includes, the Key ID, Secret, Bucket, and Region
  6. For Subfolder, use $S3_SUBFOLDER_IMG. Ensure you have variables in your .env file. Mine are: S3_ACCESS_KEY_ID="OMITTED" S3_SECRET_ACCESS_KEY="OMITTED" S3_BUCKET="uschamber.com-assets-local" S3_REGION="us-east-1" S3_BASE_URL="https://s3.us-east-1.amazonaws.com/uschamber.com-assets-local/" S3_SUBFOLDER_IMG="mbloomfield/assets/images"
  7. Keep Toggle for adding the subfolder to the Base URL to true
  8. Make uploads Public also true
  9. Enable auto-set focal point.
  10. No other fields are set
  11. Attempt to upload an image to the volume (via Assets or through an entry with an asset field).
  12. Note that it does not work (error received above)
  13. Go back to settings, update Subfolder from $S3_SUBFOLDER_IMG to 'mbloomfield/assets/images'
  14. Attempt another upload.
  15. Note that it works

Video depicting issue

https://www.loom.com/share/d443b6f7b49342f3a89b0c70ad2f77fe

Additional info

Amazon S3 | 1.2.11 Bulk Edit | 2.0.5 CP Field Inspect | 1.2.3 Default Dashboard | 1.0.8 Do It Yourself widget | 2.0.2 Embedded Assets | 2.4.3 Environment Label | 3.2.0 Expanded Singles | 1.1.3 Field Manager | 2.2.1 Freeform | 3.9.2 Mailgun | 1.4.3 Mix | 1.5.2 Navigation | 1.4.2 Neo | 2.8.14 oEmbed | 1.3.6 Read Time | 1.6.0 Redactor | 2.8.3 Redactor Custom Styles | 3.0.4 Redirect Manager | 1.1.1 SEOMate | 1.1.7 Super Table | 2.6.3 Tags | 1.0.7 Wordsmith | 3.3.0.1

mattbloomfield commented 3 years ago

IF you are also facing this issue, putting an alias in my general.php that references the environment variable solved it for me:

// config/general.php

'local' => [
        ...,
        'aliases' => [
            '@s3SubfolderImg' => getenv('S3_SUBFOLDER_IMG'),
        ],
    ],

Then just reference that alias instead of the environment variable.