Spring Boot Sping Boot AWS凭据未经授权

zpqajqem  于 2023-03-23  发布在  Spring
关注(0)|答案(1)|浏览(167)

我正在开发一个Sping Boot 应用程序,并将其与Amazon S3 Bucket连接。这是一个用于在AWS上上传视频的简单应用程序。
视频服务类

@Service
@RequiredArgsConstructor
public class VideoService {

    private final S3Service s3Service;
    private final VideoRepository videoRepository;

    public void uploadFile(MultipartFile file)
    {
        String videoURL = s3Service.uploadFile(file);

        var video = new Video();
        video.setVideoUrl(videoURL);

        videoRepository.save(video);

    }
}

服务类别

@Service
@RequiredArgsConstructor
public class S3Service implements FileService{

    public static final String BUCKET_NAME = "****";

    private final AmazonS3Client amazonS3Client;


    @Override
    public String uploadFile(MultipartFile file) {
        var filenameExtension = StringUtils.getFilenameExtension(file.getOriginalFilename());

        var key = UUID.randomUUID().toString() + "." + filenameExtension;

        var metadata = new ObjectMetadata();
        metadata.setContentLength(file.getSize());
        metadata.setContentType(file.getContentType());

        try {
            amazonS3Client.putObject(BUCKET_NAME, key, file.getInputStream(), metadata);
        } catch (IOException ioException) {
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR,
                    "An Exception occured while uploading the file");
        }

        amazonS3Client.setObjectAcl(BUCKET_NAME, key, CannedAccessControlList.PublicRead);

        return amazonS3Client.getResourceUrl(BUCKET_NAME, key);
    }
}

控制器类:

@RestController
@RequestMapping("/api/videos")
@RequiredArgsConstructor
public class VideoController {

    private final VideoService videoService;

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public void uploadVideo(@RequestParam("file")MultipartFile file)
    {
        videoService.uploadFile(file);
    }
}

我已经在AWS上创建了s3存储桶和访问密钥
访问密钥和保密密钥存储在VM选项中:- 数据云.aws.凭据.访问密钥=-数据云.aws.凭据.密钥=
这就是我在《 Postman 》里看到的Postman
这在浏览器中显示:Browser localhost
我错过什么了吗?
我按照一个教程,并检查了每一个单独的代码与完成的仓库和一切都是一样的。而不是给我200状态,它给我401状态。

hwamh0ep

hwamh0ep1#

我有完全相同的用例。一个区别是我使用的是Amazon S3 Java V2-这是推荐使用的SDK版本。

我的控制器

@RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String singleFileUpload(@RequestParam("file") MultipartFile file) {
        try {
            byte[] bytes = file.getBytes();
            String fileName = file.getOriginalFilename();
            UUID uuid = UUID.randomUUID();
            String unqueFileName = uuid + "-" + fileName;

            DynamoDBService dbService = new DynamoDBService();
            S3Service s3Service = new S3Service();
            AnalyzePhotos analyzePhotos = new AnalyzePhotos();
            UploadEndpoint endpoint = new UploadEndpoint(analyzePhotos, dbService, s3Service);
            endpoint.upload(bytes, fileName);
            return "You have uploaded "+fileName;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "File was not uploaded";
    }

注意,我正在获取上传文件的byte[]。
endpoint.upload()在这里:

public void upload(byte[] bytes, String name) {
  // Put the file into the bucket.
  s3Service.putObject(bytes, PhotoApplicationResources.STORAGE_BUCKET, name);
  this.tagAfterUpload(name);
}

**s3Service.putObject()**在这里:

// Places an image into a S3 bucket.
    public void putObject(byte[] data, String bucketName, String objectKey) {
        S3Client s3 = getClient();
        try {
            s3.putObject(PutObjectRequest.builder()
                    .bucket(bucketName)
                    .key(objectKey)
                    .build(),
                RequestBody.fromBytes(data));
        } catch (S3Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }

getClient()是

private S3Client getClient() {
        return S3Client.builder()
            .region(PhotoApplicationResources.REGION)
            .build();
    }

现在所有这些都可以工作,并且可以从Postman调用:

看起来你的POM文件中有Spring Security Dependencies(基于你的强制登录截图)。你应该删除它们,看看你的控制器在设置断点时是否得到了文件。你应该看到:

相关问题