【问题标题】:How to use Ionic proxy in conjunction with AWS SDK如何将 Ionic 代理与 AWS 开发工具包结合使用
【发布时间】:2017-11-28 05:21:35
【问题描述】:

使用 Ionic 4.4.0 和 aws-sdk 2.157.0。我正在尝试从本地 Web 浏览器创建 S3 存储桶,但在尝试运行以下代码时遇到了 CORS 问题,方法 createBucketByCompanyKey():

import { Injectable } from '@angular/core';
import * as AWS from 'aws-sdk';

@Injectable()
export class AwsProvider {

  private accessKeyId:string = 'myAccessKey';
  private secretAccessKey:string = 'mySuperSecret';
  private region:string = 'us-east-1';

  constructor() {
    AWS.config.update({accessKeyId: this.accessKeyId, secretAccessKey: this.secretAccessKey, region: this.region});
  }

  createBucketByCompanyKey(companyKey){

    let s3 = new AWS.S3();
    let params = {
      Bucket: companyKey,
      CreateBucketConfiguration: {
        LocationConstraint: this.region
      }
    };
    s3.createBucket(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });
  }
}

这给了我错误

无法加载https://s3.amazonaws.com/-KwzdjmyrHiMBCqHH1ZC:响应 预检请求未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:8100” 使用权。响应的 HTTP 状态代码为 403。

经过几个小时的谷歌搜索,我找到了this post here。看来我需要通过代理运行离子。我也尝试将我的“路径”更改为http://localhost:8100,但我仍然坚持。

{
  "name": "MyApp",
  "app_id": "",
  "type": "ionic-angular",
  "integrations": {},
  "proxies": [
    {
      "path": "/",
      "proxyUrl": "https://s3.amazonaws.com/"
    }
  ]
}

我也遇到过告诉我 download a Chrome extension 禁用 CORS 的帖子,但这也不起作用。

关于如何设置此代理以使用 AWS 的 SDK 的任何想法?

【问题讨论】:

    标签: ionic-framework aws-sdk


    【解决方案1】:

    忘记代理。对于 Mac,在终端中输入以下内容以打开禁用 CORS 的 Google Chrome 浏览器。

    open -a Google\ Chrome --args --disable-web-security --user-data-dir

    this post的赞美。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 2016-12-15
      • 2016-02-08
      • 2019-01-02
      • 2018-01-19
      • 2021-07-08
      • 1970-01-01
      • 2012-09-21
      相关资源
      最近更新 更多