add content type for s3

This commit is contained in:
bakatrouble 2021-03-17 13:05:54 +03:00
parent 178310d844
commit ffc9c42370
3 changed files with 20 additions and 9 deletions

View File

@ -38,6 +38,7 @@ twisted = {extras = ["http2", "tls"], version = "*"}
whitenoise = "*" whitenoise = "*"
django-picklefield = "*" django-picklefield = "*"
boto3 = "*" boto3 = "*"
filetype = "*"
[dev-packages] [dev-packages]

24
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "edd3a9ebec36929c8ff036bf21938fb3ac5d955cc5cbed716f1fa9c34eca854e" "sha256": "b349892ba15b7d9c0f4923076c76c40bb526ceadfd2a1a7073bcb945f75786c9"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -85,19 +85,19 @@
}, },
"boto3": { "boto3": {
"hashes": [ "hashes": [
"sha256:6758751f1181b9363e4e7559dcbd5ac0fc7147b73f429c976ec5ecd1688c9ec7", "sha256:31396fcaffdfc0f465b4de76e1ecae53894eb869b000ffa54aa1693a98db3a3c",
"sha256:fa41987f9f71368013767306d9522b627946a01b4843938a26fb19cc8adb06c0" "sha256:bf8ced7760f9ef36964ccdba719f334a2ae6ec8a7719c259b198fec56fabad87"
], ],
"index": "pypi", "index": "pypi",
"version": "==1.17.27" "version": "==1.17.29"
}, },
"botocore": { "botocore": {
"hashes": [ "hashes": [
"sha256:4477803f07649f4d80b17d054820e7a09bb2cb0792d0decc2812108bc3759c4a", "sha256:184b7d26b0669fd65ac8193662376d1267d800e005b69412b57cc82c5ffbe935",
"sha256:57e45c9d443163da7312cae61bcc60382e6d0b3aecda68e850d6438162fe7b5b" "sha256:5a26ad20662f8e15685889a17c62a2f8804bf02fcdea59f45ad993b261f9caa2"
], ],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'", "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'",
"version": "==1.20.27" "version": "==1.20.29"
}, },
"celery": { "celery": {
"hashes": [ "hashes": [
@ -333,6 +333,14 @@
"index": "pypi", "index": "pypi",
"version": "==6.0.2" "version": "==6.0.2"
}, },
"filetype": {
"hashes": [
"sha256:353369948bb1c09b8b3ea3d78390b5586e9399bff9aab894a1dff954e31a66f6",
"sha256:da393ece8d98b47edf2dd5a85a2c8733e44b769e32c71af4cd96ed8d38d96aa7"
],
"index": "pypi",
"version": "==1.0.7"
},
"future": { "future": {
"hashes": [ "hashes": [
"sha256:b1bead90b70cf6ec3f0710ae53a525360fa360d306a86583adc6bf83a4db537d" "sha256:b1bead90b70cf6ec3f0710ae53a525360fa360d306a86583adc6bf83a4db537d"
@ -956,7 +964,7 @@
"sha256:2f4da4594db7e1e110a944bb1b551fdf4e6c136ad42e4234131391e21eb5b0df", "sha256:2f4da4594db7e1e110a944bb1b551fdf4e6c136ad42e4234131391e21eb5b0df",
"sha256:e7b021f7241115872f92f43c6508082facffbd1c048e3c6e2bb9c2a157e28937" "sha256:e7b021f7241115872f92f43c6508082facffbd1c048e3c6e2bb9c2a157e28937"
], ],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4.0'", "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'",
"version": "==1.26.4" "version": "==1.26.4"
}, },
"vine": { "vine": {

View File

@ -4,6 +4,7 @@ import time
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
import boto3 import boto3
import filetype
from django.db import models from django.db import models
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton
from telegram.ext import CallbackContext, Dispatcher, MessageHandler, Filters from telegram.ext import CallbackContext, Dispatcher, MessageHandler, Filters
@ -47,8 +48,9 @@ class RobotBotModuleConfig(TelegramBotModuleConfig):
with TemporaryDirectory() as d: with TemporaryDirectory() as d:
file_path = os.path.join(d, filename) file_path = os.path.join(d, filename)
file.download(file_path) file.download(file_path)
tp = filetype.guess(file_path)
s3 = boto3.client('s3', aws_access_key_id=config.aws_access_key_id, aws_secret_access_key=config.aws_secret_access_key) s3 = boto3.client('s3', aws_access_key_id=config.aws_access_key_id, aws_secret_access_key=config.aws_secret_access_key)
s3.upload_file(file_path, config.s3_bucket, filename, ExtraArgs={'ACL': 'public-read'}) s3.upload_file(file_path, config.s3_bucket, filename, ExtraArgs={'ACL': 'public-read', 'ContentType': tp.mime})
file_url = f'https://{config.s3_bucket}.s3.amazonaws.com/{filename}' file_url = f'https://{config.s3_bucket}.s3.amazonaws.com/{filename}'
if update.message.photo: if update.message.photo:
markup.inline_keyboard = [[ markup.inline_keyboard = [[