FittyAI File Uploading Guide

Upload files to FittyAI’s file storage using pre-signed URLs. This guide will walk you through fetching the pre-signed URL and using it to upload a file to the designated storage.

Requirements

  • Python with the requests library installed.
  • An authentication token to interact with FittyAI’s API Gateway.

Code Explanation

1. Fetching the Pre-signed URL

The get_presigned_url function fetches the pre-signed URL required to securely upload your file to FittyAI’s storage.

import requests

def get_presigned_url(api_gateway_url, object_key, token):
    headers = {
        "authorization": f"Bearer {token}"
    }
    body = {
        "fileName": object_key
    }
    response = requests.post(api_gateway_url, headers=headers, json=body)
    response_data = response.json()
    return response_data['upload_url'], response_data['access_url']

2. Uploading File to S3

Using the pre-signed URL, the upload_to_s3 function allows direct file upload to the designated S3 bucket.

def upload_to_s3(presigned_url, filepath):
    with open(filepath, 'rb') as file:
        file_data = file.read()
    response = requests.put(presigned_url, data=file_data)
    return response.status_code

3. Execution

This section sets your configurations, calls the above functions in sequence, and handles the file upload response.

def main():
    API_GATEWAY_URL = "https://38en04sov6.execute-api.us-west-2.amazonaws.com/v1/fitty/files/uploadurl"
    FILE_TO_UPLOAD = "hope.png"
    OBJECT_KEY = f"images/{FILE_TO_UPLOAD}"
    SECRET_TOKEN = "SECRET_TOKEN"

    signed_url, access_url = get_presigned_url(API_GATEWAY_URL, OBJECT_KEY, SECRET_TOKEN)
    status = upload_to_s3(signed_url, FILE_TO_UPLOAD)

    if status == 200:
        print("File uploaded successfully!")
    else:
        print(f"File upload failed with status code {status}.")

Execution

To run the script, ensure you have the necessary prerequisites and execute it in a Python environment.