Tuesday, 27 April 2021

How to reference S3 bucket inside views within a Django project?

I'm using an S3 to deliver static content. I only use Django as a RESTful API, my frontend is built using React.

I do however use one HTML file as a template. The HTML file is only used as part of the HTML content of an email message sent by my server.

I plan on moving this HTML file to my S3 Bucket. I'm trying to wrap my head around how I may reference such file within my S3.

Normally to reference a standard HTML template in django, it can look something like this:

html = render_to_string('email/email_confirm.html', context)

email being the name of the folder inside templates.

I know how to work with pathing within an S3, but how can I pull up and reference that object directly within my view below? I have already configured all my S3 settings


class CustomUserCreate(APIView):
    permission_classes = [AllowAny]

    def post(self, request, format='json'):
        serializer = CustomUserSerializer(data=request.data)

        if serializer.is_valid():
            user = serializer.save()
            if user:
                # GENERATE EMAIL CONFIRMATION TOKEN
                user_data = serializer.data
                user = User.objects.get(email=user_data['email'])

                token = RefreshToken.for_user(user).access_token

                # GENERATE EMAIL CONFIRMATION TEMPLATE
                current_site = get_current_site(request).domain
                relative_link = reverse('users:email-verify')


                # CHANGE TO HTTPS in PRODUCTION
                absurl = 'http://'+current_site+relative_link+"?token="+str(token)
                email_body = 'Hi '+ user.username+', Please use link below to verify your email \n' + absurl
                
                context = {
                    'name': user.first_name,
                    'url': absurl
                }
                html = render_to_string('email/email_confirm.html', context) # How can I fix this path to pull my template from my s3 bucket?
                


                data = {'to_email':user.email,
                        'email_subject': 'Please Verify Your Email',
                        'email_body':email_body,
                        'html_message':html
                }


                Util.send_email(data)

                return Response(user_data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Here are my S3 configurations:

AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_S3_ENDPOINT_URL = config('AWS_S3_ENDPOINT_URL')
AWS_S3_CUSTOM_DOMAIN = config('AWS_S3_CUSTOM_DOMAIN')
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
AWS_LOCATION = config('AWS_LOCATION')
AWS_DEFAULT_ACL = 'public-read'



STATIC_URL = '{}/{}/'.format(AWS_S3_ENDPOINT_URL, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'


from How to reference S3 bucket inside views within a Django project?

No comments:

Post a Comment