-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/image upload #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b76b20e
Started on image upload support
m-messer 36e95aa
Switched to put
m-messer 97373a4
Fixed issue with request not sending file name and updated tests
m-messer cdf120c
Switched to auto parsing of mime_type
m-messer fe5df2d
Implemented auth for uploading to S3
m-messer e011409
Added session token
m-messer b375d16
Added passing of folder name
m-messer f104066
Fixed inconsistent casing for BMP mime type in image upload
m-messer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,179 @@ | ||
| import hashlib | ||
|
|
||
| import requests | ||
| import uuid | ||
| import os | ||
| from io import BytesIO | ||
| from typing import Dict, List, Optional | ||
| from PIL import Image | ||
| from dotenv import load_dotenv | ||
|
|
||
| from botocore.auth import SigV4Auth | ||
| from botocore.awsrequest import AWSRequest | ||
| from botocore.credentials import Credentials | ||
|
|
||
| load_dotenv() | ||
|
|
||
| MIME_TO_FORMAT: Dict[str, List[str]] = { | ||
| 'image/jpeg': ['JPEG', 'JPG'], | ||
| 'image/png': ['PNG'], | ||
| 'image/gif': ['GIF'], | ||
| 'image/bmp': ['BMP'], | ||
| } | ||
|
|
||
| FORMAT_TO_MIME: Dict[str, str] = { | ||
| 'JPEG': 'image/jpeg', | ||
| 'JPG': 'image/jpeg', | ||
| 'PNG': 'image/png', | ||
| 'GIF': 'image/gif', | ||
| "BMP": 'image/bmp' | ||
| } | ||
|
|
||
| class ImageUploadError(Exception): | ||
| """Custom exception for image upload failures""" | ||
| pass | ||
|
|
||
|
|
||
| class InvalidMimeTypeError(ImageUploadError): | ||
| """Exception for invalid MIME type""" | ||
| pass | ||
|
|
||
|
|
||
| class MissingEnvironmentVariableError(ImageUploadError): | ||
| """Exception for missing environment variables""" | ||
| pass | ||
|
|
||
|
|
||
| def generate_file_name(img: Image.Image) -> str: | ||
| """Generate filename for the image | ||
|
|
||
| Args: | ||
| img: PIL Image object | ||
|
|
||
| Returns: | ||
| Generated filename string | ||
| """ | ||
| unique_id: str = str(uuid.uuid4()) | ||
| format_ext: str = img.format.lower() if img.format else 'png' | ||
| return f"{unique_id}.{format_ext}" | ||
|
|
||
| def get_s3_bucket_uri() -> str: | ||
| """Get S3 bucket URI from environment variable""" | ||
| s3_uri: Optional[str] = os.getenv('S3_BUCKET_URI') | ||
|
|
||
| if not s3_uri: | ||
| raise MissingEnvironmentVariableError( | ||
| "S3_BUCKET_URI environment variable is not set" | ||
| ) | ||
|
|
||
| return s3_uri | ||
|
|
||
|
|
||
| def get_aws_signed_request(full_url, buffer, mime_type): | ||
| credentials = Credentials( | ||
| access_key=os.environ['AWS_ACCESS_KEY_ID'], | ||
| secret_key=os.environ['AWS_SECRET_ACCESS_KEY'], | ||
| token=os.environ.get('AWS_SESSION_TOKEN', None) | ||
| ) | ||
|
|
||
| if hasattr(buffer, 'read'): | ||
| # It's a file-like object (BytesIO, etc.) | ||
| current_pos = buffer.tell() # Save current position | ||
| buffer.seek(0) # Go to start | ||
| data = buffer.read() # Read all data | ||
| buffer.seek(current_pos) # Restore position | ||
| else: | ||
| # It's already bytes | ||
| data = buffer | ||
|
|
||
| # Calculate content hash and length | ||
| content_hash = hashlib.sha256(data).hexdigest() | ||
| content_length = len(data) | ||
|
|
||
| # Create the request for signing with required headers | ||
| headers = { | ||
| 'Content-Type': mime_type, | ||
| 'Content-Length': str(content_length), | ||
| 'x-amz-content-sha256': content_hash | ||
| } | ||
|
|
||
| # Create the request for signing | ||
| aws_request = AWSRequest( | ||
| method='PUT', | ||
| url=full_url, | ||
| data=buffer, | ||
| headers=headers | ||
| ) | ||
|
|
||
| region = os.environ.get('AWS_REGION', 'eu-west-2') | ||
|
|
||
| # Sign the request | ||
| SigV4Auth(credentials, 's3', region).add_auth(aws_request) | ||
|
|
||
| return aws_request | ||
|
|
||
|
|
||
| def upload_image(img: Image.Image, folder_name: str) -> str: | ||
| """Upload PIL image with comprehensive MIME type validation | ||
|
|
||
| Args: | ||
| folder_name: name of folder to save image | ||
| img: PIL Image object to upload | ||
|
|
||
| Returns: | ||
| JSON response from the server as a dictionary | ||
|
|
||
| Raises: | ||
| InvalidMimeTypeError: If MIME type validation fails | ||
| MissingEnvironmentVariableError: If S3_BUCKET_URI is not set | ||
| ImageUploadError: If upload fails for any reason | ||
| """ | ||
| try: | ||
| # Get URL from environment variable | ||
| base_url: str = get_s3_bucket_uri() | ||
|
|
||
| filename: str = generate_file_name(img) | ||
|
|
||
| full_url = os.path.join(base_url, folder_name, filename) | ||
|
|
||
| if img.format is None: | ||
| img.format = 'PNG' | ||
|
|
||
| mime_type = FORMAT_TO_MIME[img.format.upper()] | ||
|
|
||
| buffer: BytesIO = BytesIO() | ||
| img_format: str = img.format if img.format else 'PNG' | ||
| img.save(buffer, format=img_format) | ||
| buffer.seek(0) | ||
|
|
||
| aws_request = get_aws_signed_request(full_url, buffer, mime_type).prepare() | ||
|
|
||
| response: requests.Response = requests.request( | ||
| method=aws_request.method, | ||
| url=aws_request.url, | ||
| data=aws_request.body, | ||
| headers=aws_request.headers, | ||
| timeout=30 | ||
| ) | ||
|
|
||
| if response.status_code != 200: | ||
| raise ImageUploadError( | ||
| f"Upload failed with status code {response.status_code}: {response.text}" | ||
| ) | ||
|
|
||
| return full_url | ||
|
|
||
| except (InvalidMimeTypeError, MissingEnvironmentVariableError): | ||
| raise | ||
| except requests.exceptions.RequestException as e: | ||
| raise ImageUploadError(f"Network error: {str(e)}") | ||
| except Exception as e: | ||
| raise ImageUploadError(f"Unexpected error: {str(e)}") | ||
|
|
||
| if __name__ == "__main__": | ||
| img = Image.new('RGB', (100, 100), color='red') | ||
| img.format = 'JPEG' | ||
|
|
||
| # Execute | ||
| result = upload_image(img, "eduvision") | ||
| print(result) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.