|  | 
| 36 | 36 | # ---------------------------------------------------------------------------- # | 
| 37 | 37 | #                                 Upload Image                                 # | 
| 38 | 38 | # ---------------------------------------------------------------------------- # | 
| 39 |  | -def upload_image(job_id, job_results): | 
|  | 39 | +def upload_image(job_id, job_result, result_index=0): | 
| 40 | 40 |     ''' | 
| 41 | 41 |     Upload image to bucket storage. | 
| 42 | 42 |     ''' | 
| 43 | 43 |     if boto_client is None: | 
| 44 | 44 |         # Save the output to a file | 
| 45 |  | -        for index, result in enumerate(job_results): | 
| 46 |  | -            output = BytesIO() | 
| 47 |  | -            img = Image.open(result) | 
| 48 |  | -            img.save(output, format=img.format) | 
| 49 |  | - | 
| 50 |  | -            os.makedirs("uploaded", exist_ok=True) | 
| 51 |  | -            with open(f"uploaded/{index}.png", "wb") as file_output: | 
| 52 |  | -                file_output.write(output.getvalue()) | 
| 53 |  | - | 
| 54 |  | -        return [] | 
| 55 |  | - | 
| 56 |  | -    object_urls = [] | 
| 57 |  | -    for index, result in enumerate(job_results): | 
| 58 | 45 |         output = BytesIO() | 
| 59 |  | -        img = Image.open(result) | 
|  | 46 | +        img = Image.open(job_result) | 
| 60 | 47 |         img.save(output, format=img.format) | 
| 61 | 48 | 
 | 
| 62 |  | -        bucket = time.strftime('%m-%y') | 
|  | 49 | +        os.makedirs("uploaded", exist_ok=True) | 
|  | 50 | +        with open(f"uploaded/{result_index}.png", "wb") as file_output: | 
|  | 51 | +            file_output.write(output.getvalue()) | 
| 63 | 52 | 
 | 
| 64 |  | -        # Upload to S3 | 
| 65 |  | -        boto_client.put_object( | 
| 66 |  | -            Bucket=f'{bucket}', | 
| 67 |  | -            Key=f'{job_id}/{index}.png', | 
| 68 |  | -            Body=output.getvalue(), | 
| 69 |  | -            ContentType="image/png" | 
| 70 |  | -        ) | 
|  | 53 | +        return None | 
| 71 | 54 | 
 | 
| 72 |  | -        output.close() | 
|  | 55 | +    output = BytesIO() | 
|  | 56 | +    img = Image.open(job_result) | 
|  | 57 | +    img.save(output, format=img.format) | 
|  | 58 | + | 
|  | 59 | +    bucket = time.strftime('%m-%y') | 
|  | 60 | + | 
|  | 61 | +    # Upload to S3 | 
|  | 62 | +    boto_client.put_object( | 
|  | 63 | +        Bucket=f'{bucket}', | 
|  | 64 | +        Key=f'{job_id}/{result_index}.png', | 
|  | 65 | +        Body=output.getvalue(), | 
|  | 66 | +        ContentType="image/png" | 
|  | 67 | +    ) | 
| 73 | 68 | 
 | 
| 74 |  | -        presigned_url = boto_client.generate_presigned_url( | 
| 75 |  | -            'get_object', | 
| 76 |  | -            Params={ | 
| 77 |  | -                'Bucket': f'{bucket}', | 
| 78 |  | -                'Key': f'{job_id}/{index}.png' | 
| 79 |  | -            }, ExpiresIn=604800) | 
|  | 69 | +    output.close() | 
| 80 | 70 | 
 | 
| 81 |  | -        object_urls.append(presigned_url) | 
|  | 71 | +    presigned_url = boto_client.generate_presigned_url( | 
|  | 72 | +        'get_object', | 
|  | 73 | +        Params={ | 
|  | 74 | +            'Bucket': f'{bucket}', | 
|  | 75 | +            'Key': f'{job_id}/{result_index}.png' | 
|  | 76 | +        }, ExpiresIn=604800) | 
| 82 | 77 | 
 | 
| 83 |  | -        log(f"Presigned URL generated: {presigned_url}") | 
|  | 78 | +    log(f"Presigned URL generated: {presigned_url}") | 
| 84 | 79 | 
 | 
| 85 |  | -    return object_urls | 
|  | 80 | +    return presigned_url | 
0 commit comments