Days
Hours
Minutes
Seconds
x

Froala Editor v4.1.4 is Here LEARN MORE

Skip to content

.NET Image S3 Upload

How it works

  1. Create a bucket on Amazon S3 and set the CORS for it.
  2. Code computes the Amazon S3 signature on the server side.
  3. The editor initializes with the imageUploadToS3 option.
  4. Uploaded images go directly to the S3 bucket without passing through the server.

Jump to Complete Example

Create a S3 Bucket

For information on creating a bucket and setting a region, refer to the Amazon documentation. If you have any issues creating it, please contact Amazon for getting it set up.

Set CORS on the S3 Bucket

Cross-origin resource sharing (CORS) tells Amazon from which domains to accept requests and what kind of requests. For a detailed explanation of how that works, refer to the Amazon CORS Documentation.

The following example shows the recomended configuration, remember to replace ALLOWED_URL with the URL of the page where you are using 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

To send uploaded images 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 editor .NET 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.

FroalaEditor.S3Config config = new FroalaEditor.S3Config
{
    // 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 images.
    KeyStart = 'uploads',

    // File access.
    Acl = 'public-read',

    // AWS keys.
    AccessKey = YOUR_AWS_ACCESS_KEY,
    SecretKey = YOUR_AWS_SECRET_KEY
};

// Compute the signature.
object s3Hash = FroalaEditor.S3.GetHash(config);

Initialize the Javascript editor

To upload images to Amazon S3, set the imageUploadToS3 option on initialization. The editor .NET SDK computes the hash required for this as the response of the FroalaEditor.S3.GetHash method.

<script>
  $.get( "/FroalaApi/S3Signature", {})
  .done(function( data ) {
      $('.selector').froalaEditor({
          imageUploadToS3: data
      })
  });
</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>
using System;
using System.Web.Mvc;

namespace demo.Controllers
{
    public class FroalaApiController : Controller
    {
        public ActionResult S3Signature()
        {
            FroalaEditor.S3Config config = new FroalaEditor.S3Config
            {
                // 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 images.
                KeyStart = 'uploads',

                // File access.
                Acl = 'public-read',

                // AWS keys.
                AccessKey = YOUR_AWS_ACCESS_KEY,
                SecretKey = YOUR_AWS_SECRET_KEY
            };

            return Json(FroalaEditor.S3.GetHash(config), JsonRequestBehavior.AllowGet);
        }
    }
}
<script>
  $.get( "/FroalaApi/S3Signature", {})
  .done(function( data ) {
      $('.selector').froalaEditor({
          imageUploadToS3: data
      })
  });
</script>

Do you think we can improve this article? Let us know.

[class^="wpforms-"]
[class^="wpforms-"]
[bws_google_captcha]
<div class="gglcptch gglcptch_v2"><div id="gglcptch_recaptcha_596550210" class="gglcptch_recaptcha"></div> <noscript> <div style="width: 302px;"> <div style="width: 302px; height: 422px; position: relative;"> <div style="width: 302px; height: 422px; position: absolute;"> <iframe src="https://www.google.com/recaptcha/api/fallback?k=6Ld6lNoUAAAAAM626LfCOrnkBFJtYZAKESFCjgv_" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe> </div> </div> <div style="border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; height: 60px; width: 300px;"> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;"></textarea> </div> </div> </noscript></div>
[class^="wpforms-"]
[class^="wpforms-"]
[bws_google_captcha]
<div class="gglcptch gglcptch_v2"><div id="gglcptch_recaptcha_410025404" class="gglcptch_recaptcha"></div> <noscript> <div style="width: 302px;"> <div style="width: 302px; height: 422px; position: relative;"> <div style="width: 302px; height: 422px; position: absolute;"> <iframe src="https://www.google.com/recaptcha/api/fallback?k=6Ld6lNoUAAAAAM626LfCOrnkBFJtYZAKESFCjgv_" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe> </div> </div> <div style="border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; height: 60px; width: 300px;"> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;"></textarea> </div> </div> </noscript></div>
[class^="wpforms-"]
[class^="wpforms-"]
[bws_google_captcha]
<div class="gglcptch gglcptch_v2"><div id="gglcptch_recaptcha_409340984" class="gglcptch_recaptcha"></div> <noscript> <div style="width: 302px;"> <div style="width: 302px; height: 422px; position: relative;"> <div style="width: 302px; height: 422px; position: absolute;"> <iframe src="https://www.google.com/recaptcha/api/fallback?k=6Ld6lNoUAAAAAM626LfCOrnkBFJtYZAKESFCjgv_" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe> </div> </div> <div style="border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; height: 60px; width: 300px;"> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;"></textarea> </div> </div> </noscript></div>