Skip to content

Commit

Permalink
16 - AWS S3 File Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Nov 15, 2017
1 parent 3389638 commit 96d6647
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 8 deletions.
Binary file modified src/db.sqlite3
Binary file not shown.
5 changes: 3 additions & 2 deletions src/ecommerce/aws/conf.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import datetime
import os

from .ignore import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY

AWS_GROUP_NAME = "CFE_eCommerce_Group"
AWS_USERNAME = "cfe-ecommerce-user"
AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID", "AKIAJARK375PALZJC55Q")
AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY", "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")
# AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID", "AKIAJARK375PALZJC55Q")
# AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY", "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")

AWS_FILE_EXPIRE = 200
AWS_PRELOAD_METADATA = True
Expand Down
3 changes: 3 additions & 0 deletions src/ecommerce/aws/ignore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os
AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID", "AKIAI7CMDWSUQZA33PBQ")
AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY", "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")
3 changes: 2 additions & 1 deletion src/ecommerce/aws/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from storages.backends.s3boto3 import S3Boto3Storage

StaticRootS3BotoStorage = lambda: S3Boto3Storage(location='static')
MediaRootS3BotoStorage = lambda: S3Boto3Storage(location='media')
MediaRootS3BotoStorage = lambda: S3Boto3Storage(location='media')
ProtectedS3Storage = lambda: S3Boto3Storage(location='protected')
7 changes: 7 additions & 0 deletions src/ecommerce/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "media_root")


PROTECTED_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "protected_media")



from ecommerce.aws.conf import *


CORS_REPLACE_HTTPS_REFERER = False
HOST_SCHEME = "http://"
SECURE_PROXY_SSL_HEADER = None
Expand Down
2 changes: 1 addition & 1 deletion src/ecommerce/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@

PROTECTED_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "protected_media")

# from ecommerce.aws.conf import *
from ecommerce.aws.conf import *


CORS_REPLACE_HTTPS_REFERER = False
Expand Down
5 changes: 4 additions & 1 deletion src/ecommerce/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "media_root")

# from ecommerce.aws.conf import *

PROTECTED_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "protected_media")

from ecommerce.aws.conf import *


# https://kirr.co/vklau5
Expand Down
17 changes: 14 additions & 3 deletions src/products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.db.models.signals import pre_save, post_save
from django.urls import reverse

from ecommerce.aws.utils import ProtectedS3Storage
from ecommerce.utils import unique_slug_generator, get_filename

def get_filename_ext(filepath):
Expand Down Expand Up @@ -104,18 +105,28 @@ def product_pre_save_receiver(sender, instance, *args, **kwargs):

def upload_product_file_loc(instance, filename):
slug = instance.product.slug
id_ = 0
try:
id_ = instance.id
except:
Klass = instance.__class__
qs = Klass.objects.all().order_by('-pk')
id_ = qs.first() + 1

if not slug:
slug = unique_slug_generator(instance.product)
location = "product/{}/".format(slug)
location = "product/{slug}/{id}/".format(slug=slug, id=id_)
return location + filename #"path/to/filename.mp4"



class ProductFile(models.Model):
product = models.ForeignKey(Product)
file = models.FileField(
upload_to=upload_product_file_loc,
storage=FileSystemStorage(location=settings.PROTECTED_ROOT)
)
storage=ProtectedS3Storage(), #FileSystemStorage(location=settings.PROTECTED_ROOT)
) # path
#filepath = models.TextField() # '/protected/path/to/the/file/myfile.mp3'
free = models.BooleanField(default=False) # purchase required
user_required = models.BooleanField(default=False) # user doesn't matter

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

1 comment on commit 96d6647

@RayGar7
Copy link

Choose a reason for hiding this comment

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

I noticed that both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY have different corresponding values between the two files src/ecommerce/aws/conf.py and src/ecommerce/aws/ignore.py. For those of us coding along, are we supposed to have different keys between our files named like that or is this something that happened for a different reason only with this project?

Please sign in to comment.