pingdotgg / uploadthing

File uploads for modern web devs
https://uploadthing.com
MIT License
4.26k stars 315 forks source link

[misc]: UploadThing REST API Question/Problem #996

Closed camenduru closed 1 month ago

camenduru commented 1 month ago

I understand that this issue may be closed if it should be filed in another category

My issue

image

Hi I am new 👋 It is stuck. Do I need to call https://api.uploadthing.com/v6/completeMultipart? If yes, where am I supposed to get the fileKey, uploadId, etags?

payload = {
    "fileKey": "",
    "uploadId": "",
    "etags": [
        {
            "tag": "",
            "partNumber": 1
        }
    ]
}

Regenerate

import requests
import os
import mimetypes

def upload_image(file_path, api_key):
    file_name = os.path.basename(file_path)
    file_size = os.path.getsize(file_path)
    file_type, _ = mimetypes.guess_type(file_path)
    payload = {
        "files": [
            {
                "name": file_name,
                "size": file_size,
                "type": file_type
            }
        ],
        "acl": "public-read",
        "contentDisposition": "inline"
    }
    headers = {
        "Content-Type": "application/json",
        "X-Uploadthing-Api-Key": api_key
    }
    response = requests.post("https://api.uploadthing.com/v6/uploadFiles", json=payload, headers=headers)
    return response

response = upload_image("/content/test_image.png", "sk_live_34d57c4")
response.json()

response.json()

{'data': [{'url': 'https://uploadthing-prod-sea1.s3.us-west-2.amazonaws.com/',
   'fields': {'acl': 'public-read',
    'Content-Type': 'image/png',
    'Content-Disposition': 'inline; filename="test_image.png"; filename*=UTF-8\'\'test_image.png',
    'bucket': 'uploadthing-prod-sea1',
    'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
    'X-Amz-Credential': 'AKIA4BVCOYKVWQXJFN4V/20241005/us-west-2/s3/aws4_request',
    'X-Amz-Date': '20241005T033040Z',
    'key': '7dae05ca-d6af-4121-8abc-4d8905882b9d-y320ry.png',
    'Policy': 'eyJleHBpcmF0aW9uIjoiMjAyNC0xMC0wNVQwMzozNTo0MFoiLCJjb25kaXRpb25zIjpbWyJlcSIsIiRhY2wiLCJwdWJsaWMtcmVhZCJdLFsiZXEiLCIkQ29udGVudC1UeXBlIiwiaW1hZ2UvcG5nIl0sWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsMCw2NzY5MDJdLFsiZXEiLCIkQ29udGVudC1EaXNwb3NpdGlvbiIsImlubGluZTsgZmlsZW5hbWU9XCJ0ZXN0X2ltYWdlLnBuZ1wiOyBmaWxlbmFtZSo9VVRGLTgnJ3Rlc3RfaW1hZ2UucG5nIl0seyJhY2wiOiJwdWJsaWMtcmVhZCJ9LHsiQ29udGVudC1UeXBlIjoiaW1hZ2UvcG5nIn0seyJDb250ZW50LURpc3Bvc2l0aW9uIjoiaW5saW5lOyBmaWxlbmFtZT1cInRlc3RfaW1hZ2UucG5nXCI7IGZpbGVuYW1lKj1VVEYtOCcndGVzdF9pbWFnZS5wbmcifSx7ImJ1Y2tldCI6InVwbG9hZHRoaW5nLXByb2Qtc2VhMSJ9LHsiWC1BbXotQWxnb3JpdGhtIjoiQVdTNC1ITUFDLVNIQTI1NiJ9LHsiWC1BbXotQ3JlZGVudGlhbCI6IkFLSUE0QlZDT1lLVldRWEpGTjRWLzIwMjQxMDA1L3VzLXdlc3QtMi9zMy9hd3M0X3JlcXVlc3QifSx7IlgtQW16LURhdGUiOiIyMDI0MTAwNVQwMzMwNDBaIn0seyJrZXkiOiI3ZGFlMDVjYS1kNmFmLTQxMjEtOGFiYy00ZDg5MDU4ODJiOWQteTMyMHJ5LnBuZyJ9XX0=',
    'X-Amz-Signature': 'cb085844b7172fcff6e065ba072435fc4546aafa3bc4cc4b2a916520d2dfc753'},
   'key': '7dae05ca-d6af-4121-8abc-4d8905882b9d-y320ry.png',
   'contentDisposition': 'inline',
   'fileUrl': 'https://utfs.io/f/7dae05ca-d6af-4121-8abc-4d8905882b9d-y320ry.png',
   'appUrl': 'https://utfs.io/a/x77ycemiwr/7dae05ca-d6af-4121-8abc-4d8905882b9d-y320ry.png',
   'fileName': 'test_image.png',
   'pollingUrl': 'https://iubunr2q4qqhmlmm7bkp34az5a0ponok.lambda-url.us-west-2.on.aws/v6/uploadFiles/api/pollUpload/7dae05ca-d6af-4121-8abc-4d8905882b9d-y320ry.png',
   'pollingJwt': '',
   'fileType': 'image/png',
   'customId': None}]}
nativenotifydev commented 1 month ago

here when i upload my file using axios i get the response but when i open the fileurl the file say Your file is currently being uploaded...

This page will automatically refresh once it's done.

camenduru commented 1 month ago

This function is working.

def upload_file_to_uploadthing(file_path):
    file_name = os.path.basename(file_path)
    _, file_extension = os.path.splitext(file_name)
    random_string = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(8))
    md5_hash = hashlib.md5(random_string.encode()).hexdigest()
    file_name = md5_hash+file_extension
    file_size = os.path.getsize(file_path)
    file_type, _ = mimetypes.guess_type(file_path)
    with open(file_path, "rb") as file:
        file_content = file.read()
    file_info = {"name": file_name, "size": file_size, "type": file_type}
    uploadthing_api_key = os.getenv('uploadthing_api_key')
    headers = {"x-uploadthing-api-key": uploadthing_api_key}
    data = {"contentDisposition": "inline", "acl": "public-read", "files": [file_info]}
    presigned_response = requests.post("https://api.uploadthing.com/v6/uploadFiles", headers=headers, json=data)
    presigned_response.raise_for_status()
    presigned = presigned_response.json()["data"][0]
    upload_url = presigned["url"]
    fields = presigned["fields"]
    files = {"file": file_content}
    upload_response = requests.post(upload_url, data=fields, files=files)
    upload_response.raise_for_status()
    return presigned_response, upload_response, file_name
nativenotifydev commented 1 month ago

hello could you help me

nativenotifydev commented 1 month ago

const data = { files: files.map((file) => ({ name: file.file.name, size: file.file.size, type: file.file.type, })), acl: 'public-read', contentDisposition: 'attachment', }; const options = { method: 'POST', url: 'https://api.uploadthing.com/v6/uploadFiles', headers: { 'Content-Type': 'application/json', 'X-Uploadthing-Api-Key': process.env.NEXT_PUBLIC_UPLOADTHING_SECRET }, data: data };

    i am using axios 
    and the fileurl i get wont open it say it is uploading 
nativenotifydev commented 1 month ago

hello which url is used to access the files