简体   繁体   English

如何将aws访问和密钥与aws sqs sdk v3一起用于angular?

[英]How to use aws access and secret keys with aws sqs sdk v3 for angular?

I'm trying to add sqs in my Angular app so my back end can send notification to my front end.我正在尝试在我的 Angular 应用程序中添加 sqs,以便我的后端可以向我的前端发送通知。 I found this example, but they don't explain how to login.我找到了这个例子,但他们没有解释如何登录。

The repo with the source is here: https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascriptv3/example_code/sqs/src/sqs_receivemessage.ts带有源代码的仓库在这里: https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascriptv3/example_code/sqs/src/sqs_receivemessage.ts

I installed also the sdk like this npm i @aws-sdk/client-sqs but in the npm repo they don't explain how to configure the authent.我还安装了 sdk 像npm i @aws-sdk/client-sqs但在 npm 存储库中,他们没有解释如何配置 authent。

How could i do?我该怎么办?

/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
which is available at https://github.com/aws/aws-sdk-js-v3. This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at
https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sqs-examples-send-receive-messages.html.
Purpose:
sqs_receivemessage.ts demonstrates how to receive and delete a message from an Amazon SQS queue.
Inputs (replace in code):
- REGION
- SQS_QUEUE_URl
Running the code:
ts-node sqs_receivemessage.js
 */
// snippet-start:[sqs.JavaScript.messages.receiveMessageV3]

// Import required AWS SDK clients and commands for Node.js
const {
  SQSClient,
  ReceiveMessageCommand,
  DeleteMessageCommand,
} = require("@aws-sdk/client-sqs");

// Set the AWS Region
const REGION = "REGION"; //e.g. "us-east-1"

// Set the parameters
const queueURL = "SQS_QUEUE_URL"; //SQS_QUEUE_URL; e.g., 'https://sqs.REGION.amazonaws.com/ACCOUNT-ID/QUEUE-NAME'
const params = {
  AttributeNames: ["SentTimestamp"],
  MaxNumberOfMessages: 10,
  MessageAttributeNames: ["All"],
  QueueUrl: queueURL,
  VisibilityTimeout: 20,
  WaitTimeSeconds: 0,
};

// Create SQS service object
const sqs = new SQSClient({ region: REGION });

const run = async () => {
  try {
    const data = await sqs.send(new ReceiveMessageCommand(params));
    if (data.Messages) {
      var deleteParams = {
        QueueUrl: queueURL,
        ReceiptHandle: data.Messages[0].ReceiptHandle,
      };
      try {
        const data = await sqs.send(new DeleteMessageCommand(deleteParams));
        console.log("Message deleted", data);
      } catch (err) {
        console.log("Error", err);
      }
    } else {
      console.log("No messages to delete");
    }
  } catch (err) {
    console.log("Receive Error", err);
  }
};
run();
// snippet-end:[sqs.JavaScript.messages.receiveMessageV3]

EDIT I added in the file "C:\Users\koste\.aws\config"编辑我在文件"C:\Users\koste\.aws\config"中添加

[default]
aws_access_key_id=AKIA4LUIXXXXXY3SE3RO
aws_secret_access_key=yJKM96KISxxxxxxxxxxxxxOhajncWXe0YXo
region = eu-west-3

[sqs-dev]
aws_access_key_id=AKIAXXXXXXXXXXG46ZUET
aws_secret_access_key=XgGq2mbxxxxxxxLRfchaxw0FvAKVWpuXO0
region = eu-west-3

Still not working.还是行不通。 I got in the console我进入控制台

