-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #31 fix: modify migrations * #9 fix : modify selector * Merge branch '31/fix/server_api' of https://github.com/chaechaeis/UPCY_BE into 31/fix/server_api * #23 feat : upload photo in s3 socket --------- Co-authored-by: 밍 <[email protected]>
- Loading branch information
1 parent
d975a66
commit 87ad1b2
Showing
9 changed files
with
160 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
포트폴리오, 룩북, 서비스 모두 사용할 수 있도록 공통 함수로 따로 제작. | ||
services>services.py>ServicePhotoApi 참고해서 작성하면 됨 | ||
""" | ||
|
||
import io | ||
import boto3 | ||
import uuid | ||
from django.conf import settings | ||
from UpcyProject import settings | ||
from datetime import datetime | ||
|
||
|
||
def get_random_text(length): | ||
return str(uuid.uuid4()).replace('-', '')[:length] | ||
|
||
def s3_file_upload_by_file_data(upload_file, region_name, bucket_name, bucket_path, content_type=None): | ||
bucket_name = bucket_name.replace('/', '') | ||
# 파일 확장자 추출 | ||
if content_type: | ||
content_type = content_type | ||
else: | ||
content_type = upload_file.content_type | ||
|
||
now = datetime.now() | ||
random_str = get_random_text(20) | ||
extension = upload_file.name.split('.')[-1] | ||
|
||
random_file_name = '{}.{}'.format(str(now.strftime('%Y%m%d%H%M%S'))+random_str,extension) | ||
# 파일의 ContentType을 설정 | ||
random_file_name = f"{now.strftime('%Y%m%d%H%M%S')}_{random_str}.{extension}" | ||
upload_file_path_name = f"{bucket_path}/{random_file_name}" | ||
|
||
s3 = boto3.resource('s3', region_name=region_name, aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY) | ||
|
||
try: | ||
# 버킷이 존재하는지 확인 | ||
s3.meta.client.head_bucket(Bucket=bucket_name) | ||
except s3.meta.client.exceptions.NoSuchBucket: | ||
raise Exception(f"The specified bucket does not exist: {bucket_name}") | ||
|
||
file_data = io.BytesIO(upload_file.read()) | ||
|
||
try: | ||
# 파일을 S3에 업로드 | ||
s3.Bucket(bucket_name).put_object(Key=upload_file_path_name, Body=file_data, ContentType=content_type, ACL='public-read') | ||
print(extension) | ||
return f"https://{bucket_name}.s3.{region_name}.amazonaws.com/{upload_file_path_name}" | ||
except Exception as e: | ||
print(f"Failed to upload file to S3: {e}") | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters