S3-Uploads icon indicating copy to clipboard operation
S3-Uploads copied to clipboard

Compatibility with Cloudflare R2

Open iasgharalikhan opened this issue 2 years ago • 1 comments

Hello, Since Cloudflare is S3 compatible, can someone share some directions for implementation of S3-Uploads with Cloudflare R2?

iasgharalikhan avatar May 15 '22 19:05 iasgharalikhan

I haven't looked at the code, but I guess all that is needed is to change the API URLs + credentials? I may investigate this as we will be using CloudFlare for a project soon.

woutersamaey avatar Jun 21 '22 11:06 woutersamaey

Adding some insight into this. So far this plugin is working well with R2. The only trouble I had was that the default ACL set on upload was not accepted by R2. Looking closer in the R2 docs it is clear that setting ACL on upload is not supported. However setting the value to private will make it work.

Following the plugin docs, this is pretty straight-forward:

define("S3_UPLOADS_OBJECT_ACL", "private");

Also remember to set the custom endpoint pointing to R2 instead of AWS and tell the client use path style endpoint. Do this in the s3_uploads_s3_client_params filter according to the plugin docs. I defined the endpoint in a constant.

function tw_s3_uploads_s3_client_params( $params ) {
    $params["endpoint"] = S3_UPLOADS_ENDPOINT;
    $params["use_path_style_endpoint"] = true;
    return $params;
}

add_filter( "s3_uploads_s3_client_params", "tw_s3_uploads_s3_client_params");

Providing a full example of defined constants. For some context check out an example from CF on how to utilize the aws-sdk for PHP:

define("S3_UPLOADS_ENDPOINT", "https://<Cloudflare account ID>.r2.cloudflarestorage.com")
define("S3_UPLOADS_BUCKET", "<R2 bucket Name>")
define("S3_UPLOADS_BUCKET_URL", "<R2 bucket public url or domain>")
define("S3_UPLOADS_REGION", "auto")
define("S3_UPLOADS_KEY", "<R2 access key ID>")
define("S3_UPLOADS_SECRET", "<R2 secret access key>")

tedyw avatar Oct 15 '22 17:10 tedyw

Adding some insight into this. So far this plugin is working well with R2. The only trouble I had was that the default ACL set on upload was not accepted by R2. Looking closer in the R2 docs it is clear that setting ACL on upload is not supported. However setting the value to private will make it work.

Following the plugin docs, this is pretty straight-forward:

define("S3_UPLOADS_OBJECT_ACL", "private");

Also remember to set the custom endpoint pointing to R2 instead of AWS and tell the client use path style endpoint. Do this in the s3_uploads_s3_client_params filter according to the plugin docs. I defined the endpoint in a constant.

function tw_s3_uploads_s3_client_params( $params ) {
    $params["endpoint"] = S3_UPLOADS_ENDPOINT;
    $params["use_path_style_endpoint"] = true;
    return $params;
}

add_filter( "s3_uploads_s3_client_params", "tw_s3_uploads_s3_client_params");

Providing a full example of defined constants. For some context check out an example from CF on how to utilize the aws-sdk for PHP:

define("S3_UPLOADS_ENDPOINT", "https://<Cloudflare account ID>.r2.cloudflarestorage.com")
define("S3_UPLOADS_BUCKET", "<R2 bucket Name>")
define("S3_UPLOADS_BUCKET_URL", "<R2 bucket public url or domain>")
define("S3_UPLOADS_REGION", "auto")
define("S3_UPLOADS_KEY", "<R2 access key ID>")
define("S3_UPLOADS_SECRET", "<R2 secret access key>")

dude you're a legend

mathiznogoud avatar Dec 15 '22 09:12 mathiznogoud

As we don't use Cloudflare R2, we don't plan to add any specific support for this to S3 Uploads per our support policy. We would accept documentation PRs (e.g. to the "Custom Endpoints" section of the readme) so long as it's clear we're not officially supporting Cloudflare.

I'm glad you were able to get this to work in the meantime!

rmccue avatar Apr 17 '23 15:04 rmccue

Adding some insight into this. So far this plugin is working well with R2. The only trouble I had was that the default ACL set on upload was not accepted by R2. Looking closer in the R2 docs it is clear that setting ACL on upload is not supported. However setting the value to private will make it work.

Following the plugin docs, this is pretty straight-forward:

define("S3_UPLOADS_OBJECT_ACL", "private");

Also remember to set the custom endpoint pointing to R2 instead of AWS and tell the client use path style endpoint. Do this in the s3_uploads_s3_client_params filter according to the plugin docs. I defined the endpoint in a constant.

function tw_s3_uploads_s3_client_params( $params ) {
    $params["endpoint"] = S3_UPLOADS_ENDPOINT;
    $params["use_path_style_endpoint"] = true;
    return $params;
}

add_filter( "s3_uploads_s3_client_params", "tw_s3_uploads_s3_client_params");

Providing a full example of defined constants. For some context check out an example from CF on how to utilize the aws-sdk for PHP:

define("S3_UPLOADS_ENDPOINT", "https://<Cloudflare account ID>.r2.cloudflarestorage.com")
define("S3_UPLOADS_BUCKET", "<R2 bucket Name>")
define("S3_UPLOADS_BUCKET_URL", "<R2 bucket public url or domain>")
define("S3_UPLOADS_REGION", "auto")
define("S3_UPLOADS_KEY", "<R2 access key ID>")
define("S3_UPLOADS_SECRET", "<R2 secret access key>")

Thank you very much, it is still working!

fernandodilland avatar Jan 28 '24 02:01 fernandodilland

I'm unclear where this goes.

function tw_s3_uploads_s3_client_params( $params ) {
    $params["endpoint"] = S3_UPLOADS_ENDPOINT;
    $params["use_path_style_endpoint"] = true;
    return $params;
}

add_filter( "s3_uploads_s3_client_params", "tw_s3_uploads_s3_client_params");

I understand the define statments go in wp-config.php, but do the function and filter statements go in here, or in wordpress theme functions.php or somewhere in the plugins/s3-uploads directory.

NKNdevs avatar May 08 '24 08:05 NKNdevs