Thi*_*iss 1 amazon-s3 amazon-web-services angular
所以我是打字稿和亚马逊的新手....
我需要从 S3 存储桶中获取一个文件并在我的视图中显示其内容(比如 readme.txt)。
我已经完成了获取对象,但在打字稿/角度中找不到如何实际读取文件。这个样本
https://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingJava.html
显示了 java 示例,但它依赖于我认为在 angular 中不存在的缓冲区和流读取器?
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = null;
while ((line = reader.readLine()) != null) {
Run Code Online (Sandbox Code Playgroud)
感谢有关下一步去哪里的一些指导 - 有人说我必须使用指令?
只是为了澄清 - 我可以获得 data.Body 但它是一个二进制缓冲区 - 我如何将其恢复为文本?
塔克斯
对,这里是 Typescript 中的代码。
正如文档中所建议的,您可以从data.Body.
当我这样做时,我可以看到它Body是一个uint8array,所以我们需要使用它TextDecoder来获取它作为一个字符串。
import { Component, OnInit } from '@angular/core';
import * as AWS from 'aws-sdk';
declare var TextDecoder;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app';
ngOnInit() {
// Set up credentials
AWS.config.credentials = new AWS.Credentials({
accessKeyId: 'YOURKEY', secretAccessKey: 'YOURSECRET'
});
const params = {
Bucket: 'so-test-bucket',
Key: 'index.html'
};
let s3 = new AWS.S3();
s3.getObject(params, function(err, data) {
if (err) {
console.error(err); // an error occurred
} else {
const string = new TextDecoder('utf-8').decode(data.Body);
console.log(string);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6075 次 |
| 最近记录: |