Sold files are frequently hosted on Amazon S3.
We need to support tracking downloads (download counts), downloading from S3, disallowing download if the counts have been reached.

Right now all this works out of the box with 7.x-2.x.
However, the file download page (file/%file/download) uses file_transfer(), meaning that the file is downloaded from S3 to the server, and then served to the user.
This negates most of S3's benefits.

We should extend our code to redirect to S3's url when appropriate.

Related issues:
#2021997: Honor access limits when serving files through AmazonS3
#1685142: Amazon S3 Download count integration?

CommentFileSizeAuthor
#1 2049481-1-s3-compat.patch1.69 KBbojanz
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bojanz’s picture

Status: Active » Needs review
FileSize
1.69 KB

Notes:
1) The download is regularly logged, since drupal_goto() calls hook_exit().
Note that regular files are logged at the end of the download, while with S3 the logging happens right before the user is redirected to S3.
I don't know if this is a deal breaker for any people, in which case a setting could force file_transfer(), which would make logging work as with regular files, but sacrificing performance. However I don't think anyone would want that behavior.
2) Download count limits are respected. The authenticated S3 url is generated only if the file is under limits.
3) It is your responsibility to configure the AmazonS3 module to serve authenticated urls that force the download.
4) While testing I had cases where calling is_file($file->uri) would stall indefinitely, but that's probably a bug in the AmazonS3 module, not here.

deggertsen’s picture

Could you clarify what this patch does? I'd be happy to test it but even without this patch I have been successfully using Commerce File 2.x with S3 using the patch at #1277152: Integration with Filefield Sources.

I'm guessing that the patch will fix any download count limits? I have not yet been able to test download limits with Commerce File 2.x.

bojanz’s picture

The file download count limits already work and are respected.

This patch makes the file downloaded directly from S3, instead of downloading it to the server first.

deggertsen’s picture

Ahhh, so it's to help with performance. I've applied the patch and everything is working as expected. I will let you know if I run into any issues.

torgosPizza’s picture

Downloading direct from S3 (or, better, our CDN) is our preferred method. I will try to test this as well, since I'd like to make sure that our domain alias settings are respected.

bojanz’s picture

Status: Needs review » Fixed

I'm going away for 10 days, so I've committed this patch, since it's definitely an improvement over the current code.
Reopen this issue with any S3 problems you find while testing.

torgosPizza’s picture

Status: Fixed » Active

It looks like it's not compatible with the S3 Reference field just yet.

When attempting to Save a product after selecting an S3 Reference file in the new Commerce File branch, I get a Fatal error:

PHP Fatal error: Call to undefined function commerce_file_field_is_empty() in /docroot/modules/field/field.module on line 509

It looks like my field implementation for the widget creates a call to _field_filter_items($field, $items) which, in turn, creates a function programatically:

  $function = $field['module'] . '_field_is_empty';

Since the field was provided by commerce_file, the widget is looking for the function commerce_file_field_is_empty(), which doesn't currently exist. I'm working on a fix.

On top of that, the autocomplete now only works if I start typing in "s3://" into the autocomplete picker field. I'll work on that too.

bojanz’s picture

Did you have 7.x-1.x installed on that site? Because it sounds like it is actually crashing on the old field provided by 7.x-1.x
The commerce_file field created by 7.x-2.x has $field['module'] set to 'file', I just checked.

torgosPizza’s picture

Yeah, I did. Looks like I should remove that old field first :)

Still having issues with the autocomplete, which I'm happy to follow up with here.

Thanks!

bojanz’s picture

Status: Active » Fixed

I'm closing this issue again, until we can confirm that the s3 reference field bug is coming from commerce_file.

deggertsen’s picture

3) It is your responsibility to configure the AmazonS3 module to serve authenticated urls that force the download.

Could someone please explain to me how to get downloads to force save as? I have been trying to figure this out for the last year, and still don't understand it.

#1702314: Torrents, Presigned URLs, Force Save As. How to use

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Expand