已配置AWS Glue爬虫,如何通过Web API访问Data Catalog?
Got it! Since you already have this working with Python's Boto3, switching to the AWS SDK for JavaScript is straightforward. Below are examples for both the recommended v3 modular SDK and the older v2 SDK, so you can pick whichever fits your setup:
AWS SDK for JavaScript v3 (Modular)
First, install the necessary packages. You'll need the Glue client and the credential provider package (though the SDK will automatically pick up your AWS credentials from environment variables, IAM roles, or the AWS config file just like Boto3):
npm install @aws-sdk/client-glue @aws-sdk/credential-providers
Then here's the code to fetch your Glue databases:
import { GlueClient, GetDatabasesCommand } from "@aws-sdk/client-glue"; // Initialize the Glue client with your region const client = new GlueClient({ region: "us-west-2" }); async function getGlueDatabases() { try { // Create and send the GetDatabases command const command = new GetDatabasesCommand({}); const response = await client.send(command); // Log or process the database list console.log("Glue Databases:", response.DatabaseList); return response.DatabaseList; } catch (error) { console.error("Error fetching databases:", error); throw error; } } // Call the function getGlueDatabases();
AWS SDK for JavaScript v2
If you're still using the v2 SDK (the older, monolithic version), here's how to do it:
First install the SDK:
npm install aws-sdk
Then the code:
const AWS = require('aws-sdk'); // Configure the region AWS.config.update({ region: 'us-west-2' }); // Initialize the Glue client const glue = new AWS.Glue(); // Fetch databases using callbacks glue.getDatabases({}, (err, data) => { if (err) { console.error("Error getting databases:", err); } else { console.log("Glue Databases:", data.DatabaseList); } }); // Or use async/await for cleaner code: async function getDatabasesAsync() { try { const data = await glue.getDatabases().promise(); console.log("Glue Databases:", data.DatabaseList); return data.DatabaseList; } catch (err) { console.error("Error getting databases:", err); } } getDatabasesAsync();
A quick note: Just like with Boto3, make sure your execution environment has the correct IAM permissions (glue:GetDatabases) to access your Glue Data Catalog. The SDK will handle credential resolution automatically (from ~/.aws/credentials, environment variables, IAM roles for EC2/EKS/Lambda, etc.).
内容的提问来源于stack exchange,提问作者alexopoulos7

