Skip to main content
Last updated: 12 Jul 2024

Manage assets

Get an asset’s ID

If the asset URL starts with /media, the asset ID is in the URL. For example, for the URL https://assets.publishing.service.gov.uk/media/65f2c1110e1c2f8c4dffaa53/my_file.jpg, the ID is 65f2c1110e1c2f8c4dffaa53.

For assets that do not start /media, obtain the ID by running the following rake task:

k exec deploy/asset-manager -- rake assets:get_id_by_legacy_url_path[SLUG]

For example, for https://assets.publishing.service.gov.uk/government/uploads/uploaded/hmrc/realtimepayetools-update-v23.xml", you would pass /government/uploads/uploaded/hmrc/realtimepayetools-update-v23.xml as the argument.

Remove an asset

Where possible, you should delete the asset in the publishing application that originally uploaded the file, for example Whitehall. Deleting an asset from Asset Manager without removing the record from the publishing application could cause problems if the asset is attempted to be re-published again in the future.

If it isn’t feasible to remove the asset in the publishing app, you can use these steps to remove the asset from assets.publishing.service.gov.uk in Asset Manager.

  1. Get the asset ID.

  2. Decide if the asset can be marked as deleted or whether all traces need to be removed (e.g. if it contains secret information).

To soft delete the asset (i.e. mark the asset as deleted in Asset Manager’s database, but retain the file in cloud storage), run:

k exec deploy/asset-manager -- rake assets:delete[ASSET_ID]

To delete all traces of the asset (including the file from cloud storage), run:

k exec deploy/asset-manager -- rake assets:delete[ASSET_ID, true]

For “Whitehall” Assets (paths starting with /government/uploads/system), use the assets:whitehall_delete Rake task instead:

k exec deploy/asset-manager -- rake assets:whitehall_delete[ASSET_PATH]
  1. Add a query string to the URL (e.g. ?cache-bust=12345) to bypass the cache and check that the asset responds with a 404 not found.

  2. Wait 5 minutes for the cache to clear, or purge it yourself.

  3. Verify that the URL returns a 404 response without using a query string.

  4. Request removal of the asset from Google’s search results:

    • Go to the Google Search Console
    • Click ‘New request’
    • Select ‘To refresh Google’s outdated result for a webpage that has changed’
    • Click ‘Next’
    • Enter the page URL and click ‘Submit’
  5. Remove the asset from the Google Cloud Platform (GCP) mirror:

    • Log into the GCP console.
    • Go to the GOVUK Production project under the DIGITAL.CABINET-OFFICE.GOV.UK organisation.
    • Select Cloud Storage -> Browser, go to the govuk-production-mirror bucket.
    • Navigate to the file, then delete it.
  6. Remove the asset from the Amazon Web Services (AWS) mirror:

gds aws govuk-production-poweruser aws s3 rm s3://govuk-production-mirror/assets.publishing.service.gov.uk/<slug>

Redirect an asset

Sometimes it might be necessary to manually redirect an asset, for example if an associated document wasn’t unpublished correctly.

Ideally, the redirect should be done in the publishing application that originally uploaded the file (e.g. Whitehall).

If it isn’t possible or desirable to redirect the asset in the publishing app, use these steps to remove the asset in Asset Manager:

  1. Get the asset ID.
  2. Run the following rake task:
k exec deploy/asset-manager -- rake assets:redirect[ASSET_ID,REDIRECT_URL]

For Whitehall assets you will have to run:

k exec deploy/whitehall_redirect -- rake assets:redirect[LEGACY_PATH_URL,REDIRECT_URL]

Upload an asset

Some publishing apps such as Mainstream Publisher do not provide the facility for editors to upload assets such as images and PDFs. In these rare cases, we can upload assets to asset-manager manually and give the URL to content editors to embed.

Production assets are replicated to staging and integration nightly, so it is best to simply perform the upload directly in production.

If the pod terminates at any point during this process (e.g. a deployment occurs), you will need to start again as the /tmp directory is specific to the pod.

  1. Get the name of an asset-manager app pod.
   POD=$(kubectl get pods -l app=asset-manager -o name | head -1)
   echo $POD
  1. Upload the file to the pod’s /tmp directory:
   k cp my_file.jpg apps/$POD:/tmp
  1. Run the create_asset command on the same pod:
   k exec -it $POD -- bin/create_asset /tmp/my_file.jpg

The output will look like the following:

   Saved!
   Asset id: 65f2c1110e1c2f8c4dffaa53
   Asset name: my_file.jpg
   Asset basepath: /media/65f2c1110e1c2f8c4dffaa53/my_file.jpg

The asset should now be available at https://assets.publishing.service.gov.uk/{base-path-from-previous-output}, for example https://assets.publishing.service.gov.uk/media/65f2c1110e1c2f8c4dffaa53/my_file.jpg.

Replace an asset

If you need to replace the file of an existing attachment, follow these steps.

The replacement file must have a different filename to the one it is replacing.

Where possible, you should do the replacement in the publishing application that originally uploaded the file, for example Whitehall.

If it isn’t possible or desirable to replace the asset in the publishing app, use these steps to remove the asset in Asset Manager:

  1. Get the name of an asset-manager app pod.
   POD=$(kubectl get pods -l app=asset-manager -o name | head -1)
   echo $POD
  1. Upload the file to the pod’s /tmp directory:
   k cp my_file.jpg apps/$POD:/tmp
  1. Open an app console on that same pod:
   k exec -it $POD -- rails c
  1. Find the asset by its ID (see “Get an asset’s ID”):
   asset = Asset.find(asset_id)
  1. Check the asset is what you think it is.

  2. Replace the file:

   asset.file = Pathname.new("/tmp/filename.ext").open
   asset.send(:store_metadata)
   asset.save!
  1. If the attachment is a PDF that was originally uploaded in Whitehall, update the page count field in Whitehall:
   k exec -it deploy/whitehall-admin -- rails c
   attachment_data = Asset.find_by(asset_manager_id: asset_id).assetable # e.g. 57a9c52b40f0b608a700000a
   attachment_data.update(file_size: 123, number_of_pages: 28) # file_size in bytes