Angular,AWS,S3:无需完整的个人用户认证基础设施进行上传。
创始人
2024-11-01 17:02:01
0

在Angular中使用AWS SDK for JavaScript,可以实现在S3上进行文件上传而无需完整的个人用户认证基础设施。以下是一个示例解决方法:

  1. 首先,安装AWS SDK for JavaScript。在Angular项目的根目录下,使用以下命令安装AWS SDK:
npm install aws-sdk
  1. 在Angular项目的src/assets目录下,创建一个名为aws-config.json的配置文件,并填入以下内容:
{
  "accessKeyId": "YOUR_ACCESS_KEY_ID",
  "secretAccessKey": "YOUR_SECRET_ACCESS_KEY",
  "region": "YOUR_REGION",
  "bucketName": "YOUR_BUCKET_NAME"
}

请替换YOUR_ACCESS_KEY_ID,YOUR_SECRET_ACCESS_KEY,YOUR_REGION和YOUR_BUCKET_NAME为您自己的AWS S3凭证和配置信息。

  1. 在Angular项目的src/app目录下,创建一个名为aws.service.ts的服务文件,并添加以下代码:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class AwsService {
  private readonly bucketName: string;
  private readonly s3: any;

  constructor() {
    const awsConfig = require('../../assets/aws-config.json');
    this.bucketName = awsConfig.bucketName;
    this.s3 = new AWS.S3({
      apiVersion: '2006-03-01',
      accessKeyId: awsConfig.accessKeyId,
      secretAccessKey: awsConfig.secretAccessKey,
      region: awsConfig.region
    });
  }

  uploadFile(file: File): Observable {
    return new Observable(observer => {
      const params = {
        Bucket: this.bucketName,
        Key: file.name,
        Body: file,
        ACL: 'public-read'
      };

      this.s3.upload(params, (err, data) => {
        if (err) {
          observer.error(err.message);
        } else {
          observer.next(100);
          observer.complete();
        }
      }).on('httpUploadProgress', progress => {
        const uploadedPercentage = Math.round((progress.loaded / progress.total) * 100);
        observer.next(uploadedPercentage);
      });
    });
  }
}
  1. 在需要使用文件上传功能的组件中,引入AwsService并使用它来上传文件。例如,在一个名为upload.component.ts的组件中:
import { Component } from '@angular/core';
import { AwsService } from './aws.service';

@Component({
  selector: 'app-upload',
  templateUrl: './upload.component.html',
  styleUrls: ['./upload.component.css']
})
export class UploadComponent {
  constructor(private awsService: AwsService) { }

  onFileSelected(event: any): void {
    const file: File = event.target.files[0];
    this.awsService.uploadFile(file).subscribe(
      progress => {
        console.log(`File upload progress: ${progress}%`);
      },
      error => {
        console.error('File upload failed:', error);
      },
      () => {
        console.log('File upload completed!');
      }
    );
  }
}
  1. 在组件的HTML模板中,添加一个文件选择输入框,用于选择要上传的文件。例如,在upload.component.html中:

以上代码示例演示了如何在Angular中使用AWS SDK for JavaScript和S3进行文件上传。在上传文件时,代码会根据配置文件中的凭证和配置信息,将文件上传到指定的S3存储桶,并在上传过程中获取上传进度。

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...