问题描述
我有一个django项目,尝试将文件上传到S3存储桶-我已经完成所有设置,并且可以在localhost上运行,但是当我部署到活动服务器时,它不会上传,而是上传到活动服务器文件夹。
我曾尝试从django admin localhost上传文件,它上传到S3存储桶,但是当我尝试从实时服务器上传时-它上传到localserver
我不知道如何尝试调试它,并且尝试检查error.nginx等的日志。
任何人都可以帮助我如何在实时服务器中调试或修复此问题。
我是django的新手,如果没有以适当的方式提及任何内容,请多多包涵。
Settings.py
"""
Django settings for abo_back project.
Generated by 'django-admin startproject' using Django 3.0.8.
For more information on this file,see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values,see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR,...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'xxxxxxxxxx'
DEBUG = True
# ALLOWED_HOSTS = []
# python manage.py runserver 0.0.0.0:8000
# python manage.py runserver 192.168.0.2:8000
# Application definition
INSTALLED_APPS = [
'rest_framework','rest_framework.authtoken','dj_rest_auth','corsheaders','django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','users.apps.UsersConfig','storages','allauth.socialaccount.providers.facebook','allauth.socialaccount.providers.google',]
AUTH_USER_MODEL = 'users.CustomUser'
SITE_ID = 1
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','corsheaders.middleware.CorsMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',]
ROOT_URLCONF = 'abo_back.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [os.path.join(BASE_DIR,'templates')],'APP_DIRS': True,'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},]
WSGI_APPLICATION = 'abo_back.wsgi.application'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3','NAME': os.path.join(BASE_DIR,'db.sqlite3'),}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_ROOT = os.path.join(BASE_DIR,"static/")
STATIC_URL = '/static/'
USE_S3 = "TRUE"
if USE_S3:
# aws settings
AWS_ACCESS_KEY_ID = 'xxxxxxxx'
AWS_SECRET_ACCESS_KEY = 'yyyyyyyyyyyy'
AWS_STORAGE_BUCKET_NAME = 'abc.com'
AWS_DEFAULT_ACL = 'public-read'
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
# s3 static settings
AWS_LOCATION = 'static'
PUBLIC_MEDIA_LOCATION = 'media'
MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{PUBLIC_MEDIA_LOCATION}/'
DEFAULT_FILE_STORAGE = 'abo_back.storage_backends.PublicMediaStorage'
STATICFILES_STORAGE = 'abo_back.backends.s3boto3.S3Boto3Storage'
else:
STATIC_URL = '/staticfiles/'
STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')
MEDIA_URL = '/mediafiles/'
MEDIA_ROOT = os.path.join(BASE_DIR,'mediafiles')
STATICFILES_DIRS = (os.path.join(BASE_DIR,'static'),)
CORS_ORIGIN_WHITELIST = [
"http://localhost:3000","http://localhost:3001","http://127.0.0.1:3000","http://192.168.0.2:8000",]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',# 'rest_framework.permissions.AllowAny',"rest_framework.permissions.DjangoModelPermissions",),'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication','DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination','PAGE_SIZE': 2
}
REST_AUTH_SERIALIZERS = {
'TOKEN_SERIALIZER': 'users.serializer.MyCustomTokenSerializer','PASSWORD_RESET_SERIALIZER':'users.serializer.PasswordResetSerializer'
}
# REST_AUTH_SERIALIZERS = {
# # 'LOGIN_SERIALIZER': 'path.to.custom.LoginSerializer',# # 'TOKEN_SERIALIZER': 'path.to.custom.TokenSerializer',# }
CSRF_COOKIE_NAME = "csrftoken"
# --------------------------------new from auth demo
AUTHENTICATION_BACKENDS = [
# Needed to login by username in Django admin,regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',# `allauth` specific authentication methods,such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',]
storage_backends.py
from storages.backends.s3boto3 import S3Boto3Storage
from django.conf import settings
class StaticStorage(S3Boto3Storage):
location = 'static'
default_acl = 'public-read'
class PublicMediaStorage(S3Boto3Storage):
location = 'media'
default_acl = 'public-read'
file_overwrite = False
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)