SuspiciousOperation - Попытка доступа к '%s' отклонена | Django
Я загрузил свое веб-приложение в Интернет. В нем есть секция, которая отправляет аудиофайл на вход. Ранее я использовал FileSystemStorage для хранения файлов по пути "media/my_audio", но теперь, когда я загрузил приложение на Google Cloud Platform, я больше не могу этого делать. Я уже все настроил, но мне кажется, что в моем коде есть какая-то проблема.
Вот код, который я пытался использовать:
"settings.py"
PROJECT_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
# STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
GS_PROJECT_ID = 'my_id'
GS_STATIC_BUCKET_NAME = 'my_STATIC_BUCKET_NAME'
GS_MEDIA_BUCKET_NAME = 'my_MEDIA_BUCKET_NAME' # same as STATIC BUCKET if using single bucket both for static and media
STATIC_URL = 'https://storage.googleapis.com/{}/'.format(GS_STATIC_BUCKET_NAME)
STATIC_ROOT = "static/"
MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_MEDIA_BUCKET_NAME)
MEDIA_ROOT = "media/"
UPLOAD_ROOT = 'media/uploads/'
DOWNLOAD_ROOT = os.path.join(PROJECT_ROOT, "static/media/downloads")
DOWNLOAD_URL = STATIC_URL + "media/downloads"
"views.py"
def Risultato(request):
testo = request.POST.get('testo')
lang = request.POST.get('lang')
audio = request.FILES['audio']
fs = GoogleCloudStorage(location=settings.MEDIA_URL)
filename = fs.save(audio.name, audio)
os.environ["COQUI_TOS_AGREED"] = "1"
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
file = tts.tts_to_file(text=testo,
file_path=f"https://storage.googleapis.com/MEDIA_URL/my_output/{filename}",
speaker_wav=f"https://storage.googleapis.com/MEDIA_URL/media/my_audio/{filename}",
language=lang,
split_sentences=False
)
context = {
'file':file,
'filename':filename,
}
return render(request, 'risultato.html', context)
"gcloud.py"
from django.conf import settings
from storages.backends.gcloud import GoogleCloudStorage
from storages.utils import setting
from urllib.parse import urljoin
class GoogleCloudMediaFileStorage(GoogleCloudStorage):
# Google file storage class which gives a media file path from MEDIA_URL not google generated one.
bucket_name = setting('GS_MEDIA_BUCKET_NAME')
def url(self, name):
# Gives correct MEDIA_URL and not google generated url.
return urljoin(settings.MEDIA_URL, name)
class GoogleCloudStaticFileStorage(GoogleCloudStorage):
# Google file storage class which gives a media file path from MEDIA_URL not google generated one.
bucket_name = setting('GS_STATIC_BUCKET_NAME')
def url(self, name):
# Gives correct STATIC_URL and not google generated url.
return urljoin(settings.STATIC_URL, name)