gpt4 book ai didi

java - AWS Java SDK - 无法通过区域提供商链找到区域

转载 作者:IT老高 更新时间:2023-10-28 21:05:51 25 4
gpt4 key购买 nike

我已经完成了题为“以编程方式设置 AWS 区域 1”的问题,但它并没有提供我需要的所有答案。

Q1:我收到 SDKClientException-Unable to find a region via the region provider chain。我究竟做错了什么?还是我漏掉了一个错字。

public class CreateS3Bucket {

public static void main(String[] args) throws IOException {

BasicAWSCredentials creds = new BasicAWSCredentials("aws-access-key", "aws-secret-key");
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).build();

Region region = Region.getRegion(Regions.US_EAST_1);
s3Client.setRegion(region);

try {
String bucketName = "testBucket" + UUID.randomUUID();
s3Client.createBucket(bucketName);
System.out.println("Bucket Created Successfully.");

} catch(AmazonServiceException awse) {

System.out.println("This means that your request made it AWS S3 but got rejected");
System.out.println("Error Message:" +awse.getMessage());
System.out.println("Error Message:" +awse.getErrorCode());
System.out.println("Error Message:" +awse.getErrorType());
System.out.println("Error Message:" +awse.getRequestId());

} catch (AmazonClientException ace) {

System.out.println("The Amazon Client encountered an Error with network Connectivity");
System.out.println("Error Message:" + ace.getMessage());
}


}

}

Q2:如果我想用它构建一个 Lambda 函数,需要做哪些代码更改?我知道如何创建它需要的 lambda 函数和角色。只需要知道我编写的代码是否需要更改。我应该如何实现 LambdaFuctionHandler 类如下:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<String, String> {

@Override
public String handleRequest(String input, Context context) {
context.getLogger().log("Input: " + input);


return null;
}

}

最佳答案

关于 Q1,尝试使用以下语法构建您的客户端:

AmazonS3 amazonS3 = AmazonS3Client.builder()
.withRegion("us-east-1")
.withCredentials(new AWSStaticCredentialsProvider(creds))
.build();

关于java - AWS Java SDK - 无法通过区域提供商链找到区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44151982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com