5

I'm using Amazon S3 for hosting images of users. Although I set all of them to be available for public (it means I could access images by their URLs in Chrome/Firefox/IE), I get a 403 Forbidden exception in my code. It's a thumbnail handler (C#, ASP.NET)

string file = context.Request.QueryString[IMG_PARAM].Trim().ToLower().Replace("\\", "/");

      if (file.StartsWith("http") || file.StartsWith("https"))
      {
          using (System.Drawing.Image im = System.Drawing.Image.FromStream(WebRequest.Create(file).GetResponse().GetResponseStream()))
          using (System.Drawing.Image tn = this.CreateThumbnail(im))
          {
              tn.Save(context.Response.OutputStream, this._formatType);
          }
      }

The exception comes from this part:

WebRequest.Create(file).GetResponse().GetResponseStream()

It's really strange to me, If there's something wrong with ACL or permissions, why I can see images with browser?

starball
  • 20,030
  • 7
  • 43
  • 238

3 Answers3

3

Found it! After long research, I found it's because of URL strings which are CASE SENSITIVE. Turns out I had a .toLower() somewhere:

string file = context.Request.QueryString[IMG_PARAM].Trim().ToLower().Replace("\\", "/");

After removing .toLower() the issue will be solved.

2

I found you will also get this error if you access your Cloudfront CDN resources using a CNAME, but dont add those CNAMES to the Distribution Settings in the AWS Management Console.

EG:

cdn.yourdomain.com/js/your_script.js
rather than:
abc12345.cloudfront.net/js/your_script.js
Sage
  • 4,769
  • 1
  • 21
  • 28
0

I had this same issue. It can also happen if you do not specify the root file option when configuring your Cloudfront distribution.

When I set my root file to index.html, everything worked beautifully.

SimplGy
  • 20,079
  • 15
  • 107
  • 144