简体   繁体   English

使用AWS-SDK-JS访问AWS ELB服务时配置CORS

[英]Configure CORS when accessing AWS ELB service using AWS-SDK-JS

I'm working on a ELB monitoring application that I'd like to build with JS and host directly in S3. 我正在研究一个ELB监控应用程序,我想用JS构建并直接在S3中托管。 I have no experience of JS and I'm struggling to get to grips with CORS. 我没有JS的经验,我很难掌握CORS。

I have successfully created a build of the SDK for my browser ( http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-building.html ) which includes ELB API support. 我已经成功为我的浏览器创建了一个SDK版本( http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-building.html ),其中包括ELB API支持。

I have integrated Amazon login ( http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-configuring-wif.html ) with my script and I have attached it to an IAM WebIdentity role. 我已将Amazon登录( http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-configuring-wif.html )与我的脚本集成,并且我已将其附加到IAM WebIdentity角色。

When I call 我打电话的时候

elb.describeInstanceHealth(elbRequestParams).on('success', describeInstanceHealthCallback).send();

I get an error in Javascript console 我在Javascript控制台中收到错误

XMLHttpRequest cannot load 'elasticloadbalancing.eu-west-1.amazonaws.com/'. The request was redirected to 'aws.amazon.com/elasticloadbalancing', which is disallowed for cross-origin requests that require preflight.

I can't find any clear documentation that what I'm trying to do won't work, but, I'll admit I'm confused by some of the terms in the documentation and I have no experience of CORS in previous applications to fall back on. 我找不到任何明确的文档,我正在尝试做的事情不会起作用,但是,我承认我对文档中的一些术语感到困惑,而且我在之前的应用程序中没有使用CORS的经验。重新开始。 I would think the whole JS-SDK for the browser is a bit redundant if the majority of services aren't CORS aware and need to be. 我认为如果大多数服务不是CORS意识到并且需要,那么浏览器的整个JS-SDK有点多余。

I can get this working by disabling web-security in my Chrome browser, obviously this isn't a good workaround but would indicate to me that CORS is the issue. 我可以通过在我的Chrome浏览器中禁用网络安全性来实现这一点,显然这不是一个好的解决方法,但会告诉我CORS是问题。

Is anyone familiar with this approach, is it a problem that I'm hosting on S3, or, would I have this problem from any server? 是否有人熟悉这种方法,这是我在S3上托管的问题,或者,我是否会从任何服务器出现此问题? Should I be configuring my S3 bucket with CORS, or, is it that there's no CORS policy on the 'elasticloadbalancing.eu-west-1.amazonaws.com' endpoint? 我应该使用CORS配置我的S3存储桶,还是在'elasticloadbalancing.eu-west-1.amazonaws.com'端点上没有CORS策略?

Thanks 谢谢

Andrew 安德鲁

I have successfully created a build of the SDK for my browser ( http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-building.html ) which includes ELB API support. 我已经成功为我的浏览器创建了一个SDK版本( http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-building.html ),其中包括ELB API支持。

The referenced page already provides the first clue towards the lack of CORS support for elasticloadbalancing.eu-west-1.amazonaws.com you correctly suspect to be the cause here: 引用的页面已经提供了第一个线索,因为缺少对elasticloadbalancing.eu-west-1.amazonaws.com的CORS支持,你正确怀疑它是这里的原因:

If you are working with the SDK outside of an environment that enforces CORS in your browser and want access to the full gamut of services provided by the AWS SDK for JavaScript , it is possible to build a custom copy of the SDK locally by cloning the repository and running the same build tools used to generate the default hosted version of the SDK. 如果您在浏览器中强制执行CORS的环境之外使用SDK并希望访问AWS SDK for JavaScript提供的全部服务,则可以通过克隆存储库在本地构建SDK的自定义副本并运行用于生成SDK的默认托管版本的相同构建工具。

That is, the main reason for the official AWS SDK for JavaScript in the Browser excluding all but the currently 5 services listed in section Supported Services within Working with Services in the Browser is their lack of CORS support: 也就是说, 浏览器中官方AWS SDK for JavaScript的主要原因除了浏览器中 使用服务中 支持的服务部分中列出的当前5种服务之外的所有服务都缺乏CORS支持:

