【问题标题】:Static files in Amazon S3 bucket - Django collectstatic errorAmazon S3 存储桶中的静态文件 - Django collectstatic 错误
【发布时间】:2016-01-04 21:25:40
【问题描述】:

我正在尝试将我的静态文件放入我的 Amazon S3 帐户的一个存储桶中 我的配置如下:

  • 我已经安装了这些包:

pip install django-storages-redux pip install django-boto

我的settings.py 文件是:

INSTALLED_APPS = [
         ...
    'storages',
         ...
]

STATIC_URL = '/static/'


STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

#With this configuration, the static files (projects and applications) will stayed centralized in one directory
STATIC_ROOT = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2] + ['static-content'])

#Amazon S3 Storage
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
#DEFAULT_FILE_STORAGE = 'storages.backends.s3.S3Storage'
AWS_ACCESS_KEY_ID = get_env_variable('AWS_ACCESS_KEY_ID'),
AWS_SECRET_ACCESS_KEY =  get_env_variable('AWS_SECRET_ACCESS_KEY'),
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_STORAGE_BUCKET_NAME = 'neurorehabilitation-project'

当我执行collectstatic 命令时,我收到以下消息:

(nrb_dev)➜  neurorehabilitation_project git:(master) ✗ python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/__init__.py", line 342, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 176, in handle
    collected = self.collect()
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 107, in collect
    handler(path, prefixed_path, storage)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 321, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 234, in delete_file
    if self.storage.exists(prefixed_path):
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/storages/backends/s3boto.py", line 431, in exists
    k = self.bucket.new_key(self._encode_name(name))
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/storages/backends/s3boto.py", line 289, in bucket
    self._bucket = self._get_or_create_bucket(self.bucket_name)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/storages/backends/s3boto.py", line 322, in _get_or_create_bucket
    return self.connection.get_bucket(name, validate=self.auto_create_bucket)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/storages/backends/s3boto.py", line 278, in connection
    proxy_port=self.proxy_port
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/boto/s3/connection.py", line 190, in __init__
    validate_certs=validate_certs, profile_name=profile_name)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/boto/connection.py", line 569, in __init__
    host, config, self.provider, self._required_auth_capability())
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/boto/auth.py", line 977, in get_auth_handler
    ready_handlers.append(handler(host, config, provider))
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/boto/auth.py", line 134, in __init__
    HmacKeys.__init__(self, host, config, provider)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/boto/auth.py", line 71, in __init__
    self.update_provider(provider)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/boto/auth.py", line 138, in update_provider
    super(HmacAuthV1Handler, self).update_provider(provider)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/boto/auth.py", line 75, in update_provider
    self._hmac = hmac.new(self._provider.secret_key.encode('utf-8'),
AttributeError: 'tuple' object has no attribute 'encode'
(nrb_dev)➜  neurorehabilitation_project git:(master) ✗ 

将静态文件发送到 Amazon S3 时应考虑哪些项目或配置?

我正在使用 python3.4 和 Django 1.9

【问题讨论】:

    标签: python django amazon-s3 django-staticfiles


    【解决方案1】:

    从以下设置中删除尾随逗号。尾随逗号表示 Python 将它们视为元组而不是字符串。

    AWS_ACCESS_KEY_ID = get_env_variable('AWS_ACCESS_KEY_ID')
    AWS_SECRET_ACCESS_KEY =  get_env_variable('AWS_SECRET_ACCESS_KEY')
    

    【讨论】:

    • 感谢@Alasdair,我已将文件放入存储桶。 :D。我看不到我的媒体文件(用户上传)。我想我没有被告知上传它们的 Django 如何将我的媒体文件上传到 Amazon S3?在这个链接github.com/jschneier/django-storages/issues/6谈一下,我已经按照说明...
    • 最好是问一个关于上传媒体文件的新问题,而不是在 cmets 中问。
    • 我已经在另一个线程stackoverflow.com/questions/34604034/… 中写了这个问题以防万一。谢谢
    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2019-08-31
    • 2019-01-03
    • 2023-04-10
    • 2015-09-10
    • 2020-03-13
    • 2012-08-02
    相关资源
    最近更新 更多