Django 应用程序不从 AWS 存储桶的媒体文件夹加载图像

2024-04-19

我在用着django-oscar,并希望使用 AWS S3 提供我的静态文件。 为了配置我的 s3 存储桶,我创建了一个名为的模块aws with conf.py and utils.py files.

在我的网站上,当我将图像上传到产品时,它会以正确的路径上传到我的 aws s3 存储桶,但在很短的时间之后,路径会从https://mybucketname.s3.amazonaws.com/media/cache/..../image.jpg to https://mybucketname.s3.amazonaws.com/cache/..../image.jpg

图像位于media我的桶里的文件夹。

我在 heroku 上托管我的网络应用程序,静态文件服务正确,但问题发生在媒体文件夹中。

这是我的代码 -

utils.py 文件

from storages.backends.s3boto3 import S3Boto3Storage

StaticRootS3BotoStorage = lambda: S3Boto3Storage(location='static')
MediaRootS3BotoStorage  = lambda: S3Boto3Storage(location='media')

as static and media是我的 s3 存储桶上的文件夹

conf.py

import datetime

AWS_ACCESS_KEY_ID = "xxx"
AWS_SECRET_ACCESS_KEY = "yyy"

AWS_PRELOAD_METADATA = True
AWS_QUERYSTRING_AUTH = False
AWS_DEFAULT_ACL = None 

DEFAULT_FILE_STORAGE = 
'myproject.aws.utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 
'myproject.aws.utils.StaticRootS3BotoStorage'
AWS_STORAGE_BUCKET_NAME = 'mybucket-name'
S3DIRECT_REGION = 'us-east-2'
S3_URL = '//%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = '//%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
STATIC_URL = S3_URL + 'static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

two_months = datetime.timedelta(days=61)
date_two_months_later = datetime.date.today() + two_months
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00 
GMT")

AWS_HEADERS = { 
    'Expires': expires,
    'Cache-Control': 'max-age=%d' % 
    (int(two_months.total_seconds()), ),
}

和我的 settings.py 我添加了这个

from myproject.aws.conf import *

我应该怎么做才能解决这个问题?


The file storage system configured for your Django app should be a class that implements django.core.files.storage.Storage [1] https://docs.djangoproject.com/en/2.1/howto/custom-file-storage/#writing-a-custom-storage-system

storages.backends.s3boto3.S3Boto3Storage already implements this storage interface. [2] https://github.com/jschneier/django-storages/blob/1.7.1/storages/backends/s3boto.py#L186

Setting StaticRootS3BotoStorage in utils.py to a lambda, the Storage system is instantiated lazily with the proper location value; but the location attribute in the storage class itself is never changes. [3] https://github.com/jschneier/django-storages/blob/1.7.1/storages/backends/s3boto.py#L216

location = setting('AWS_LOCATION', '')

Django clears properties of storage instance when the project settings changes. [4] https://github.com/django/django/blob/2.1.4/django/core/files/storage.py#L180 So that when the location attribute is resolved on the storage system, it effectively looks up the class attribute one (location value is shown in above snippet) because location attribute is missing in the instance.

这种情况可以通过子类化来解决storages.backends.s3boto3.S3Boto3Storage反而。这保证了location无论项目设置如何更改,值都不会改变。

class StaticRootS3BotoStorage(S3Boto3Storage):
    location = 'static'

