teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードの追記

2021/12/31 08:28

投稿

alpha95
alpha95

スコア10

title CHANGED
File without changes
body CHANGED
@@ -2,18 +2,60 @@
2
2
  下記コードにおいて``s3_client.upload_file``の引数で
3
3
  ``file="static/uploads/mov_hts-samp001.mp4"``と直接パスを指定して、該当箇所に動画を置いておけば保存ができる状況です。
4
4
 
5
+ サーバー側のコードは下記のように書きました。
5
6
 
7
+
8
+ ``ルーティング``
6
9
  ```
10
+ @router.post("/", operation_id='createTrainingVideo', status_code=HTTP_201_CREATED, tags=["training_videos"])
11
+ def create_training_video(
12
+ file: UploadFile = File(...),
13
+ title: str = Form(...),
14
+ description: str = Form(...),
15
+ settings: Settings = Depends(get_settings),
16
+ db: Session = Depends(get_db)):
17
+
18
+ training_videos.create_training_video(db=db,
19
+ upload_file = file,
20
+ title=title,
21
+ description=description,
22
+ settings=settings
23
+ )
24
+
25
+ ```
26
+
27
+ ``training_videos.py``
28
+ ```
29
+ def create_training_video(db:Session,
30
+ settings: Settings,
31
+ upload_file: UploadFile,
32
+ title: str,
33
+ description: str):
34
+
35
+ content_type = upload_file.content_type.split("/")[1]
36
+ filename = datetime.utcnow().strftime("%Y%m%d%H%M%SZ-") + title + "." + content_type
37
+ aws_helper.upload_to_aws(bucket_name_str=settings.bucket_name, file="static/uploads/training_videos/mov_hts-samp001.mp4", s3_filename=filename, settings=settings)
38
+ db_training_video = TrainingVideoModel(description = description,
39
+ title = title,
40
+ video_name = filename,
41
+ created_at = datetime.utcnow())
42
+ db.add(db_training_video)
43
+ db.commit()
44
+ return Result(result=True)
45
+
46
+ ```
47
+
48
+ ``aws_helper.py``
49
+ ```
7
50
  def upload_to_aws(bucket_name_str: str, file: str, s3_filename: str, settings: Settings):
8
51
  s3_client = boto3.client('s3',
9
52
  aws_access_key_id=settings.aws_access_key,
10
53
  aws_secret_access_key=settings.aws_secret_key,
11
54
  region_name=settings.aws_region_name)
12
55
  s3_client.upload_file(file, bucket_name_str, s3_filename)
56
+ ```
13
57
 
14
58
 
15
- ```
16
-
17
59
  ですが、実際にはstatic/uploadsに動画を置いておくわけではなくて、フロントからデータを受け取ってその動画を保存するという流れになります。
18
60
 
19
61
  この場合は、直接boto3に動画を渡すのではなくて、一度サーバーに動画を保存しないといけないということでしょうか?