Managed File Upload to S3 Drupal 7
Don't forget to subscribe to our YouTube channel to stay up-to-appointment.
File Management Series
Drupal doesn't support the ability to replace existing files. You tin can create and delete files, just you can't supervene upon a file without using a module. If y'all try to upload a file with the same name Drupal will suspend "_0", "_1", etc… to the filename and increment it.
Luckily 2 modules tin aid with replacing files, and they're called Media Entity File Replace and File Replace.
What is the primary difference betwixt Media Entity File Replace and File Supplant?
Both modules will replace "files" whilst retaining the original file's filename by performing an "overwrite" function in the backend. In Drupal, an uploaded "file" can be a "media entity" or a "file entity".
As the module names suggest, the main difference is that Media Entity File Replace works at the media entity level, whereas File Replace works at the file entity level.
This module allows editors to supersede files at the media entity level past overwriting the existing media files. Because it overwrites the files, the filename and path are exactly retained.
You should utilise this module to let content editors to replace media entity files whilst keeping the same filename and path equally the original file.
Getting Started
1. Download and enable the module. This module requires no additional libraries and can but be installed with composer which is the recommended manner.
To install using Composer:
composer require drupal/media_entity_file_replace If you get the following fault when using Composer:
[InvalidArgumentException] Could non find a version of package drupal/media_entity_file_replace matching your minimum-stability (stable). Crave it with an explicit version constraint assuasive its desired stability. And so specify the version:
composer require drupal/media_entity_file_replace:^one.0-beta3 To enable the module using Drush:
drush en media_entity_file_replace two. Articulate the cache. You can easily clear the cache via the Drupal admin UI at admin/config/evolution/performance or by the drush command:
drush cr A quick overview of this footstep is adding a media field to an existing content type and uploading an bodily media document. If you already have a media field in your content type and an uploaded media file then you can skip to step three.
A. For whatever content blazon (we will use the default Drupal Commodity content type in this tutorial), add a Media field and check "Document" as the Reference type. Run into Image ane beneath.
B. Become to Content >> Media and click on "Add Media". At the next folio, select "Document" and proceed to upload a file. Note the filename.
3. Now enable the "Replace file" widget that comes from the Media Entity File Supersede module.
Go to Construction >> Media Types >> Document >> Manage Form Brandish and enable the "Replace file" widget. This widget is disabled by default. See Image 2 below. You will not be able to supercede a file until you enable this widget.
It'southward a good idea to position the "Replace file" widget right under the file widget as shown in Epitome two so that when a user is editing a file, the "Supersede file" widget is right under the file that they desire to replace.
Without this module and widget enabled, the default Drupal interface will appear equally in Image 3.
4. Now if you go to Content >> Media and edit the document media that nosotros uploaded in Step 2.B, you lot will meet the "Replace file" widget as shown in Image 4 below. Discover that the default Drupal widget is automatically removed (meet the difference between Image iii and Image 4).
5. Go ahead and replace the original uploaded file with a new file with a unlike filename. Upon replacing the file, if you left the default option "Overwrite original file" checked, then Drupal will upload the new file and set the former file for deletion. Even if your new file has a new file proper noun, Drupal volition store the new file whilst retaining the filename of the original file. Note that the filename is the same as that noted from Step 2.B.
If you click on the new uploaded file even later doing a refresh in the browser, you may nevertheless see the old content of the original file. You must do a difficult refresh in your browser to see the contents of the newly uploaded file.
More information: How to practice a hard refresh in Chrome, Firefox and IE.
File Replace
This module allows editors to supercede files at the file entity level by overwriting the existing files. Because it overwrites the files, the filename and path are exactly retained.
You should use this module if you want to let content editors to supplant file entities whilst keeping exactly the aforementioned filename and path as the original file. It is useful in cases where existing files in Drupal demand to be updated occasionally.
Getting Started
ane. Download and enable the module. This module requires no additional libraries and can simply be installed with composer which is the recommended way.
Using Composer:
composer require drupal/file_replace To enable the module using Drush:
drush en file_replace 2. Clear the cache. You tin hands articulate the cache via the Drupal admin UI at admin/config/development/performance or by the drush command:
drush cr three. In that location is one extra manual footstep that yous must practise. Because this module only provides the "Replace page" for each file upload, it does not provide a link to this page from the Drupal UI. You can access the "Supercede page" direct past typing the URL into the browser. You can manually type the following URL into the browser:
admin/content/files/supersede/{{ fid }} Where {{ fid }} is the file id of the file you want to supplant. For case, if the fid was half dozen, then you would go to:
admin/content/files/replace/6 This is not ideal. We volition show yous how to link to the page from the default Drupal Files overview page. (The outcome nosotros are trying to achieve is shown in Epitome 11.)
Go to Content >> Files and click on "Edit view" as shown in Epitome 5.
Alternatively, you lot can get to Structure >> Views and edit the Files view as shown in Image half dozen.
four. Add a Custom text field to the Files View as shown in Image vii.
v. So add some custom text for the text of the link. See Image 8
Also, edit the "Rewrite Results" section and enter the following in the "Link path":
admin/content/files/replace/{{ fid }} You can likewise enter custom text in the "Title text" field although this is simply for aesthetic purposes. See Epitome 9 below.
Now click on "Apply (all displays)" and save your View.
half-dozen. The last step is to enable the "Supplant files" permission for the role that yous want. Come across Prototype 10.
vii. Now go to the Files overview page at Content >> Files and you will see a "supercede" link every bit shown in Image eleven.
Click on the "supervene upon" link next to the file you desire to supplant and this volition take you to another page where you can upload a new file every bit shown in Image 12.
Mutual Pitfall – Caching
Static files served past Drupal are usually buried externally (outside of Drupal) by services such as Varnish, Content Delivery Network (CDN) and the user's browser. Past default, a user's browser will not pick up the content of the newly uploaded file until after the max-age header which is set in .htaccess for two weeks for static files. In other words, if yous replace a file and do nothing, end users volition see the new file content after two weeks (unless they locally exercise a hard refresh).
Immigration Drupal'south internal cache will non solve this caching caveat considering Drupal is not involved with serving static files. This is the job of your webserver.
There are a few options worth mentioning:
- Read the department on Caching on Media Entity File Supercede module's homepage (2 options are listed)
- Later on you replace a file, manually articulate your Varnish/CDN/CloudFlare cache (not ideal but it works).
- Follow the discussion to integrate with the Purge module
Summary
Drupal does not retain filenames for uploaded files that are replaced or overwritten. Additionally, Drupal stores uploaded files every bit either media entities or file entities depending on your set up. This tutorial demonstrated how to retain existing file names when replacing media or file entities. In that location are two contributed modules yous can use to solve this problem and they are Media Entity File Replace and File Supercede. These modules will allow you to overwrite existing media and files respectively, whilst keeping their original filenames.
stevensfonumene62.blogspot.com
Source: https://www.webwash.net/how-to-replace-files-in-drupal/
Post a Comment for "Managed File Upload to S3 Drupal 7"