class MediaRootS3BotoStorage(S3Boto3Storage):
    location = 'media'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django 应用程序不从 AWS 存储桶的媒体文件夹加载图像 的相关文章

  • python列表理解和extend() [重复]

    这个问题在这里已经有答案了 深入学习 Python 2 7 1 但未能理解这一点 几个小时 gt gt gt a 1 2 gt gt gt b 3 4 gt gt gt gt gt gt a extend b 0 gt gt gt a 1
  • 使用 Python 连接从 FTP 检索文件

    我构建了这个简单的工具来暴力破解并连接到 ftp 服务器 import socket import ftplib from ftplib import FTP port 21 ip 192 168 1 108 file1 passwords
  • 使用自定义元素类在 Python 中解析 xml

    我想使用 Python 的 xml etree ElementTree 模块解析 xml 文档 但是 我希望生成的树对象中的所有元素都具有我定义的一些类方法 这建议创建我自己的 Python 元素类的子类 但我无法告诉解析器在解析时使用我自
  • python 正则表达式中括号的奇怪行为

    我正在编写一个 python 正则表达式 它可以在文本文档中查找引用的字符串 从黑匣子中记录的航空公司飞行员的引用 我首先尝试编写具有以下规则的正则表达式 返回引号之间的内容 如果以 single 打开 则仅在以 single 关闭时返回
  • PyKCS11 不可哈希列表

    我的 python 脚本旨在获取特定 so 库中插槽 令牌的详细信息 输出如下所示 Library manufacturerID Safenet Inc Available Slots 4 Slot no 0 slotDescription
  • 如何从 python 脚本更改 python 文件中的变量值

    我目前有一个 python 文件 其中包含一堆带有值的全局变量 我想从一个单独的 python 脚本永久更改这些值 我尝试过 setattr 等 但似乎不起作用 有没有办法做到这一点 简短的回答是 不 不值得这么麻烦 听起来您正在尝试创建一
  • Matplotlib 动画未显示

    当我在家里的电脑上尝试这个时 它可以工作 但在工作的电脑上却不行 这是代码 import numpy as np import matplotlib pyplot as plt import matplotlib animation as
  • 使用 boto 和 python 从带有参数的布局创建 mTurk HIT

    我正在尝试利用 boto 在 Mechanical Turk 中生成 HIT 目标是使用我的 mTurk 帐户上已生成的通用布局 并向其传递图像 URL 以迭代创建 HIT 问题是 即使正确命名参数 如果图像 url boto 也不成功 我
  • scipy 的 curve_fit 函数的尺寸问题

    我对 python 中的曲线拟合以及一般的 python 都很陌生 目前 我正在尝试使用 scipy 中的 curve fit 模块来拟合 4 个光谱峰 简而言之 我的文本文件中有两列数据 所以我的第一步是将数据导入到两个数组中 一个包含
  • Python lmfit:拟合 2D 模型

    我正在尝试将二维高斯拟合到一些灰度图像数据 该数据由一个二维数组给出 lmfit 库实现了一个易于使用的模型类 它应该能够做到这一点 不幸的是文档 http lmfit github io lmfit py model html http
  • 如何在 Python 中重命名文件并保留创建日期

    我知道创建日期不存储在文件系统本身中 但是当我使用时我遇到了问题os rename 它正在更新我正在使用的文件的创建日期 是否可以重命名文件而不更改其原始创建日期 正如都铎所说 你可以使用os stat http docs python o
  • 在 Django 1.9 中使用信号

    在 Django 1 8 中 我能够使用信号执行以下操作 一切顺利 init py from signals import 信号 py receiver pre save sender Comment def process hashtag
  • 插入失败“OperationalError:没有这样的列”

    我尝试使用我尝试修复的姓名和电话创建一个数据库 但它会随时向我重播 File exm0 py line 14 in
  • NumPy 中 exp(-x^2) 的快速傅立叶变换

    I have to calculate numerically the 2nd derivative of a Gaussian function I ve read every question on this topic here bu
  • 分别计算男女宿舍

    我想要的结果是这样的 males 1990 Q1 value Q2 value Q3 Value Q4 Value females Q1 value Q2 value Q3 Value Q4 value 如果任何值不存在则默认值 0 imp
  • 将数值和分类数据混合到具有密集层的 keras 序列模型中

    我在 Pandas 数据框中有一个训练集 我将此数据框传递到model fit with df values 以下是有关 df 的一些信息 df values shape 981 5 df values 0 array 163 0 6 83
  • 将自定义字段添加到 Django 中的 auth_user 表

    目前我创建了另一个类 表名为MyAppUser我的自定义列 例如地址和电话号码 具有 Django 身份验证的外键User 像这样的东西 from django db import models from django contrib au
  • 如何在Python中检查元组是否包含元素?

    我试图找到可用的方法 但找不到 没有contains 我应该使用index 我只想知道该项目是否存在 不需要它的索引 You use in if element in thetuple whatever you want to do
  • 保存 Jupyter Notebook,并显示 Plotly Express 小部件

    我有一个 Jupyter 笔记本 python 我使用plotlyexpress 在笔记本中绘图以进行分析 我想与非编码人员共享此笔记本 并让交互式视觉效果仍然可用 但它似乎不起作用 我尝试以下此处提出的建议 https community
  • 交响二阶颂歌

    我有一个简单的二阶 ODE 的齐次解 当我尝试使用 Sympy 求解初始值时 它返回相同的解 它应该替代 y 0 和 y 0 并产生一个没有常数的解 但事实并非如此 这是建立方程的代码 它是一个弹簧平衡方程 k 弹簧常数 m 质量 我在其他

随机推荐