utapi

package module
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2025 License: Unlicense Imports: 8 Imported by: 0

README

utapi-go (current as of 24.04.2025)

A thin wrapper for the UploadThing API.

Why?

There was no working UploadThing API client for Go (only outdated and broken ones).

Setup

You will need a .env file with your UploadThing API secret key:

UPLOADTHING_SECRET=sk_*************************

Usage

After adding your import statement as below, run go mod tidy to install dependencies.

package main

import (
    "github.com/IXackerr/utapi-go"
    "os"
    "fmt"
)

func main() {
    // Create API handler
    utApi, err := utapi.NewUtApi()
    if err != nil {
        fmt.Println("Error creating UploadThing API handler:", err)
        os.Exit(1)
    }

    // Example: deleting a file
    // This is the key returned by UploadThing when you upload a file
    keys := []string{"fc8d296b-20f6-4173-bfa5-5d6c32fc9f6b-geat9r.csv"}
    resp, err := utApi.DeleteFiles(keys)
    if err != nil {
        fmt.Println("Error deleting files:", err)
    } else {
        fmt.Println("Successfully deleted file(s):", resp.Success)
    }
}

More examples

See examples.md for additional usage scenarios.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UploadContentToPresignedUrl added in v1.0.2

func UploadContentToPresignedUrl(content io.Reader, size int64, presigned PresignedPostURLs) error

Upload content to presigned URL (S3 compatible POST) content - io.Reader with file data size - content size in bytes presigned - PresignedPostURLs struct from GetPresignedUploadUrl response

func UploadFileToPresignedUrl

func UploadFileToPresignedUrl(filePath string, presigned PresignedPostURLs) error

Upload file to presigned URL (S3 compatible POST) filePath - path to local file presigned - PresignedPostURLs struct from GetPresignedUploadUrl response

Types

type DeleteFilesByCustomIdsRequest added in v1.0.6

type DeleteFilesByCustomIdsRequest struct {
	CustomIds []string `json:"customIds"`
}

type DeleteFilesByKeysRequest added in v1.0.6

type DeleteFilesByKeysRequest struct {
	FileKeys []string `json:"fileKeys"`
}

1. Delete files POST /v6/deleteFiles {"fileKeys": ["key1", ...]}

type DeleteFilesResponse

type DeleteFilesResponse struct {
	Success      bool `json:"success"`
	DeletedCount int  `json:"deletedCount"`
}

type GetAppInfoResponse

type GetAppInfoResponse struct {
	AppId            string `json:"appId"`
	DefaultACL       string `json:"defaultACL"`
	AllowACLOverride bool   `json:"allowACLOverride"`
}

7. Get app info POST /v7/getAppInfo

type ListFilesFile

type ListFilesFile struct {
	Id         string  `json:"id"`
	CustomId   *string `json:"customId"`
	Key        string  `json:"key"`
	Name       string  `json:"name"`
	Status     string  `json:"status"`
	Size       int64   `json:"size"`
	UploadedAt int64   `json:"uploadedAt"`
}

type ListFilesRequest

type ListFilesRequest struct {
	Limit  int `json:"limit"`
	Offset int `json:"offset"`
}

3. List files POST /v6/listFiles {"limit": 100, "offset": 0}

type ListFilesResponse

type ListFilesResponse struct {
	HasMore bool            `json:"hasMore"`
	Files   []ListFilesFile `json:"files"`
}

type PresignedPostURLs

type PresignedPostURLs struct {
	Key                string            `json:"key"`
	FileName           string            `json:"fileName"`
	FileType           string            `json:"fileType"`
	FileUrl            string            `json:"fileUrl"`
	ContentDisposition string            `json:"contentDisposition"`
	PollingJwt         string            `json:"pollingJwt"`
	PollingUrl         string            `json:"pollingUrl"`
	CustomId           *string           `json:"customId"`
	Url                string            `json:"url"`
	Fields             map[string]string `json:"fields"`
}

type RenameFileUpdate

type RenameFileUpdate struct {
	FileKey string `json:"fileKey"`
	NewName string `json:"newName"`
}

4. Rename files POST /v6/renameFiles {"updates": [{"fileKey": "...", "newName": "..."}]}

type RenameFilesRequest

type RenameFilesRequest struct {
	Updates []RenameFileUpdate `json:"updates"`
}

type RenameFilesResponse

type RenameFilesResponse struct {
	Success      bool `json:"success"`
	RenamedCount int  `json:"renamedCount"`
}

type RequestFileAccessRequest

type RequestFileAccessRequest struct {
	FileKey   string `json:"fileKey"`
	ExpiresIn int    `json:"expiresIn,omitempty"`
}

6. Get presigned URL for a private file POST /v6/requestFileAccess {"fileKey": "...", "expiresIn": 3600}

type RequestFileAccessResponse

type RequestFileAccessResponse struct {
	UfsUrl string `json:"ufsUrl"`
	Url    string `json:"url"` // deprecated
}

type UploadFileInfo

type UploadFileInfo struct {
	Name     string  `json:"name"`
	Size     int64   `json:"size"`
	Type     string  `json:"type"`
	CustomId *string `json:"customId,omitempty"`
}

8. Get presigned URL for file upload (without file router) POST /v6/uploadFiles {"files": [{"name": "file.txt", "size": 123, "type": "text/plain"}], "acl": "public-read"}

type UploadFilesRequest

type UploadFilesRequest struct {
	Files              []UploadFileInfo `json:"files"`
	ACL                string           `json:"acl"` // "public-read" or "private"
	Metadata           interface{}      `json:"metadata,omitempty"`
	ContentDisposition string           `json:"contentDisposition,omitempty"`
}

type UploadFilesResponse

type UploadFilesResponse struct {
	Data []PresignedPostURLs `json:"data"`
}

type UsageInfoResponse

type UsageInfoResponse struct {
	TotalBytes    int64 `json:"totalBytes"`
	AppTotalBytes int64 `json:"appTotalBytes"`
	FilesUploaded int   `json:"filesUploaded"`
	LimitBytes    int64 `json:"limitBytes"`
}

5. Get usage info POST /v6/getUsageInfo

type UtApi

type UtApi struct {
	// contains filtered or unexported fields
}

Client structure httpClient can be overridden for tests config stores the key and version fePackage/beAdapter - optional headers for analytics (can be left empty)

func NewUtApi

func NewUtApi() (*UtApi, error)

Client constructor

func (*UtApi) DeleteFilesByCustomIds added in v1.0.3

func (ut *UtApi) DeleteFilesByCustomIds(customIds []string) (*DeleteFilesResponse, error)

func (*UtApi) DeleteFilesByKeys added in v1.0.3

func (ut *UtApi) DeleteFilesByKeys(fileKeys []string) (*DeleteFilesResponse, error)

func (*UtApi) GetAppInfo

func (ut *UtApi) GetAppInfo() (*GetAppInfoResponse, error)

func (*UtApi) GetPresignedUploadUrl

func (ut *UtApi) GetPresignedUploadUrl(files []UploadFileInfo, acl string) (*UploadFilesResponse, error)

Get presigned URL for file upload

func (*UtApi) GetPresignedUrl

func (ut *UtApi) GetPresignedUrl(fileKey string, expiresIn int) (string, error)

func (*UtApi) GetUsageInfo

func (ut *UtApi) GetUsageInfo() (*UsageInfoResponse, error)

func (*UtApi) ListFiles

func (ut *UtApi) ListFiles(limit, offset int) (*ListFilesResponse, error)

func (*UtApi) RenameFiles

func (ut *UtApi) RenameFiles(updates []RenameFileUpdate) (*RenameFilesResponse, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL