Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate settings when storage is instantiated; not imported #524

Merged
merged 1 commit into from
Mar 10, 2020
Merged

Calculate settings when storage is instantiated; not imported #524

merged 1 commit into from
Mar 10, 2020

Conversation

jdufresne
Copy link
Contributor

Makes the storage classes usable with Django's override_settings. For example, projects can now change the S3 location & bucket when running their tests. Affects the following backends:

  • azure_storage
  • gcloud
  • gs
  • s3boto
  • s3boto3

The remaining storage backends do not need to be altered as they do no calculate settings at import time.

The class BaseStorage was created to use a common pattern to initialize settings for various backends. Users of these backends can expect them to work consistently. This class is fully backwards compatible. Settings can now be set:

  • In settings.py
  • Using override_settings
  • As a class variable
  • As an argument to init

Closes #498

Completes all necessary backends and adds tests.

@codecov-io
Copy link

codecov-io commented Jul 6, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@87c42d1). Click here to learn what that means.
The diff coverage is 81.08%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #524   +/-   ##
=========================================
  Coverage          ?   75.72%           
=========================================
  Files             ?       12           
  Lines             ?     1524           
  Branches          ?        0           
=========================================
  Hits              ?     1154           
  Misses            ?      370           
  Partials          ?        0
Impacted Files Coverage Δ
storages/backends/azure_storage.py 0% <0%> (ø)
storages/backends/gcloud.py 94.61% <100%> (ø)
storages/backends/gs.py 61.01% <100%> (ø)
storages/backends/s3boto.py 87.54% <100%> (ø)
storages/backends/s3boto3.py 86.68% <100%> (ø)
storages/base.py 91.66% <91.66%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 87c42d1...78d40a0. Read the comment docs.

@sww314
Copy link
Contributor

sww314 commented Jul 6, 2018

@jdufresne This looks good, but it will not merge cleanly after I merged on of your other changesets. Can you update?

@jdufresne
Copy link
Contributor Author

Thanks @sww314 I have rebased on to the latest master.

@jdufresne
Copy link
Contributor Author

Rebased to resolve merge conflicts.

Copy link
Contributor

@sww314 sww314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look good to me, but I would like another person to review as well.

@sww314
Copy link
Contributor

sww314 commented Jul 12, 2018

@jschneier These changes look good to me. Can you review as well?

@jschneier
Copy link
Owner

Looking now. This will close what I consider by far the biggest issue in the original design of the library.

@jdufresne
Copy link
Contributor Author

I have rebased to resolve all merge conflicts. Ready for additional review. Thanks!

@sww314
Copy link
Contributor

sww314 commented Sep 11, 2018

@jschneier @jdufresne Now that 1.7 is out. Maybe we can get this merged for version 2?

@jdufresne
Copy link
Contributor Author

I've rebased this to the latest master. Feedback welcome.

@yozlet
Copy link

yozlet commented Oct 24, 2018

@jschneier Bumping this PR - it would be very useful for those of us having to initialise credentials from other sources (e.g. reading Google Cloud creds from an env var rather than a file).

@sushifan
Copy link

@jschneier, any indication as to when this might make it into an official release? Would be extremely useful as we often need to switch between multiple sets of storage settings on the fly. Thanks!

@jschneier
Copy link
Owner

jschneier commented Nov 15, 2018 via email

@yozlet
Copy link

yozlet commented May 20, 2019

@sww314 Are you able to merge this? (Note that Travis last tested this PR several months ago, so it would be good to check they still pass.)

@jdufresne
Copy link
Contributor Author

Note that Travis last tested this PR several months ago, so it would be good to check they still pass.

Rebased. Travis is still green.

@jdufresne
Copy link
Contributor Author

Hi, is there still interest in this PR? I'll happily rebase if it will be reviewed for inclusion. Just let me know.

@sushifan
Copy link

sushifan commented Mar 5, 2020

Hi, is there still interest in this PR? I'll happily rebase if it will be reviewed for inclusion. Just let me know.

I've been using the following workaround this entire time, but the original idea of this PR still seems like a great, essential feature... My workaround is defining multiple storage backends and using them as appropriate:

from storages.backends.s3boto3 import S3Boto3Storage


