我在尝试调试AWS Lambda函数时收到以下错误。20:38:01.244 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 301 Moved Permanently[\r][\n]"
20:38:01.244 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "x-amz-bucket-region: us-east-1[\r][\n]"
我的部分困惑是,我正在使用通常来自ap-south-1的桶,而实际上位于us-east-1,我真的不知道为什么us-east-1会出现在记录中。
我的代码是一个基本函数,在接收到S3 Put的触发器时,记录一些事情:log.info("Lambda function is invoked: Processing the uploads........." + s3Event.toJson());
log.info("File - "+ FileName+" uploaded into "+BucketName+" bucket at "+ s3Event.getRecords().get(0).getEventTime());
try (InputStream is = s3client.getObject(BucketName, FileName).getObjectContent()) { log.info("File Contents : "+StreamUtils.copyToString(is, StandardCharsets.UTF_8));}
全部
private final String accessKeyId = System.getenv("accessKeyId");
private final String secretAccessKey = System.getenv("secretAccessKey");
private final String region = System.getenv("region");
private final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
AmazonS3 s3client = AmazonS3ClientBuilder
.standard()
.withRegion("ap-south-1")
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
.build();
static final Logger log = LoggerFactory.getLogger(S3EventHandler.class);
@Override
public String handleRequest(S3Event s3Event, Context context) {
log.info("Lambda function is invoked: Processing the uploads........." + s3Event.toJson());
String BucketName = s3Event.getRecords().get(0).getS3().getBucket().getName();
String FileName = s3Event.getRecords().get(0).getS3().getObject().getKey();
log.info("File - "+ FileName+" uploaded into "+
BucketName+" bucket at "+ s3Event.getRecords().get(0).getEventTime());
try (InputStream is = s3client.getObject(BucketName, FileName).getObjectContent()) {
log.info("File Contents : "+StreamUtils.copyToString(is, StandardCharsets.UTF_8));
}catch (IOException e){
e.printStackTrace();
return "Error reading contents of the file";
}
return null;
}
x1c 0d1x使用Toolkit插件的调试功能,我尝试模拟S3 Put。这会向S3 Function发送S3 Event,并记录所有内容:lambdas3.S3EventHandler - Lambda function is invoked: Processing the uploads......... (etc)
lambdas3.S3EventHandler - File - HappyFace.jpg uploaded into source-bucket bucket at 1970-01-01T00:00:00.000Z
直到它运行到试图从bucket中获取文件的代码。我希望它返回文件的内容,或者返回一个错误,因为lambda函数只接收到事件,而对象没有在任何地方创建,
但是,它通知我某些内容已经被“301 Moved Permanently”和一些关于“x-amz-bucket-region:us-east-1”当一切都应该在ap-south-1
中时
所以现在我很困惑为什么我的区域被改变了,为什么我得到的是这个错误,而不是我期望的两个结果之一。
1条答案
按热度按时间7gcisfzg1#
那看起来是客户端的旧版本。我猜301正在添加HTTPS
https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectData.java
在Aws上运行代码时,尽量避免使用密钥和secret。
去拿证件。