Usage Example

Usage

Once you have set up the environment variables and configured the S3 bucket, you can use the useUpload hook to upload files to S3. Here's an example of how to use it:

import { useUpload } from "rebuilt-next-s3";
 
const MyComponent = () => {
  const { uploadFile, progress } = useUpload();
 
  const handleFileUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0];
    if (file) {
      const response = await uploadFile(file);
      if (response.success) {
        console.log("File uploaded successfully:", response.src);
      } else {
        console.log("Error uploading file:", response.error);
      }
    }
  };
 
  return (
    <div>
      <input type="file" onChange={handleFileUpload} />
      {progress && (
        <div>
          Upload progress: {progress.progress}% ({progress.status})
        </div>
      )}
    </div>
  );
};