首页 > 解决方案 > axios获取请求未收到数据

问题描述

我正在尝试下载我的 s3 存储桶的内容,当我点击 API 端点时,数据显示在我的 Intellij 控制台中,但在我的邮递员和浏览器控制台中,我只是得到一个空对象。

是否有一定的原因我应该在 Axios 请求中收到这个?

爱讯 -

downloadLocations() {
  axios.get("http://localhost:8080/api/v1/targetLocation/downloadSearchData")
      .then((res) => {
        console.log(res.data)

        // We will need to retrieve the data into a downloadable blob
        // const  content = new Blob([JSON.stringify(???)],{ type: 'text/plain;charset=utf-8' })
        // const fileName = `test.txt`
        // saveAs(content, fileName)
      }, (error) => {
        console.log(error);
      });
}

服务 -

public ByteArrayOutputStream downloadSearchData() throws IOException {
    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
    AmazonS3 s3client = AmazonS3ClientBuilder
            .standard()
            .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
            .withRegion(awsRegion)
            .build();

    var s3Object = s3client.getObject("downloadable-cases", "7863784198_2021-08-16T13_30_06.690Z.json");
    var out = new ByteArrayOutputStream();
    try (var in = s3Object.getObjectContent()) {
        in.transferTo(out);
    }
    System.out.println(out);
    return out;
}

控制器 -

@GetMapping(value = "downloadSearchData")
public ByteArrayOutputStream downloadSearchData() throws IOException {
    return targetLocationService.downloadSearchData();
}

标签: javascriptjavaspring-bootamazon-s3axios

解决方案


好的,我找到了答案。我将服务和控制器返回值更改为字符串,现在它可以完美运行

public String downloadSearchData() throws IOException {
    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
    AmazonS3 s3client = AmazonS3ClientBuilder
            .standard()
            .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
            .withRegion(Regions.GovCloud)
            .build();

    var s3Object = s3client.getObject("downloadable-cases", "7863784198_2021-08-16T13_30_06.690Z.json");
    var out = new ByteArrayOutputStream();
    try (var in = s3Object.getObjectContent()) {
        in.transferTo(out);
    }
    return out.toString();
}

推荐阅读