Files can be uploaded to the system for a variety of purposes;
Getting attached to documents
Uploading an ID photo to someones profile
Getting attached to a persons skill/qualification
To do this, files are;
Uploaded to Amazon S3 to a temporary file storage location
Then moved (internally) to their proper storage location (depending on what they’ll be used for - eg. as a document / ID photo / etc). This step is done by including the file information as part of the request to update data - eg. if you would like a photo to be set as someones ID photo, the temporary-location information is passed in with the request to update someones information.
To upload a file to this temporary storage location;
An API request must first be made to obtain a signed-url
A signed URL with some form fields are returned from your request for a signed-url
Using the signed URL & form-fields, submit your request with the file-data to Amazon S3
A PHP sample of the code required to submit the file data is included below;
composer.json
{ "require": { "guzzlehttp/guzzle": "~6.0" } }
test.php
<?php require 'vendor/autoload.php'; use GuzzleHttp\Client; $client = new Client(); $arr = json_decode('... JSON DATA GOES HERE ...', true); $postVals = []; foreach ($arr['formData'] as $key => $val) { $postVals[] = [ 'name' => $key, 'contents' => $val, ]; } $localFilePath = './myfile.png'; $fileMimeType = 'image/png'; $remoteFileName = 'myfile.png'; $multipart = array_merge( $postVals, [ [ 'name' => 'Content-Type', 'contents' => $fileMimeType, ], [ 'name' => 'file', 'contents' => fopen($localFilePath, 'r'), 'filename' => $remoteFileName, 'headers' => [ 'Content-Type' => $fileMimeType, ], ], ] ); print_r($multipart); $response = $client->request( 'POST', $arr['url'], [ 'multipart' => $multipart, ] ); print_r($response);
Running php test.php
will output the metadata which is being sent, as well as information about the response received.
A key
will be included in the multipart array (the first shown), generally starting with ‘temporary-uploads/…’
. This will represent the ‘path
’ when referring to this uploaded file in subsequent requests - eg. to attach the file to a new document, or use it as an ID photo for a person in the system.
AWS provides more information on this concept here; https://docs.aws.amazon.com/AmazonS3/latest/dev/PresignedUrlUploadObject.html