Ich versuche, mein API -Gateway in AWS durch die CDK einzurichten. Ich habe es eingerichtet, aber egal was ich versuche, ich kann CORS nicht umgehen, um die Lambda -Funktion auszuführen, die die Datenbank liest. Ich benutze C#, um das zu schreiben. Hier ist der Code: < /p>
using Constructs;
using Amazon.CDK.AWS.APIGateway;
using Amazon.CDK.AWS.Lambda;
using System.Collections.Generic;
/**
* This stack creates an API Gateway REST API.
* The API has a single resource, /items, which supports GET, POST, and PUT methods.
* Each method is integrated with a Lambda function.
*/
namespace Cdk
{
public class ApiGatewayStack
{
// Need to use RestApiBase instead of RestApi for v1
public RestApiBase Api { get; }
public ApiGatewayStack(Construct scope, Function lambdaFunction)
{
Api = new RestApi(scope, "ServerlessProjApi", new RestApiProps
{
DefaultCorsPreflightOptions = new CorsOptions
{
AllowOrigins = Cors.ALL_ORIGINS, // Allow all origins
AllowMethods = Cors.ALL_METHODS, // Allow all methods (GET, POST, PUT, etc.)
AllowHeaders = new[] { "Content-Type", "X-Amz-Date", "Authorization", "X-Api-Key" } // CORS headers
}
});
SetupApiGateway(Api, lambdaFunction);
}
private void SetupApiGateway(RestApiBase api, Function lambdaFunction)
{
var lambdaIntegration = new LambdaIntegration(lambdaFunction);
var options = new MethodOptions
{
AuthorizationType = AuthorizationType.NONE,
MethodResponses = new[] {
new MethodResponse
{
StatusCode = "200",
ResponseParameters = new Dictionary
{
{ "method.response.header.Access-Control-Allow-Origin", true },
{ "method.response.header.Access-Control-Allow-Headers", true },
{ "method.response.header.Access-Control-Allow-Methods", true }
}
}
}
};
// Create /items resource
var items = api.Root.AddResource("items");
// Add methods for /items resource (GET, POST, PUT)
items.AddMethod("GET", lambdaIntegration, options); // GET /items
items.AddMethod("POST", lambdaIntegration, options); // POST /items
items.AddMethod("PUT", lambdaIntegration, options); // PUT /items
}
}
}
< /code>
Ich habe zuerst die Restapi -Klasse ausprobiert, aber diese Lösung gesehen, obwohl die Änderung keinen Unterschied machte. Ich kann über meinen Browser auf die API zugreifen, indem ich den Endpunkt eingeben, aber nicht über eine Webseite. Hier ist mein genauer Fehler: < /p>
Access to XMLHttpRequest at 'https://{apicode}.execute-api.us-east-1.amazonaws.com/prod/items' from origin 'http://{my-s3-bucket}.s3-website-us-east-1.amazonaws.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Ich versuche, mein API -Gateway in AWS durch die CDK einzurichten. Ich habe es eingerichtet, aber egal was ich versuche, ich kann CORS nicht umgehen, um die Lambda -Funktion auszuführen, die die Datenbank liest. Ich benutze C#, um das zu schreiben. Hier ist der Code: < /p> [code]using Constructs; using Amazon.CDK.AWS.APIGateway; using Amazon.CDK.AWS.Lambda; using System.Collections.Generic;
/** * This stack creates an API Gateway REST API. * The API has a single resource, /items, which supports GET, POST, and PUT methods. * Each method is integrated with a Lambda function. */
namespace Cdk { public class ApiGatewayStack { // Need to use RestApiBase instead of RestApi for v1 public RestApiBase Api { get; }
public ApiGatewayStack(Construct scope, Function lambdaFunction) { Api = new RestApi(scope, "ServerlessProjApi", new RestApiProps { DefaultCorsPreflightOptions = new CorsOptions { AllowOrigins = Cors.ALL_ORIGINS, // Allow all origins AllowMethods = Cors.ALL_METHODS, // Allow all methods (GET, POST, PUT, etc.) AllowHeaders = new[] { "Content-Type", "X-Amz-Date", "Authorization", "X-Api-Key" } // CORS headers } });
SetupApiGateway(Api, lambdaFunction); }
private void SetupApiGateway(RestApiBase api, Function lambdaFunction) { var lambdaIntegration = new LambdaIntegration(lambdaFunction); var options = new MethodOptions { AuthorizationType = AuthorizationType.NONE, MethodResponses = new[] { new MethodResponse { StatusCode = "200", ResponseParameters = new Dictionary { { "method.response.header.Access-Control-Allow-Origin", true }, { "method.response.header.Access-Control-Allow-Headers", true }, { "method.response.header.Access-Control-Allow-Methods", true } } } } };
// Create /items resource var items = api.Root.AddResource("items");
// Add methods for /items resource (GET, POST, PUT) items.AddMethod("GET", lambdaIntegration, options); // GET /items items.AddMethod("POST", lambdaIntegration, options); // POST /items items.AddMethod("PUT", lambdaIntegration, options); // PUT /items } } } < /code> Ich habe zuerst die Restapi -Klasse ausprobiert, aber diese Lösung gesehen, obwohl die Änderung keinen Unterschied machte. Ich kann über meinen Browser auf die API zugreifen, indem ich den Endpunkt eingeben, aber nicht über eine Webseite. Hier ist mein genauer Fehler: < /p> Access to XMLHttpRequest at 'https://{apicode}.execute-api.us-east-1.amazonaws.com/prod/items' from origin 'http://{my-s3-bucket}.s3-website-us-east-1.amazonaws.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. [/code] Jede Hilfe wird geschätzt!
nach Git -Klon und wenn ich:
npm i
Es zeigt den Fehler wie diesen:
npm warn EBADENGINE required: { node: '>=20.0.0 =10.0.0' },
npm warn EBADENGINE current: { node: 'v22.11.0', npm: '11.2.0' }
npm...
Ich versuche, Komponententests für meinen Python-CDK-Code zu schreiben. Dem AWS-Dokument entnehme ich, dass Vorlagen für die Level-2- und Level-1-Konstrukte einfach generiert werden können, sodass...
Um explizit zu sein, verwendet der Code die folgende Klasse: aws_cdk.aws_sso importieren cfnpermissionset mit dem Argument inline_policy . Rolle, die mit inline_policy entspricht. Ich suche nach...
Ich hatte einige Probleme mit dem Knoten-Cache, also hatte ich die großartige Idee, diesen Befehl auf meinem Mac M1 zu starten, um alle Knotenmodules zu löschen:
find . -name 'node_modules' -type d...
Um Ihnen etwas Kontext zu geben: Ich hatte Probleme damit, dass meine Serilog-Konfiguration ordnungsgemäß funktioniert.
Lassen Sie mich erklären: Ich versuche, Serilog in einen einfachen ASP.NET Core...