i want to host my files on aws s3 , files are being uploaded by client browser
i used to stream the files from my backend to s3 but there was lots of problem with large files
so i decided to create a pre signed upload url to directly upload the file from front
fornt will send the file to back end , back end will store the file information in database and creates a pre signed upload url and sends it back to the front
front will upload the file directly in s3 and notifies the back end , here is the first step / gathering information about the file / generating upload url / storing data in database
$request->validate([
'attachment'=>'required|string' ,
]);
$original_name = $request->input('attachment');
$newName = 'some-versioning-info-'.$original_name ;
$s3Upload = // store file data in database
list($client , $bucket ) = $this->getS3Client();
$cmd = $client->getCommand('PutObject', [
'Bucket' => $bucket,
'Key' => $newName
]);
$request = $client->createPresignedRequest($cmd, '+20 minutes')->withMethod('PUT');
$url = (string)$request->getUri();
return response( ['url'=>$url , 'token' => $s3Upload->token );
front will upload the file with given url , and then will call this function to very upload and update the database
function verifyUpload($token){
$s3Upload = S3UploadUrl::where('token' , $token )->firstOrFail();
if(!Storage::disk('s3')->exists($s3Upload->file_name))
throw new ApiException("file not found!");
$size = Storage::disk('s3')->size($s3Upload->file_name);
if($size < 1 )
throw new ApiException("something is wrong with the file");
// all ok , store in database
}
the problem is after upload is done , s3 only returns a boll value
even if frond doesnt send a file , and just send a empty PUT request to s3 it will return the same true value ... basically there is no way to check if the stored file is the same one that has been uploaded by client it could be some empty binary ... some additional info would have been nice
so my only option is to check the files size on back end in the verification function
$size = Storage::disk('s3')->size($s3Upload->file_name);
if($size < 1 )
throw new ApiException("something is wrong with the file");
i was wondering if there is better way to check if the file has been compeltely uploaded and it's the same one as the uploaded ?