# midway-component-aws

> midway aws component

Latest version **0.0.9** (published 2023-10-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install midway-component-aws
pnpm add midway-component-aws
yarn add midway-component-aws
bun add midway-component-aws
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.9 |
| Published | 2023-10-05 |
| First published | 2023-10-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 17.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | ion1ze |
| Maintainers | ion1ze |

## Links

- npm: https://www.npmjs.com/package/midway-component-aws
- Repository: https://github.com/ion1ze/midway-component-aws
- Issues: https://github.com/ion1ze/midway-component-aws/issues
- npm.io page: https://npm.io/package/midway-component-aws

## Dependencies (4)

- [assert](https://npm.io/package/assert.md) ^2.1.0
- [@midwayjs/core](https://npm.io/package/@midwayjs/core.md) ^3.12.3
- [@aws-sdk/client-s3](https://npm.io/package/@aws-sdk/client-s3.md) ^3.421.0
- [@aws-sdk/client-sts](https://npm.io/package/@aws-sdk/client-sts.md) ^3.425.0

## Recent versions

- 0.0.9 (latest) — 2023-10-05
- 0.0.8 — 2023-10-05
- 0.0.7 — 2023-10-05
- 0.0.6 — 2023-10-05
- 0.0.5 — 2023-10-05
- 0.0.4 — 2023-10-05
- 0.0.2 — 2023-10-05
- 0.0.1 — 2023-10-05

## README

# midway-component-s3

> 对接 `S3` 协议存储，使用的是 `AWS` 的 `SDK`，理论支持所有支持 `S3` 协议的对象存储，包括 `Minio`。

## 安装依赖

`npm i midway-component-s3 -S`

## 引入组件

首先，引入组件 `configuration.ts` 中导入：

```javascript
import { Configuration } from '@midwayjs/core';
import * as s3 from 'midway-component-s3';
import { join } from 'path'

@Configuration({
  imports: [
    // ...
    s3     // 导入 s3 组件
  ],
  importConfigs: [
    join(__dirname, 'config')
  ]
})
export class MainConfiguration {
}
```

## 配置 S3

`S3` 组件需要配置后才能使用，需要填写 `S3` 服务的 `endpoint`，`region`，`apiVersion`，`credentials` 等参数，下面是使用 `Minio` 自建服务的参数。

```javascript
// src/config/config.default
export default {
  // ...
  s3: {
    client: {
      endpoint: 'http://localhost:9000',
      region: 'local',
      apiVersion: '2011-06-15',
      credentials: {
        accessKeyId: 'your access key id',
        secretAccessKey: 'your secret access key',
      },
    },
  },
}
```

## 使用组件

使用 `STS` 获取临时秘钥

```javascript
import { S3Service } from 'midway-component-s3';
import { join } from 'path';

@Provide()
export class UserService {

  @Inject()
  s3Service: S3Service;

  async getCredentails() {

    const result = await s3Service.assumeRole({
      RoleArn: '',
      RoleSessionName: '',
      Policy: JSON.stringify({
        Version: '2012-10-17', // 协议版本，固定值
        Statement: [
          {
            Effect: 'Allow',
            Action: ['s3:GetObject', 's3:GetBucketLocation'],
            Resource: ['arn:aws:s3:::*'],
          },
        ],
      }),
      DurationSeconds: 3600, // 临时凭证有效期，单位秒，最小900，最大3600
    })
    // => result.Credentials
  }
}
```

---
_Source: https://npm.io/package/midway-component-aws · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
