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

Let media have a different bucket if desired #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions zappa_file_widget/url_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,23 @@ def render(self, name, value, attrs=None):
attrs['type'] = 'hidden'
parent_html = super(URLWidget, self).render(name, value, attrs=attrs)
template = Template(html_template)
s3_host = 's3-ap-southeast-1.amazonaws.com'
default_region = getattr(settings, 'AWS_S3_HOST', s3_host).split(".amazonaws.com")[0].split("s3-")[1]
aws_access_key_id = getattr(settings, 'MEDIA_AWS_ACCESS_KEY_ID', 'AWS_ACCESS_KEY_ID')
aws_secret_access_key = getattr(settings, 'MEDIA_AWS_SECRET_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY')
s3_host_from_settings = getattr(settings, 'MEDIA_AWS_S3_HOST', 'AWS_S3_HOST')
s3_host = s3_host_from_settings or 's3-ap-southeast-1.amazonaws.com'
default_region = s3_host.split(".amazonaws.com")[0].split("s3-")[1]
aws_storage_bucket_name = getattr(settings, 'MEDIA_AWS_STORAGE_BUCKET_NAME', 'AWS_STORAGE_BUCKET_NAME')
Copy link
Owner

Choose a reason for hiding this comment

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

I appreciate your concept of having separate media bucket name. But consider
AWS_STORAGE_BUCKET_NAME this is going to be the default value if no MEDIA_AWS_STORAGE_BUCKET_NAME defined in the settings.
My Concern is to provide backward compatibility.

Copy link
Author

Choose a reason for hiding this comment

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

Excuse me if I do not understand your comment correctly: the proposed changes are meant to provide full backwards compatibility. Unless settings.MEDIA_xxx is provided the exact same settings.xxx your code uses is used for xxx. One example (the other two context keys behave in the exact same way):

  1. branch master:
    context['AWS_ACCESS_KEY_ID'] == settings.AWS_ACCESS_KEY_ID

  2. branch feature/allow_media_differentiation, with settings.MEDIA_AWS_ACCESS_KEY_ID not defined:
    context['AWS_ACCESS_KEY_ID'] == settings.AWS_ACCESS_KEY_ID

  3. branch feature/allow_media_differentiation, with settings.MEDIA_AWS_ACCESS_KEY_ID defined:
    context['AWS_ACCESS_KEY_ID'] == settings.MEDIA_AWS_ACCESS_KEY_ID

case 2) defaults back to the same values as case 1) so someone upgrading to the new version of zappa-file-widget and not modifying his settings would see no difference.

Copy link
Owner

Choose a reason for hiding this comment

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

ok @rebost

prefix = settings.MEDIAFILES_LOCATION + "/" + self.upload_to
context = Context({
'field_name': name,
"field_value": value,
'parent_html': parent_html,
'prefix': prefix,
"prefix_url": settings.MEDIA_URL + self.upload_to,
'AWS_ACCESS_KEY_ID': settings.AWS_ACCESS_KEY_ID,
'AWS_SECRET_ACCESS_KEY': settings.AWS_SECRET_ACCESS_KEY,
'AWS_ACCESS_KEY_ID': aws_access_key_id,
'AWS_SECRET_ACCESS_KEY': aws_secret_access_key,
'AWS_DEFAULT_REGION': default_region,
'AWS_STORAGE_BUCKET_NAME': settings.AWS_STORAGE_BUCKET_NAME
'AWS_STORAGE_BUCKET_NAME': aws_storage_bucket_name,
})
html = template.render(context)

Expand Down