django - 亚马逊 S3 + docker - "403 Forbidden: The difference between the request time and the current time is too large"

标签 django ubuntu amazon-web-services amazon-s3 docker

我正在尝试在 docker 容器中运行我的 django 应用程序,其中包含从 Amazon S3 提供的静态文件。当我在我的 Dockerfile 中运行 RUN $(which python3.4)/home/docker/code/vitru/manage.py collectstatic --noinput 时,我从 Amazon S3 收到一个 403 Forbidden 错误,其中包含以下内容响应 XML

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>RequestTimeTooSkewed</Code>
    <Message>The difference between the request time and the current time is too large.</Message>
    <RequestTime>Sat, 27 Dec 2014 11:47:05 GMT</RequestTime>
    <ServerTime>2014-12-28T08:45:09Z</ServerTime>
    <MaxAllowedSkewMilliseconds>900000</MaxAllowedSkewMilliseconds>
    <RequestId>4189D5DAF2FA6649</RequestId>
    <HostId>lBAhbNfeV4C7lHdjLwcTpVVH2snd/BW18hsZEQFkxqfgrmdD5pgAJJbAP6ULArRo</HostId>
</Error>

我的 docker 容器正在运行 Ubuntu 14.04... 如果这有什么不同的话。 我还使用 uWSGI 运行应用程序,没有使用 nginx 或 apache 或任何其他类型的反向代理服务器。

当文件被提供给网站时,我也在运行时遇到错误。

尝试的解决方案

其他 stackoverflow 问题报告了使用 S3 的类似错误(没有专门与 Docker 一起使用),他们说这个错误是由于系统时钟不同步引起的,可以通过运行来修复

sudo service ntp stop
sudo ntpd -gq
sudo service ntp start

所以我将以下内容添加到我的 Dockerfile 中,但它并没有解决问题。

RUN apt-get install -y ntp
RUN ntpd -gq
RUN service ntp start

在构建 docker 镜像之前,我还尝试使用 sudo ntpd -gq 同步我本地机器上的时间,但这也没有用。

Dockerfile

FROM ubuntu:14.04

# Get most recent apt-get
RUN apt-get -y update

# Install python and other tools
RUN apt-get install -y tar git curl nano wget dialog net-tools build-essential
RUN apt-get install -y python3 python3-dev python-distribute
RUN apt-get install -y nginx supervisor
# Get Python3 version of pip
RUN apt-get -y install python3-setuptools
RUN easy_install3 pip

# Update system clock so S3 does not get 403 Error
# NOT WORKING
#RUN apt-get install -y ntp
#RUN ntpd -gq
#RUN service ntp start

RUN pip install uwsgi
RUN apt-get -y install libxml2-dev libxslt1-dev 

RUN apt-get install -y python-software-properties uwsgi-plugin-python3

# Install GEOS
RUN apt-get -y install binutils libproj-dev gdal-bin

# Install node.js
RUN apt-get install -y nodejs npm

# Install postgresql dependencies 
RUN apt-get update && \
    apt-get install -y postgresql libpq-dev && \
    rm -rf /var/lib/apt/lists

# Install pylibmc dependencies
RUN apt-get update
RUN apt-get install -y libmemcached-dev zlib1g-dev libssl-dev

ADD . /home/docker/code

# Setup config files
RUN ln -s /home/docker/code/supervisor-app.conf /etc/supervisor/conf.d/

RUN pip install -r /home/docker/code/vitru/requirements.txt


# Create directory for logs
RUN mkdir -p /var/logs

# Set environment as staging
ENV env staging

# Run django commands
# python3.4 is at /usr/bin/python3.4, but which works too
RUN $(which python3.4) /home/docker/code/vitru/manage.py collectstatic --noinput
RUN $(which python3.4) /home/docker/code/vitru/manage.py syncdb --noinput
RUN $(which python3.4) /home/docker/code/vitru/manage.py makemigrations --noinput
RUN $(which python3.4) /home/docker/code/vitru/manage.py migrate --noinput


EXPOSE 8000

CMD ["supervisord", "-c", "/home/docker/code/supervisor-app.conf"]

最佳答案

在评论中指出,但对于来到这里的其他人:

如果使用 boot2docker(即如果在 Windows 或 Mac 上),boot2docker 虚拟机在您休眠计算机时会出现已知时间问题——请参阅 here .由于您的 docker 容器的主机是 boot2docker 虚拟机,这就是它同步时间的地方。

我已成功重启 boot2docker 虚拟机。这可能会导致丢失某些状态的问题,即如果您有一些数据量。

关于django - 亚马逊 S3 + docker - "403 Forbidden: The difference between the request time and the current time is too large",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27674968/

相关文章:

django - 在 django admin 中启用按 __str__ 列排序

django - django 基于日期的自动电子邮件通知

shell - 记录 Shell 输出

amazon-web-services - 是否可以在 AWS Glue 中重命名作业?

django - 我需要什么样的模型和字段来处理诸如Apple的Autorenewable订阅之类的订阅?

javascript - Ajax 错误处理 Django 响应

python - 在 ubuntu 树莓派中自动刷新图像

ubuntu - 在不使用 ppa-purge 的情况下删除 ubuntu 中的 ppa

amazon-web-services - 我的 AWS CLI 配置有什么问题?

java - 将 AWS SageMaker 机器学习模型转换为 Java 库