How to Attach User-Defined Metadata When Uploading to S3 with Laravel
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
A guide on how to add custom parameters as metadata when saving files from Laravel to S3.
Summary
use Storage;
class TestController extends Controller
{
public function store(Request $request)
{
$disk = Storage::disk('s3');
$params = [
'visibility' => 'public',
'Metadata' => [
'x-amz-meta-post_id' => '1'
]
];
$path = $disk->put('test', $request->file('movie'), $params);
return response()->json(['url' => $path], 200);
}
}
Use put
to send parameters as the third argument.
In this part:
$params = [
'visibility' => 'public',
'Metadata' => [
'x-amz-meta-post_id' => '1'
]
];
x-amz-meta-
Ensure the prefix starts with this format, as it’s the convention.
If you try to input this manually from the management console, it will look like this:
The official documentation also describes this process.
This covers the main topic; the following section is additional context.
Why Metadata Was Necessary
In this system, there is a process where user-uploaded video files are transcribed using AWS Transcribe.
However, to accurately link the transcription results to the corresponding video files, a unique ID was required.
Since a typical file upload cannot retain this information, I implemented a method to attach the ID as metadata to the file.
- Upload the video with Laravel
- Generate an audio file from the video on the server using FFMpeg
- Save the audio file to S3
- Trigger Lambda upon saving to S3
- Use AWS Transcribe in Lambda to save a transcription file to S3
- Trigger Lambda again when the transcription file is saved
- In Lambda, save the output file path to the database and link it to the video file's ID, among other data
I'm not sure if this is the best solution, so if you know of a better method, feel free to leave a comment on any YouTube video or contact us directly.
Conclusion
It’s straightforward once you know it, but it took a few hours to find the right solution. I hope this is helpful.
I hope this helps someone.