javascript - 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();
}
解决方案
好的,我找到了答案。我将服务和控制器返回值更改为字符串,现在它可以完美运行
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();
}
推荐阅读
- c# - Unity Fog 在 Android 上看起来不一样
- python - 如何更改 db.session.query 值 Sqlalchemy
- amazon-web-services - 无法将安全组添加到启动模板
- python - 分解 Dask DataFrames
- wordpress - 如何使用 Wordpress、Tailwind 和 Gutenberg 清除 CSS
- java - 使用泛型检索泛型类型的类列表
- c - 如何在不使用 itoa 的情况下将大十进制数字转换为二进制?
- html - React - 关于重新执行组件时 html 标签的属性值如何变化,以下是否正确?
- ruby-on-rails - Rspec:变量在不同范围内不可用
- python - 无法在 google colab 上安装 dask