【问题标题】:Axios get Request not receiving dataaxios获取请求未收到数据
【发布时间】:2021-10-21 07:33:21
【问题描述】:

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

为什么我应该在 Axios 请求中收到这个?

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();
}

【问题讨论】:

    标签: javascript java spring-boot amazon-s3 axios


    【解决方案1】:

    好的,我找到了答案。我将 Service 和 Controller 返回值更改为 String,现在它可以正常工作了

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-22
      • 2017-10-26
      • 2018-06-29
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      相关资源
      最近更新 更多