humanmade / S3-Uploads

The WordPress Plugin to Store Uploads on Amazon S3
1.93k stars 388 forks source link

"location constraint" error on some regions #406

Open uni4online opened 4 years ago

uni4online commented 4 years ago

If you create a bucket in some regions then you get the below error when trying to access the file:

` IllegalLocationConstraintException

The af-south-1 location constraint is incompatible for the region specific endpoint this request was sent to. AA9313B3EA6564D2 GfLxHF3hvL+6o/ZCvkK1boSjj1Y+6m1/T1CqcsBAe4L9kB9m+eipqAiiin60ChdXXjwXR7TIp1Y=

`

Example for a bucket in "af-south-1" the S3 URL created is

https://{bucket}.s3.af-south-1.amazonaws.com/wordpressmedia/uploads/2020/05/test.jpg

but the plugin generates:

https://{bucket}.s3.amazonaws.com/wordpressmedia/uploads/2020/05/test-1024x191.jpg

on line 140 of class-s3-uploads.php the generated URL is: return apply_filters( 's3_uploads_bucket_url', 'https://' . $bucket . '.s3.amazonaws.com' . $path );

perhaps it should be :

` public function get_s3_url() { if ( $this->bucket_url ) { return $this->bucket_url; }

    $bucket = strtok( $this->bucket, '/' );
    $path   = substr( $this->bucket, strlen( $bucket ) );
    $region = $this->region;

    return apply_filters( 's3_uploads_bucket_url', 'https://' . $bucket . '.s3.'. $region .'.amazonaws.com' . $path );
}

`

smoglica commented 4 years ago

I'm experiencing the same issue. This thing should be handled automatically by the plugin. Luckily there is a filter applied, so you can add it in your functions.php in order to make it work.

// functions.php
function namespace_s3_uploads_bucket_url( $url ) {
    $replace = '.s3.';

    return str_replace( $replace, $replace . S3_UPLOADS_REGION . '.', $url );
}
add_filter( 's3_uploads_bucket_url', 'namespace_s3_uploads_bucket_url', 10, 1 );
ianmubangizi commented 3 years ago

have they not fixed this yet?