Receive Error ReferenceError: process is not defined
    loadSharedConfigFiles index.js:11
    parseKnownFiles index.js:102
    fromIni index.js:31
    memoize memoize.js:22
    step tslib.es6.js:102
    verb tslib.es6.js:83
    __awaiter tslib.es6.js:76
    ZoneAwarePromise Angular
    __awaiter tslib.es6.js:72
    memoize memoize.js:16
    signRequest SignatureV4.js:169
    step tslib.es6.js:102
    verb tslib.es6.js:83
    __awaiter tslib.es6.js:76
    ZoneAwarePromise Angular
    __awaiter tslib.es6.js:72
    signRequest SignatureV4.js:165
    sign SignatureV4.js:85
    step tslib.es6.js:102
    verb tslib.es6.js:83
    __awaiter tslib.es6.js:76
    ZoneAwarePromise Angular
    __awaiter tslib.es6.js:72
    sign SignatureV4.js:76
    awsAuthMiddleware middleware.js:31
    step tslib.es6.js:102
    verb tslib.es6.js:83
    fulfilled tslib.es6.js:73
    Angular 21
    RxJS 21
    ZoneAwarePromise Angular
    toPromise RxJS
    signIn auth.service.ts:211
    fulfilled tslib.es6.js:73
    Angular 22
    streamCollector index.js:6
    collectBody Aws_json1_1.js:3514
    collectBodyString Aws_json1_1.js:3518
    parseBody Aws_json1_1.js:3533
    deserializeAws_json1_1GetCredentialsForIdentityCommand Aws_json1_1.js:724
    step tslib.es6.js:100
    verb tslib.es6.js:81
    __awaiter tslib.es6.js:74
    ZoneAwarePromise Angular
    __awaiter tslib.es6.js:70
    deserializeAws_json1_1GetCredentialsForIdentityCommand Aws_json1_1.js:716
    deserialize GetCredentialsForIdentityCommand.js:32
    deserializerMiddleware deserializerMiddleware.js:7
    fulfilled tslib.es6.js:71
    Angular 11

EDIT 2编辑 2

I even set the env variable and still get the same issue我什至设置了环境变量,但仍然遇到同样的问题

set AWS_ACCESS_KEY_ID=AKIA4XXXXXXXXXXXXX6ZUET
set AWS_SECRET_ACCESS_KEY=XgGq2mbYQvnXXXXXXXXXXXXXXXXXXXXXXXXO0

EDIT 3 I look into the sdk and the process variable is used in the sdk but is not declared, that's why i have the error编辑 3我查看了 sdk 并且过程变量用于 sdk 但未声明,这就是我有错误的原因

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var os_1 = require("os");
var path_1 = require("path");
var fs_1 = require("fs");
exports.ENV_CREDENTIALS_PATH = "AWS_SHARED_CREDENTIALS_FILE";
exports.ENV_CONFIG_PATH = "AWS_CONFIG_FILE";
var swallowError = function () { return ({}); };
function loadSharedConfigFiles(init) {
    if (init === void 0) { init = {}; }
    var _a = init.filepath, filepath = _a === void 0 ? process.env[exports.ENV_CREDENTIALS_PATH] ||
        path_1.join(getHomeDir(), ".aws", "credentials") : _a, _b = init.configFilepath, configFilepath = _b === void 0 ? process.env[exports.ENV_CONFIG_PATH] ||
        path_1.join(getHomeDir(), ".aws", "config") : _b;

This content is part of the AWS SDK for JavaScript DEV Guide where all of these concepts, such as handling credentials, are explained.此内容是适用于 JavaScript 开发指南的 AWS SDK 的一部分,其中解释了所有这些概念,例如处理凭证。 Refer to the DEV Guide . 请参阅开发指南

If you do not know how to get up and running with the API, please refer to the Getting Started section.如果您不知道如何启动和运行 API,请参阅入门部分。 Cred specific information is located here: Cred 的具体信息位于此处:

https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/getting-your-credentials.html https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/getting-your-credentials.html

Once you are successful, then you can look at more complete examples, such the content under the Cross-service examples section.成功后,您可以查看更完整的示例,例如跨服务示例部分下的内容。

Solution is here: https://github.com/aws/aws-sdk-js-v3/issues/2386#event-4744209781解决方案在这里: https://github.com/aws/aws-sdk-js-v3/issues/2386#event-4744209781

It is not possible to use credentials file for browser.浏览器不能使用凭证文件。

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

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