Laravel + S3 How to save user-defined metadata when saving
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
This article summarizes how to add arbitrary parameters as metadata when saving images from Laravel to S3.
Environment
"league/flysystem-aws-s3-v3": "~1.0",
Conclusion
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
method to send parameters in the third argument.
At that time,
$params = [
'visibility' => 'public',
'Metadata' => [
'x-amz-meta-post_id' => '1'
]
];
Specify with the prefix x-amz-meta-
.
This seems to be a set phrase as etiquette.
When trying to manually enter from the management screen,
It will look like this.
Also, it is written like this in the official documentation.
In conclusion
It is nothing major if you know it, but it took me a few hours to find the correct answer, so I hope it will be helpful.
Next time, I will use this metadata in Lambda to save to RDS (whether to post an article is uncertain).