获取错误的桶区域错误,而流媒体视频在Spring Boot 与客户端Android

zour9fqk  于 2023-05-05  发布在  Android
关注(0)|答案(1)|浏览(112)

我试图创建视频流应用程序与后端在Spring Boot 。我已经创建了两个api下载视频文件和流视频文件。
用于下载视频文件

@PostMapping("/api/v1/downloadVideoAWS")
public ResponseEntity<ByteArrayResource> downloadFile(@RequestBody FileSnapshot fileSnapshot) {
    byte[] data = fileSnapshotService.downloadVideoAWS(fileSnapshot);
    ByteArrayResource resource = new ByteArrayResource(data);
    return ResponseEntity.ok().contentLength(data.length)
            .header("Content-type", "application/octet-stream")
            .header("Content-disposition", "attachement; filename=\"" + fileSnapshot.getFileName() + "\"")
            .body(resource);
}

它的客户端要求在Android改造

@HTTP(method = "POST", hasBody = true,path = "/api/v1/downloadVideoAWS")
Call <ResponseBody> getVideoFile(@Body FileSnapshot fileSnapshot);

用于流式传输视频文件

@GetMapping(value = "/api/v1/streamVideo/**", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
public ResponseEntity<StreamingResponseBody> getObject(HttpServletRequest request) {
    try {

        String uri = request.getRequestURI();
        String uriParts[] = uri.split("/", 2)[1].split("/", 2);
        String bucket = uriParts[0];
        String key = uriParts[1];
        System.out.println("Fetching " + uri);
        AmazonS3 s3client = fileSnapshotService.getS3Client();
        S3Object object = s3client.getObject(bucket, key);
        S3ObjectInputStream finalObject = object.getObjectContent();

        final StreamingResponseBody body = outputStream -> {
            int numberOfBytesToWrite = 0;
            byte[] data = new byte[1024];
            while ((numberOfBytesToWrite = finalObject.read(data, 0, data.length)) != -1) {
                outputStream.write(data, 0, numberOfBytesToWrite);
            }
            finalObject.close();
        };
        return new ResponseEntity<StreamingResponseBody>(body, HttpStatus.OK);
    } catch (Exception e) {
        System.err.println("Error "+ e.getMessage());
        return new ResponseEntity<StreamingResponseBody>(HttpStatus.BAD_REQUEST);
    }

}

它的客户端要求在Android改造

@GET("/api/v1/streamVideo/{bucketName}/{fileName}")
Call<ResponseBody> getStreamingObject(@Path("bucketName") String bucketName,@Path("fileName")String fileName);

AmazonS 3客户端

@Bean
public AmazonS3 gennerateS3Client() {
    AWSCredentials credentials = new BasicAWSCredentials(accessKey,secretKey);
    return  AmazonS3ClientBuilder.standard()
            .withRegion(regionCode)
            .withCredentials(new AWSStaticCredentialsProvider(credentials))
            .build();

}

其中regionCode =“ap-south-1”;
现在的问题是,我能够下载视频文件没有任何问题。但当我试图从API流视频文件,我得到这个错误在Spring Boot
错误存储桶在此区域:eu-west-1。请使用此区域重试请求(服务:Amazon S3;状态代码:301;错误代码:永久重定向;申请编号:XXXXXXXXXXXXXX;S3扩展请求ID:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx代理:null)
请帮我解决这个问题

m1m5dgzv

m1m5dgzv1#

这是由于此代码引起的存储桶名称问题

String uri = request.getRequestURI();
String uriParts[] = uri.split("/", 2)[1].split("/", 2);
String bucket = uriParts[0];
String key = uriParts[1];

,在访问之前,需要确保存储桶名称是key正确

相关问题