1

I am making a Django app where one uploads a .mp3, then this .mp3 file is processed by the server, and then the user can download the processed file.

I don't know how to handle the uploads and downloads. I have tried using filetransfers but it doesn't seem to answer my problem.

PS : I am not at all familiarized with Http protocols...

tk_slo
  • 23
  • 4
  • Possible duplicate of [Need a minimal Django file upload example](https://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example) – joppich Nov 19 '18 at 15:54
  • While filetransfers most definetly fulfills your requirements, it might be a bit 'too much' for your usecase. But the [django-doc on uploads](https://docs.djangoproject.com/en/2.1/topics/http/file-uploads/) is basically a guide to doing exactly what you want to do. – joppich Nov 19 '18 at 15:58

1 Answers1

0

To load the files, the FileField class is used.

FileField class (upload_to = None, max_length = 100, ** options)

The directory where the files are saved must be specified.

Example:

class MyModel(models.Model):
    # file will be uploaded to MEDIA_ROOT/uploads
    upload = models.FileField(upload_to='uploads/')
    # or...
    # file will be saved to MEDIA_ROOT/uploads/2015/01/30
    upload = models.FileField(upload_to='uploads/%Y/%m/%d/')

You must also have configured the "settings.py" file with the file addresses.

STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')

Storage can also be done remotely, that is, have another storage server, be a server and make the connection through FTP or use another service such as AWS S3.

if used FTP:

DEFAULT_FILE_STORAGE = 'storages.backends.ftp.FTPStorage'
USER = 'userftp'
PASSWORD = "passftp"
HOST = 'ftp.host.com or IP'
PORT = 21
FTP_STORAGE_LOCATION = "ftp://{user}:{passwd}@{host}:{port}/".format(user=USER, passwd=PASSWORD, host=HOST, port=PORT)

if used S3

AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_BUCKET_NAME')
AWS_ACCESS_KEY_ID = os.getenv('AWS_S3_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY_ID = os.getenv('AWS_S3_SECRET_KEY')
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME 
AWS_S3_OBJECT_PARAMETERS = { 'CacheControl': 'max-age=86400' }
AWS_LOCATION = 'static'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'code/static'),]
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

and in the "urls.py" file add:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)