Amazon s3 如何使用服务文件中回调函数的变量并将其发送到app.component.ts文件
我使用的是AmazonS3 bucket,它有我的文件,我能够读取服务文件中特定文件的内容。但我的方法是从服务文件中获取该内容,并在组件文件中使用它,以便在视图中显示该文件的数据。我面临一个挑战,我有S3方法来获取file的对象,它是一个回调函数,它包含file的内容,我无法访问函数之外的内容 服务文件:---Amazon s3 如何使用服务文件中回调函数的变量并将其发送到app.component.ts文件,amazon-s3,angular8,Amazon S3,Angular8,我使用的是AmazonS3 bucket,它有我的文件,我能够读取服务文件中特定文件的内容。但我的方法是从服务文件中获取该内容,并在组件文件中使用它,以便在视图中显示该文件的数据。我面临一个挑战,我有S3方法来获取file的对象,它是一个回调函数,它包含file的内容,我无法访问函数之外的内容 服务文件:--- 从'@angular/core'导入{Injectable,Input,Output,EventEmitter}; 从“AWS sdk”导入*作为AWS; 从“aws sdk/clien
从'@angular/core'导入{Injectable,Input,Output,EventEmitter};
从“AWS sdk”导入*作为AWS;
从“aws sdk/clients/s3”导入*作为s3;
从'@angular/router'导入{Routes,RouterModule};
从“rxjs/operators”导入{map};
从“rxjs”导入{Observable};
从“带数据的ngx导航”导入{NgxNavigationWithDataComponent};
从'rxjs'导入{of};
从“../file upload”导入{FileUpload};
从“../fileread/fileread.component”导入{FilereadComponent};
从“rxjs”导入{BehaviorSubject}
@注射的({
providedIn:'根'
})
导出类上载服务{
private fileContentSource=new BehaviorSubject('message');
fileContent=this.fileContentSource.asObservable();
文件夹=“”;
BUCKET='ewcompensation';
userId:any;
从\u服务器加载\u值\u=“”;
构造函数(公共navCtrl:NgxNavigationWithDataComponent){
this.userId=sessionStorage.getItem(“userId”);
}
changeFC(文件内容){
this.fileContentSource.next(fileContent)
}
public getS3Bucket():任意{
const bucket=new s3(
{
accessKeyId:“”,
secretAccessKey:“”,
区域:“”
}
);
返回铲斗;
}
getEachFile(bucket,文件名){
常量keyback={
桶:桶,
关键字:文件名
};
this.getS3Bucket().getObject(keyback,函数(err,data){
如果(错误){
控制台错误(err);
}否则{
const content=新的文本解码器('utf-8')。解码(data.Body);
log('myfilecontent:'+content);
}
});
返回(钥匙桶);
}
上载文件(文件){
常量参数={
水桶:这个,水桶,
Key:this.FOLDER+file.name,
正文:文件,
//ACL:“公共读取”
};
this.getS3Bucket().upload(参数、函数(错误、数据){
如果(错误){
console.log('上载文件时出错:',错误);
返回false;
}
console.log('已成功上载文件',数据);
返回true;
});
归还(文件);
}
getFiles():可观察{
const fileUploads=新数组();
常量参数={
水桶:这个,水桶,
前缀:this.FOLDER
};
this.getS3Bucket().listObjects(参数、函数(错误、数据){
如果(错误){
console.log('获取文件时出错:'+err);
返回;
}
log('成功获取文件',数据);
const fileDatas=data.Contents;
forEach(函数(文件){
推送(新文件上传(file.Key,'https://s3.amazonaws.com/“+params.Bucket+”/“+file.Key”);
});
});
返回(文件上传);
}
恩戈尼尼特(){
}
}
app.component.ts文件:-
构造函数(公共uploadService:uploadService,公共navCtrl:NgxNavigationWithDataComponent,私有路由器:RouterModule){
this.fileName='JSON file.txt';
this.bucketName='ewcompensation';
this.uploadService.getEachFile(this.bucketName,this.fileName).subscribe(response=>{
console.log(响应)
})
}
这个.getS3Bucket().getObject(keyback,函数(err,data){//函数读取文件的内容
如果(错误){
控制台错误(err);
}否则{
const content=新的文本解码器('utf-8')。解码(data.Body);
console.log('myfilecontent:'+content);//这里它显示了我的文件的内容。
}
我想在app.component.ts文件中发送此内容,请告诉我如何实现此目的
import { Injectable, Input, Output ,EventEmitter} from '@angular/core';
import * as AWS from 'aws-sdk';
import * as s3 from 'aws-sdk/clients/s3';
import { Routes, RouterModule } from '@angular/router';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { NgxNavigationWithDataComponent } from 'ngx-navigation-with-data';
import { of } from 'rxjs';
import { FileUpload } from '../file-upload';
import { FilereadComponent } from '../fileread/fileread.component';
import { BehaviorSubject } from 'rxjs'
@Injectable({
providedIn: 'root'
})
export class UploadService {
private fileContentSource = new BehaviorSubject('message');
fileContent = this.fileContentSource.asObservable();
FOLDER = '';
BUCKET = 'ewcompensation';
userId: any;
load_values_from_server = '';
constructor(public navCtrl: NgxNavigationWithDataComponent) {
this.userId = sessionStorage.getItem("userId");
}
changeFC(fileContent) {
this.fileContentSource.next(fileContent)
}
public getS3Bucket(): any {
const bucket = new s3(
{
accessKeyId: '',
secretAccessKey: '',
region: ''
}
);
return bucket;
}
getEachFile(bucket, fileName) {
const keyBucket = {
Bucket: bucket,
Key: fileName
};
this.getS3Bucket().getObject(keyBucket, function (err, data) {
if (err) {
console.error(err);
} else {
const content = new TextDecoder('utf-8').decode(data.Body);
console.log('myfilecontent : ' + content);
}
});
return of(keyBucket);
}
uploadFile(file) {
const params = {
Bucket: this.BUCKET,
Key: this.FOLDER + file.name,
Body: file,
// ACL: 'public-read'
};
this.getS3Bucket().upload(params, function (err, data) {
if (err) {
console.log('There was an error uploading your file: ', err);
return false;
}
console.log('Successfully uploaded file.', data);
return true;
});
return of(file);
}
getFiles(): Observable<Array<FileUpload>> {
const fileUploads = new Array<FileUpload>();
const params = {
Bucket: this.BUCKET,
Prefix: this.FOLDER
};
this.getS3Bucket().listObjects(params, function (err, data) {
if (err) {
console.log('There was an error getting your files: ' + err);
return;
}
console.log('Successfully get files.', data);
const fileDatas = data.Contents;
fileDatas.forEach(function (file) {
fileUploads.push(new FileUpload(file.Key, 'https://s3.amazonaws.com/' + params.Bucket + '/' + file.Key));
});
});
return of(fileUploads);
}
ngOnInit() {
}
}
app.component.ts file:-
constructor(public uploadService: UploadService, public navCtrl: NgxNavigationWithDataComponent , private router : RouterModule) {
this.fileName = 'JSON file.txt';
this.bucketName = 'ewcompensation';
this.uploadService.getEachFile(this.bucketName,this.fileName).subscribe(response => {
console.log(response)
})
}
this.getS3Bucket().getObject(keyBucket, function (err, data) {//function to read the content of file
if (err) {
console.error(err);
} else {
const content = new TextDecoder('utf-8').decode(data.Body);
console.log('myfilecontent : ' + content);// here it shows the content of my file.
}