简体   繁体   中英

Retrieving HTML files with images and style sheets from Amazon S3 using AWS SDK for .NET or REST API

I am trying to retrieve some HTML files from Amazon s3 using AWS SDK for .NET. I am able to get the HTML file but the images that are linked to the webpage are not being displayed neither is the relevant style sheet applied. Now, I do understand why this is happening. Because each image and style sheet is a separate object in Amazon s3 and my code is only creating presigned URL for the HTML file:

 private void GetWebUrl()
{
        var request =
                new GetPreSignedUrlRequest().WithBucketName(bucketName)
                  .WithKey("test/content.htm");
            request.WithExpires(DateTime.Now.Add(new TimeSpan(0, 0, 0, 50)));
            var url = S3.GetPreSignedURL(request);
            Iframe2.Attributes.Add("src", url);
}

What is the best way to access the images and style sheet related to this HTML file? I can look for all the images and then use the above method to generate presigned URL requests but that is not an efficient method and I can't make the images and style sheet public. Has anyone else encountered a similar issue? Also, is it better if I use Rest API to authenticate user( using authentication Header) so that the browser will have authentication information in header and I will not have to create presigned URL's for each object. A small piece of code for REST API would be very helpful.

The best way to achieve this is by using Generic Handlers(.ASHX). The trick is to change the source of webpage and related objects to your handler:

src:"StreamFile.ashx?file="ObjKey"

Now, to change the source you either update your old HTML files and create new ones with source pointing to (StreamFile.ashx)Generic Handler or use URL rewrite to write old URL's to new URL. This can be done in IIS or in the web.config.If you do it in IIS it will automatically add code in your web.config.

<system.webServer>
    <rewrite>
      <rules>
        <rule name="Content">
          <match url="DevelopmentContent/Course/([a-zA-Z0-9]+)" />
          <action type="Rewrite" url="StreamFile.ashx/?file=course{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

The above code will look for "DevelopmentContent/Course/" in Src string and if found will rewrite the URL to StreamFile.ashx/?file=course{R:1}. R:1 will the rest of the URL-bold part(DevelopmentContent/Course/ ) which should map to your object key in amazon S3.Now in StreamHandler.ashx will receive the requests from the server with specified URL. ),它应该映射到amazon S3中的对象键。在StreamHandler.ashx中现在将接收来自具有指定URL的服务器。 You can then get the object key from query string(context.Request.QueryString["file"]) and then create a function to Get the required object.

public void ProcessRequest(HttpContext context)
        {           
            var response = Gets3Response(context.Request.QueryString["file"]);
            if (response != null)
            {
                using (response)
                {
                    var mimEtype = response.ContentType;
                    context.Response.ContentType = mimEtype;
                    using (var responseStream = response.ResponseStream)
                    {
                        var buffer = new byte[8000];
                        var bytesRead = -1;
                        while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            context.Response.OutputStream.Write(buffer, 0, bytesRead);
                        }
                    }
                    context.Response.Flush();
                    context.Response.End();
                }
            }
            else
            {

                context.Response.Write("Unable to retrieve content!");

            }
        }




        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    private static GetObjectResponse Gets3Response(string fileName)
        {
            GetObjectResponse response;
            if (fileName.Trim().Length == 0)
            {
                return null;
            }
            try
            {
                var request = new GetObjectRequest();
                request.WithBucketName(BucketName).WithKey(fileName);
                response = AmazonS3ClientProvider.CreateS3Client().GetObject(request);
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&      (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                }
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }

            return response;
        }

So now all the HTTP requests will be made using your server as proxy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM