我在react中使用AWS SDK aws-sdk/client-batch来启动AWS批处理作业。为此,我希望传递我的AWS凭据,以便可以启动批处理作业。
我知道有几种不同的方法可以将凭据传递给AWS,但是如果我使用react,传递凭据的最佳方法是什么?
目前我的代码是:
import React, { useState, useEffect } from 'react';
import { BatchClient, SubmitJobCommand, CancelJobCommand, DescribeJobsCommand } from "@aws-
sdk/client-batch"
const App = () => {
const [ batchClient, setBatchClient ] = useState(null);
const [ submittedJobDetails, setSubmittedJobDetails ] = useState(null);
const [ jobDetails, setJobDetails ] = useState(null);
const [ error, setError ] = useState(null);
const [ cancelSuccess, setCancelSuccess ] = useState(null);
useEffect (() => {
const awsConfig = {
region: "us-east-1",
};
const client = new BatchClient(awsConfig);
setBatchClient(client);
}, []);
const startBatchJob = async () => {
try{
const params = {
jobDefinition: "my-login-test",
jobName: "my-poc",
jobQueue: "my-jq-alpha",
};
if (!batchClient) {
setError('Batch client not initialized..')
return;
}
const command = new SubmitJobCommand(params);
const data = await batchClient.send(command);
console.log('Batch job started successfully: ', data);
setSubmittedJobDetails(data.jobId);
setError(null);
} catch (error) {
console.error('Error starting batch job: ', error);
setError('Error starting Batch job');
setSubmittedJobDetails(null);
}
};
return (
<div>
<h1>AWS Batch Job Submission</h1>
<button onClick={startBatchJob}>Start Batch Job</button>
<button onClick={cancelBatchJob}>Cancel Batch Job</button>
{submittedJobDetails && (
<div>
<h2>Submitted Job Details: </h2>
<pre>{JSON.stringify(submittedJobDetails, null, 2)}</pre>
</div>
)}
</div>
);
}
export default App;
字符串
我不是React的Maven,所以我不确定在哪里设置AWS凭据?
任何帮助都非常感谢。
1条答案
按热度按时间s5a0g9ez1#
您希望对可以承担AWS IAM角色的用户进行身份验证,以便对您的AWS资源执行操作。
AWS JavaScript SDK文档中有一个示例,用于从HTTP客户端(在本例中为浏览器)使用Cognito用户池到list the objects in a private S3 bucket。
AWS SDK Javascript samples GitHub repo也有一个using SQS的例子,可以为你自己的代码提供一些提示。