@ affect-aws/s3 (英语)
这个软件包提供了 S3 具体有效的公用事业和执行.
安装
npm install --save @effect-aws/s3 @effect-aws/client-s3
使用量
使用 MultipartUpload
这个 MultipartUpload 是一个有效的端口 Upload 类从 @aws-sdk/lib-storage 软件包.
示例
import { S3 } from "@effect-aws/client-s3"
import { MultipartUpload } from "@effect-aws/s3"
import { FileSystem } from "@effect/platform"
import { Effect } from "effect"
const program = MultipartUpload.uploadObject(
{
Bucket: "my-bucket",
Key: "my-object",
Body: new Uint8Array(1024 * 1024 * 10) // 10 MB
},
{
// (optional) concurrency configuration
queueSize: 4,
// (optional) size of each part, in bytes, at least 5MB
partSize: FileSystem.MiB(5)
}
)
program.pipe(Effect.provide(S3.defaultLayer), Effect.runPromise)
使用 S3FileSystem
这个 S3FileSystem 用于平台 文件系统 标签.
您可以用它来操作 S3 桶中的文件, 好像它们是本地文件一样 .
示例 (将文件读作字符串)
import { S3 } from "@effect-aws/client-s3"
import { S3FileSystem } from "@effect-aws/s3"
import { FileSystem } from "@effect/platform"
import { NodeRuntime } from "@effect/platform-node"
import { Effect } from "effect"
const program = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem
// Reading the content of the same file where this code is written
const content = yield* fs.readFileString("content.txt")
console.log(content)
})
// Provide the necessary context and run the program
NodeRuntime.runMain(
program.pipe(
// ┌─── Layer<FileSystem, never, S3Service>
// ▼
Effect.provide(S3FileSystem.layer({ bucketName: "example-bucket" })),
Effect.provide(S3.defaultLayer)
)
)
示例 (单位:千美元)使用 Schema 校验 KeyValueStore:
import { S3 } from "@effect-aws/client-s3"
import { S3FileSystem } from "@effect-aws/s3"
import { Path } from "@effect/platform"
import { KeyValueStore } from "@effect/platform"
import { Effect, Layer, Schema } from "effect"
// Define a schema for the values
const Person = Schema.Struct({
name: Schema.String,
age: Schema.Number
})
const program = Effect.gen(function* () {
// Create a SchemaStore based on the Person schema
const kv = (yield* KeyValueStore.KeyValueStore).forSchema(Person)
// Add a value that adheres to the schema
const value = { name: "Alice", age: 30 }
yield* kv.set("user1", value)
console.log(yield* kv.size)
// Retrieve and log the value
console.log(yield* kv.get("user1"))
})
// Slash is important here, indicating a folder ──────┐
// ▼
const mainLayer = KeyValueStore.layerFileSystem("store/").pipe(
Layer.provide(S3FileSystem.layer({ bucketName: "example-bucket" })),
Layer.provide(S3.defaultLayer),
Layer.provide(Path.layer)
)
// Use the S3 file system store implementation
Effect.runPromise(program.pipe(Effect.provide(mainLayer)))
/*
Output:
1
{ _id: 'Option', _tag: 'Some', value: { name: 'Alice', age: 30 } }
*/