It is possible to use the SDK with other services if CORS security checking is disabled in your environment. 如果在您的环境中禁用CORS安全检查,则可以将SDK与其他服务一起使用。 In this case, you can build your own custom version of the SDK. 在这种情况下,您可以构建自己的SDK自定义版本。 See the Building the SDK section of the guide for more information on how to create a custom build of the SDK. 有关如何创建SDK的自定义版本的更多信息,请参阅本指南的构建SDK部分。

Unfortunately neither Amazon EC2 nor Elastic Load Balancing currently offers CORS support, see eg this recent Feature Request (CORS support for EC2 service) , where the author rightfully reaches the same conclusion as you did already (while also hinting on Node.js to be another major use case, which supports all available AWS services , see Working with Services in Node.js ): 不幸的是, Amazon EC2Elastic Load Balancing目前都没有提供CORS支持,例如参见最近的功能请求(对EC2服务的CORS支持) ,其中作者正确地得出了与您已经相同的结论(同时也暗示Node.js是另一个)主要用例, 支持所有可用的AWS服务 ,请参阅使用Node.js中的服务 ):

From my point of view if AWS is providing a aws-sdk-js library all AWS services supported by this library should support CORS so that the aws-sdk-js iibrary is not just useable in a node.js environment but also in a browser. 从我的观点来看,如果AWS提供了一个aws-sdk-js库,那么这个库支持的所有AWS服务都应该支持CORS,这样aws-sdk-js的iibrary不仅可以在node.js环境中使用,而且可以在浏览器中使用。

While I agree in principle and would have expected faster turnaround times on this myself, AWS is well known for an agile approach to product development, ie start early with a small feature set and improve over time based on customer feedback. 虽然我原则上同意并希望自己有更快的周转时间,但AWS以敏捷的产品开发方法而闻名,即以较小的功能集开始提前,并根据客户反馈随时间改进。 Notably the SDK is labeled Developer Preview only, ie not even BETA yet and I'd hope this to be addressed in the future accordingly - admittedly this is easily overlooked and a more prominent warning might go a long way in sparing users the time to figure out this limitation by themselves. 值得注意的是,SDK仅被标记为开发人员预览 ,即使还没有BETA ,我希望将来可以相应地解决这个问题 - 不可否认,这很容易被忽视,更加突出的警告可能会在很长一段时间内帮助用户节省时间。自己出这个限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将aws-sdk-js与CognitoSync服务一起使用时出现InvalidSignatureException - InvalidSignatureException while using aws-sdk-js with CognitoSync service 使用 AWS-SDK-JS 使用普通 Javascript 将大文件作为流上传到 s3 - Upload large files as a stream to s3 with Plain Javascript using AWS-SDK-JS 如何使用 aws-sdk-js 列出日期范围内的对象? - How to list objects in a date range with aws-sdk-js? 如何将 aws-sdk-js 捆绑到无服务器框架优化包中? - How do you bundle aws-sdk-js into a Serverless Framework optimized package? 使用AWS-SDK-JS通过CloudFront分发进行S3分段上传 - S3 Multipart upload via cloudfront distribution with aws-sdk-js aws-sdk-js ReferenceError:您正在尝试在 Jest 环境被拆除后“导入”文件 - aws-sdk-js ReferenceError: You are trying to `import` a file after the Jest environment has been torn down 使用angular + aws-sdk-js +预签名网址将文件上传到S3 - Uploading a file to S3 with angular + aws-sdk-js + pre-signed url aws-sdk-js DynamoDB引发错误:Request.VALIDATE_REGION的配置中缺少凭据 - aws-sdk-js DynamoDB throwing Error: Missing credentials in config at Request.VALIDATE_REGION aws-sdk-js E2 createVPC 在等待添加 promise 时执行了两次 - aws-sdk-js E2 createVPC is executed twice while waiting adding a promise 在AWS IAM JS SDK中获取``CORS标头丢失''错误 - Getting 'CORS header missing' error in AWS IAM JS SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM