Skip to content

Get template file URLs using default_storage#286

Open
uci-oit-or wants to merge 1 commit intousnistgov:masterfrom
uci-oit-or:file-urls
Open

Get template file URLs using default_storage#286
uci-oit-or wants to merge 1 commit intousnistgov:masterfrom
uci-oit-or:file-urls

Conversation

@uci-oit-or
Copy link
Copy Markdown

Hello @rptmat57,

This is a suggestion to possibly make NEMO's method of retrieving file URLs work with more storage methods. This came about because at UCI we are attempting to have files uploaded to Amazon S3 (with the help of django-storages). Viewing/downloading the files worked well when using FileField or ImageField, but errors occurred with the customization template files (emails, jumbotron, etc.) since they use a prefixed MEDIA_URL.

The change I made adds a custom template tag that utilizes default_storage.url. I tested that this also works with the previous way we were storing media files (locally on disk in a media folder). This should hopefully mean the change won't negatively affect any other NEMO users.

Please let me know if this change isn't wanted/needed or if more needs to be added.

Thank you,
Aaron

@rptmat57
Copy link
Copy Markdown
Contributor

rptmat57 commented May 4, 2025

Hello,
So that sounds like a good idea.
One thing I am worried about is that we have protected files that right now which are served via NEMO who checks that the user is staff before sending the file.
We also have the API media endpoint which serves media files (I think that one shouldn't be affected).

How does that work with S3? Maybe this is not a pb right now, but if you are using the contracts plugin (with procurement files) or the billing plugin (with invoice files) then are the files uploaded to S3 protected in any way or could anyone access them freely?

@abuckles-uci
Copy link
Copy Markdown
Contributor

Hi @rptmat57,

Thanks for the response! I don't think that this PR would change the behavior with protected files. It only changes how the URL gets inserted into the template file. So if a page is staff only, staff would be the only ones able to access the file. In our case, because the S3 bucket is private, the URL needs to be built dynamically each time with auth info (the URL will expire after 3600 seconds by default). If our bucket was public this would be much easier and we probably wouldn't need to create this pull request.

We're not utilizing file uploads with the contracts or billing plugins, just NEMO itself, so I can't really speak on this.

The way we're connecting to S3 is by installing django-storages via pip. Then inside of settings.py there's something like the code block below. The session_profile is used for authentication purposes. When developing locally on my computer I login via the AWS CLI and session information is retrieved from ~/.aws/.

STORAGES = {
    "default": {
        "BACKEND": "storages.backends.s3.S3Storage",
        "OPTIONS": {
            "location": "media",
            "session_profile": "<SESSION_PROFILE>",
            "bucket_name": "<BUCKET_NAME>",
            "region_name": "us-west-2",
            "signature_version": "s3v4",
        },
    },
    "staticfiles": {
        "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
    },
}

Hope this makes sense.

Thanks,
Aaron

@abuckles-uci
Copy link
Copy Markdown
Contributor

Hi @rptmat57,

Are there any updates with this?

Thanks,
Aaron

@abuckles-uci
Copy link
Copy Markdown
Contributor

Hi @rptmat57,

I was wondering if there was any chance of this being merged? If not, I can close this PR and just apply the changes in my fork.

Thanks,
Aaron

@rptmat57
Copy link
Copy Markdown
Contributor

Hi @abuckles-uci and thank you for your patience and the contribution.

This is unlikely to be merged anytime soon.
You can keep it or close it, up to you.

@abuckles-uci
Copy link
Copy Markdown
Contributor

Thanks for letting me know! I'll close this PR and implement the change in my fork only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants