- Back to Docs
- Install and Configure
- Image
- Server Upload
- Server Delete
- S3 Upload
- Resize
- Validation
- Image Manager
- Simple Server
- File
- Server Upload
- Server Delete
- S3 Upload
- Validation
- References
- Image
- File
- S3
PHP File S3 Upload
How it works
- You create a bucket on Amazon S3 and set the CORS for it.
- Your code computes the Amazon S3 signature on server side.
- The javascript editor is initialized with the
fileUploadToS3
option. - When an file is uploaded, it is being sent directly to the S3 bucket without touching your server.
Create a S3 Bucket
Amazon explains in detail on the URL below how to create a bucket and set a region for it. If you have any troubles with creating it, please contact Amazon for getting it set up.
http://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html
Set CORS on the S3 Bucket
Cross-origin resource sharing (CORS) mainly tells Amazon from which domains to accept requests and what kind of requests. A detailed explanation of how that works can be find in Amazon Documentation.
We recommend to add a configuration like the one below where you would replace the ALLOWED_URL
with the URL of the page where you're using the javascript editor.
<?xml version="1.0" encoding="UTF-8"?> <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <CORSRule> <AllowedOrigin>ALLOWED_URL</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> <AllowedMethod>POST</AllowedMethod> <AllowedMethod>PUT</AllowedMethod> <MaxAgeSeconds>3000</MaxAgeSeconds> <AllowedHeader>*</AllowedHeader> </CORSRule> </CORSConfiguration>
Compute Signature
In order to get the file uploaded to S3, it is necessary to compute a signature using the AWS access key ID
and AWS secret access key
and provide it together with the upload request. The rich text editor PHP SDK comes with methods to compute the S3 signature using the V4 signing algorithm that works with buckets created on any of the S3 regions.
// Configuration object. $config = array( // Your server timezone. // Read more on: http://php.net/manual/en/timezones.php. 'timezone' => 'Europe/Bucharest', // The name of your bucket. 'bucket' => 'bucket-name', // S3 region. If you are using the default us-east-1, it this can be ignored. 'region' => 'eu-west-1', // The folder where to upload the files. 'keyStart' => 'uploads', // File access. 'acl' => 'public-read', // AWS keys. 'accessKey' => YOUR_AWS_ACCESS_KEY, 'secretKey' => YOUR_AWS_SECRET_KEY ); // Compute the signature. $s3Hash = FroalaEditor_S3::getHash($config);
Initialize the javascript editor
To get files uploaded to Amazon S3, the fileUploadToS3
option should be set on initialization. The editor PHP SDK computes the hash required for this as the response of the FroalaEditor_S3::getHash
method.
<script> $(function() { $('.selector').froalaEditor({ fileUploadToS3: JSON.parse('<?php echo $s3Hash; ?>') }) }); </script>
Complete Example
<?xml version="1.0" encoding="UTF-8"?> <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <CORSRule> <AllowedOrigin>ALLOWED_URL</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> <AllowedMethod>POST</AllowedMethod> <AllowedMethod>PUT</AllowedMethod> <MaxAgeSeconds>3000</MaxAgeSeconds> <AllowedHeader>*</AllowedHeader> </CORSRule> </CORSConfiguration>
<?php // Configuration object. $config = array( // Your server timezone. 'timezone' => 'Europe/Bucharest', // The name of your bucket. 'bucket' => 'bucket-name', // S3 region. If you are using the default us-east-1, it this can be ignored. // Read more on: http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region. 'region' => 'eu-west-1', // The folder where to upload the files. 'keyStart' => 'uploads', // File access. 'acl' => 'public-read', // AWS keys. 'accessKey' => YOUR_AWS_ACCESS_KEY, 'secretKey' => YOUR_AWS_SECRET_KEY ); // Compute the signature. $s3Hash = FroalaEditor_S3::getHash($config); ?> <script> $(function() { $('.selector').froalaEditor({ fileUploadToS3: JSON.parse('<?php echo $s3Hash; ?>') }) }); </script>