c# – 使用AWS SDK for .NET或REST API从Amazon S3检索带有图像
我正在尝试使用AWS SDK for .NET从Amazon s3中检索一些
HTML文件.我能够获取
HTML文件,但链接到网页的图像没有显示,也没有应用相关的样式表.现在,我明白为什么会这样.因为每个图像和样式表都是Amazon s3中的单独对象,我的代码只为HTML文件创建预签名URL:
private void GetWebUrl() { var request = new GetPreSignedUrlRequest().WithBucketName(bucketName) .WithKey("test/content.htm"); request.WithExpires(DateTime.Now.Add(new TimeSpan(0,50))); var url = S3.GetPreSignedURL(request); Iframe2.Attributes.Add("src",url); } 访问与此HTML文件相关的图像和样式表的最佳方法是什么?我可以查找所有图像,然后使用上面的方法生成预先签名的URL请求,但这不是一种有效的方法,我无法公开图像和样式表.有没有其他人遇到过类似的问题? 解决方法
实现此目的的最佳方法是使用通用处理程序(.ASHX).诀窍是将网页和相关对象的源更改为您的处理程序:
src:"StreamFile.ashx?file="ObjKey" 现在,要更改源代码,您可以更新旧的HTML文件,并使用指向(StreamFile.ashx)Generic Handler的源创建新的HTML文件,或使用URL重写将旧URL写入新URL.这可以在IIS或web.config中完成.如果您在IIS中执行此操作,它将自动在您的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> 上面的代码将在Src字符串中查找“DevelopmentContent / Course /”,如果找到则会将URL重写为StreamFile.ashx /?file = course {R:1}. R:1将是URL粗体部分的其余部分(DevelopmentContent / Course / xyz / xsd / x / sd / ds.htm),它应该映射到amazon S3中的对象键.在StreamHandler.ashx中现在将接收来自具有指定URL的服务器.然后,您可以从查询字符串(context.Request.QueryString [“file”])获取对象键,然后创建一个函数来获取所需的对象. 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,buffer.Length)) > 0) { context.Response.OutputStream.Write(buffer,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; } 所以现在所有的HTTP请求都将使用您的服务器作为代理. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 浅谈C#手机号换成111XXXX1111 这种显示的解决思路
- extern "C"
- React Doc阅读笔记一 — Why React ? && Displayin
- ruby-on-rails – 相当于foo_ids的find_each?
- ruby-on-rails – 如何调用ActiveRecord :: Base的load-met
- Oracle数据库中表触发的多模式权限
- c – 为什么我在这个简单的代码中得到“ld:warning:在_ma
- C# socket通信实现两个控制台之间聊天
- postgresql 检查点优化
- Oracle SQL GROUP BY“不是GROUP BY表达式”的帮助