【发布时间】:2021-06-22 22:58:21
【问题描述】:
我的应用程序使用 Django REST 框架发布到 Instagram。发布到 Instagram 是一个两步过程。首先,您必须将内容发送到媒体容器。然后你必须等到 Instagram 处理完照片/视频。处理完媒体后,您将创建 ID 发送到 Instagram 以公开发布帖子。为了保证 Instagram 在发送创建 ID 之前有足够的时间处理媒体,我使用 Django 的 time.sleep 函数将请求暂停 60 秒。问题是这适用于我的桌面,但在我的 ec2 实例上,当我暂停请求超过 10 秒时,相同的代码会失败。为了运行我的应用程序,我使用了 amazon EC2、Gunicorn 和 nginx。
我的 Django 代码是这样的
```#print('posting photo to facebook')
video_url = data.get('video_url')
container_url = "https://graph.facebook.com/" + page_id + "/media?media_type=VIDEO&\
video_url=" + video_url + "&caption=" + text + "&access_token=" + access_token
res = requests.post(container_url)
data = res.json()
time.sleep(60)
publish_url = "https://graph.facebook.com/" + page_id + "/media_publish?\
creation_id=" + data['id'] + "&access_token=" + access_token
res = requests.post(publish_url)
data = res.json()
print('result of video posting attempt')
print(json.dumps(data));
a = res.status_code
b = 200
#print(res.text)
if a != b:
return Response(data = {"data": {"id":"" , "post_id":"" }})
return Response(data = data, status = res.status_code)```
我的 gunicorn.service 文件
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/xxxxx-xxxxxx-xxxxxx
ExecStart=/home/ubuntu/xxxxx-xxxxxx-xxxxxx/xxxxxxx/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
xxxxxxxxxx.wsgi:application
TimeoutStopSec=120
[Install]
WantedBy=multi-user.target
我的 nginx 配置文件
listen 80;
listen [::]:80;
server_name xxxxxxxxx.com *.xxxxxxxxx.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name xxxxxxxxx.com *.xxxxxxxxx.com;
#SSL/TLS settings
ssl_certificate /etc/letsencrypt/live/xxxxxxxxx.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxxxxxxxx.com/privkey.pem;
client_max_body_size 100M;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
location = /favicon.ico {
access_log off;
log_not_found off;
}
location /static/ {
root /home/ubuntu/xxxxxxxxx-xxxxxxxxx-xxxxxxxxx;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}```
【问题讨论】:
标签: django nginx amazon-ec2 django-rest-framework gunicorn