class PrivateS3OverwriteStorage(S3Boto3Storage):
    """Only specify params that are different from those in Django settings."""
    def __init__(self):
        super().__init__(file_overwrite=True)


class PublicS3OverwriteStorage(S3Boto3Storage):
    """Only specify params that are different from those in Django settings."""
    def __init__(self):
        super().__init__(default_acl='public-read', file_overwrite=True, querystring_auth=False)

@jschneier
Copy link
Owner

jschneier commented Mar 5, 2020 via email

@jdufresne
Copy link
Contributor Author

Great! I have rebased.

@jschneier
Copy link
Owner

Can you please change the method name to get_default_settings? Good to go after that. Thanks for your patience as usual.

Makes the storage classes usable with Django's override_settings. For
example, projects can now change the S3 location & bucket when running
their tests. Affects the following backends:

- azure_storage
- gcloud
- s3boto3

The remaining storage backends do not need to be altered as they do no
calculate settings at import time.

The class BaseStorage was created to use a common pattern to initialize
settings for various backends. Users of these backends can expect them
to work consistently. This class is fully backwards compatible. Settings
can now be set:

- In settings.py
- Using override_settings
- As a class variable
- As an argument to __init__

Closes #498

Completes all necessary backends and adds tests.
@jdufresne
Copy link
Contributor Author

👍 done.

@jschneier jschneier merged commit 955ee63 into jschneier:master Mar 10, 2020
@terencehonles
Copy link
Contributor

@jdufresne @jschneier I was just updating an unrelated PR, but I was wondering should BaseStorage provide a default for get_default_settings which returns an empty {}? Right now this would break for any custom storages, but if the base storage was customized to not raise ImproperlyConfigured but a deprecated warning it would be a soft fail until the next major release.

Since it will only take me a couple moments to write and I'm already updating an existing PR I'll just create a PR for this, and we can go from there.

@jdufresne
Copy link
Contributor Author

Right now this would break for any custom storages,

Custom storages aren't yet inheriting from BaseStorage as it is brand new.

But I'd also be fine with an implementation that return {}.

@terencehonles
Copy link
Contributor

@jdufresne I was just going to update my comment with that, sorry, disregard my previous message.

I'm not sure an empty {} is warranted because it's likely that a storage is providing some options, and it should use the get_default_settings for that? Or do you suspect people would create a new storage based off the BaseStorage that would have no settings? I guess for maybe a private source/single use storage that might be the case.

@jdufresne jdufresne deleted the override branch March 18, 2020 17:24
tomkins added a commit to developersociety/devsoc-contentfiles that referenced this pull request Apr 29, 2020
brianhelba added a commit to brianhelba/django-storages that referenced this pull request Sep 8, 2020
…rted

This allows the "storages.backends.s3boto3" module to be cleanly imported
before Django settings are configured. This further supports the changes
from jschneier#524.

Removing "S3Boto3StorageFile.buffer_size" as a class variable does not
affect the API surface of S3Boto3StorageFile, as "buffer_size" was
always accessible as an instance variable.
brianhelba added a commit to brianhelba/django-storages that referenced this pull request Sep 8, 2020
…rted

This allows the "storages.backends.s3boto3" module to be cleanly imported
before Django settings are configured. This further supports the changes
from jschneier#524.

Removing "S3Boto3StorageFile.buffer_size" as a class variable does not
affect the API surface of "S3Boto3StorageFile", as "buffer_size" was
always accessible as an instance variable.
jschneier pushed a commit that referenced this pull request Nov 16, 2020
…rted (#930)

This allows the "storages.backends.s3boto3" module to be cleanly imported
before Django settings are configured. This further supports the changes
from #524.

Removing "S3Boto3StorageFile.buffer_size" as a class variable does not
affect the API surface of "S3Boto3StorageFile", as "buffer_size" was
always accessible as an instance variable.
mlazowik pushed a commit to qedsoftware/django-storages that referenced this pull request Mar 9, 2022
…rted (jschneier#930)

This allows the "storages.backends.s3boto3" module to be cleanly imported
before Django settings are configured. This further supports the changes
from jschneier#524.

Removing "S3Boto3StorageFile.buffer_size" as a class variable does not
affect the API surface of "S3Boto3StorageFile", as "buffer_size" was
always accessible as an instance variable.
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.

7 participants