Skip to content
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

Added code to handle non-image uploads in runpod.serverless.utils.rp_upload.files(...) #160

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 42 additions & 26 deletions runpod/serverless/utils/rp_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
from typing import Optional, Tuple

import boto3
from PIL import Image
from PIL import Image, UnidentifiedImageError
from boto3 import session
from boto3.s3.transfer import TransferConfig
from botocore.config import Config
from tqdm_loggable.auto import tqdm



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything else looks good, I almost feel bad asking for the one change to get rid of this extra blank line :)

logger = logging.getLogger("runpod upload utility")
FMT = "%(filename)-20s:%(lineno)-4d %(asctime)s %(message)s"
logging.basicConfig(level=logging.INFO, format=FMT, handlers=[logging.StreamHandler()])
Expand Down Expand Up @@ -96,10 +97,11 @@ def get_boto_client(
# ---------------------------------------------------------------------------- #
def upload_image(job_id, image_location, result_index=0, results_list=None): # pragma: no cover
'''
Upload image to bucket storage.
Upload a single file to bucket storage.
'''
image_name = str(uuid.uuid4())[:8]
boto_client, _ = get_boto_client()
file_extension = os.path.splitext(image_location)[1]

if boto_client is None:
# Save the output to a file
Expand All @@ -108,39 +110,53 @@ def upload_image(job_id, image_location, result_index=0, results_list=None): # p
print("https://github.com/runpod/runpod-python/blob/main/docs/serverless/worker-utils.md")

os.makedirs("simulated_uploaded", exist_ok=True)
sim_upload_location = f"simulated_uploaded/{image_name}.png"
with Image.open(image_location) as img, open(sim_upload_location, "wb") as file_output:
img.save(file_output, format=img.format)
sim_upload_location = f"simulated_uploaded/{image_name}{file_extension}"
try:
with Image.open(image_location) as img, open(sim_upload_location, "wb") as file_output:
img.save(file_output, format=img.format)

except UnidentifiedImageError:
# If the file is not an image, save it directly
shutil.copy(image_location, sim_upload_location)

if results_list is not None:
results_list[result_index] = sim_upload_location

return sim_upload_location

with Image.open(image_location) as img:
output = BytesIO()
img.save(output, format=img.format)
output.seek(0)

bucket = time.strftime('%m-%y')
boto_client.put_object(
Bucket=f'{bucket}',
Key=f'{job_id}/{image_name}.png',
Body=output.getvalue(),
ContentType="image/png"
)
try:
with Image.open(image_location) as img:
output = BytesIO()
img.save(output, format=img.format)
output.seek(0)
content_type = "image/" + file_extension.lstrip(".")

except UnidentifiedImageError:
# If the file is not an image, read it directly
with open(image_location, "rb") as f:
output = f.read()
content_type = "application/octet-stream"


bucket = time.strftime('%m-%y')
boto_client.put_object(
Bucket=f'{bucket}',
Key=f'{job_id}/{image_name}{file_extension}',
Body=output,
ContentType=content_type
)

presigned_url = boto_client.generate_presigned_url(
'get_object',
Params={
'Bucket': f'{bucket}',
'Key': f'{job_id}/{image_name}.png'
}, ExpiresIn=604800)
presigned_url = boto_client.generate_presigned_url(
'get_object',
Params={
'Bucket': f'{bucket}',
'Key': f'{job_id}/{image_name}{file_extension}'
}, ExpiresIn=604800)

if results_list is not None:
results_list[result_index] = presigned_url
if results_list is not None:
results_list[result_index] = presigned_url

return presigned_url
return presigned_url


# ---------------------------------------------------------------------------- #
Expand Down