gpt4 book ai didi

spring - 如何为amazonS3客户端设置区域?

转载 作者:行者123 更新时间:2023-12-02 07:23:22 25 4
gpt4 key购买 nike

我正在尝试在 Java Web 应用程序中实现文件上传。当用户通过 UI 上传文件时,我希望将其存储在亚马逊 amazon s3 存储中。

这是我的后端代码:

@PostMapping("/upload-file")
public String saveFile(@RequestParam("name") String name, @RequestParam("file")MultipartFile file, RedirectAttributes redirectAttributes){

AWSCredentials credentials = new BasicAWSCredentials("user", "psw");

AmazonS3 s3Client = new AmazonS3Client(credentials);

String bucketName = "kfib";

s3Client.createBucket(bucketName);

s3Client.setRegion(com.amazonaws.regions.Region.getRegion(Regions.EU_WEST_1));


ObjectMetadata md = new ObjectMetadata();
md.setContentType("application/pdf");

//md.set

    try{
InputStream inputStream = file.getInputStream();

s3Client.putObject(new PutObjectRequest(bucketName, name, inputStream, md).withCannedAcl(CannedAccessControlList.PublicRead));

S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucketName, name));

log.info("Url of the image:\n");
log.info(s3Object.getObjectContent().getHttpRequest().getURI().toString());

}catch(IOException e){
e.printStackTrace();
}


log.info("upload called with post method...");

return "redirect:/admin/posts";
}

如果我将文件上传到默认区域,我就可以上传文件。但是,我希望将我的文件存储在另一台服务器上。我在欧盟地区有一个存储桶,如果我尝试使用上述代码上传某些内容,则会收到以下错误代码:

com.amazonaws.services.s3.model.AmazonS3Exception: The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'eu-central-1' (Service: Amazon S3; Status Code: 400; Error Code: AuthorizationHeaderMalformed; Request ID: 8EF630B5F0224C4F)

好像是这一行

s3Client.setRegion(com.amazonaws.regions.Region.getRegion(Regions.EU_WEST_1));

没有任何效果。我做错了什么?

最佳答案

我遇到了同样的问题,但以下方法对我有用 -

AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1).build();

关于spring - 如何为amazonS3客户端设置区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43020026/

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