On an intranet project, I need to track all downloads from S3. All users are logged in to access the files and I need to track who downloads what. AWS S3 has a logging feature where I only miss the name of the user. When accessing a file you can add parameters to the url that are ignored by S3 but are logged (see there).
So I added some code to the getExternalUrl() function to push these parameters to AWS. The custom parameters can be inserted by implementing the s3fs_url_settings hook like this:

function mymodule_s3fs_url_settings_alter(&$url_settings, $uri) {
	$url_settings['custom'] = array(
		'user' => 'my_username'
	);
}

This parameter will be appended to the url as as x-user=my_username. This implementation is very generic and can be used for any custom parameter that needs to be passed to AWS S3 log.

Comments

captone created an issue. See original summary.

captone’s picture

Title: Support additional parameters » Support additional parameters in AWS url
coredumperror’s picture

Sounds like a useful enough feature. I'll go ahead and add it into the next dev build.

Be wary, though: if you're storing the username in the URL like that, page caching will break the logging, as only the first user to load the page will have their username appear in the URL. Every subsequent user will see the exact same URL as the first user does, until the page's cache entry gets reset.

coredumperror’s picture

Status: Active » Needs review
StatusFileSize
new3.97 KB

Alright, here's my take on your feature request. I generalized it even further, to allow any custom GET argument. And since there are now three places in the code adding optional GET args to the S3 URLs, I refactored them into a function.

Note that you now need to include the "x-" in your hook code, since s3fs won't add it for you.

If this works for you, please let me know so I can push it to git and add it to the dev build.

coredumperror’s picture

StatusFileSize
new3.79 KB

Whoops, that patch has the wrong file paths in it. Here's the corrected patch.

captone’s picture

Thanks for reworking the idea and building cache safe code. It works fine.
I simply wonder if it's reasonnable to accept any parameter instead of forcing the 'x-' prefix. AWS S3 REST interface is not defined as accepting any parameter, so it may break in the future.

coredumperror’s picture

Status: Needs review » Fixed

I made it completely generic so that some other enterprising module author could add support for something that s3fs doesn't currently support. There are plenty of other GET args that can be added to S3 URLs which s3fs doesn't use.

  • coredumperror committed 1044784 on 7.x-2.x
    Issue #2566391: Added support for including custom GET arguments in S3...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.