Skip to content

feat(s3): implement put stream method #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ export class BlobFilePermissionDeniedError extends Error {
super(`Permission denied: ${path}`);
this.name = "BlobFilePermissionDeniedError";
}
}

export class UnimplementedError extends Error {
constructor() {
super("Unimplemented. Please refer to not use this method.");
}
}
71 changes: 66 additions & 5 deletions src/s3/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
GetObjectCommand, HeadObjectCommand, ListObjectsV2Command, NoSuchKey,
NotFound, PutObjectCommand,
S3Client,
type S3ClientConfig, S3ServiceException
type S3ClientConfig, S3ServiceException, CompleteMultipartUploadCommand, CreateMultipartUploadCommand, UploadPartCommand
} from "@aws-sdk/client-s3";
import { BlobFileNotExistError, BlobMismatchedMD5IntegrityError } from "../errors";
import { BlobFileNotExistError, BlobMismatchedMD5IntegrityError, UnimplementedError } from "../errors";

export class S3Storage implements IObjectStorage {
private readonly client: S3Client;
Expand Down Expand Up @@ -144,7 +144,7 @@ export class S3Storage implements IObjectStorage {
}

async getStream(path: string): Promise<Readable> {
return new Readable();
throw new UnimplementedError();
}

async list(path?: string): Promise<Iterable<string>> {
Expand Down Expand Up @@ -201,8 +201,69 @@ export class S3Storage implements IObjectStorage {
}
}

putStream(path: string, options?: PutOptions): Promise<Writable> {
return Promise.resolve(new Writable());
async putStream(path: string, options?: PutOptions): Promise<Writable> {
const command = new CreateMultipartUploadCommand({
Bucket: this.bucketName,
Key: path,
Metadata: options?.metadata,
CacheControl: options?.cacheControl,
ContentEncoding: options?.contentEncoding,
ContentDisposition: options?.contentDisposition,
ContentType: options?.contentType,
ContentLanguage: options?.contentLanguage
});

const response = await this.client.send(command);
const uploadIdentifier = response.UploadId;

// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
return new Writable({
defaultEncoding: "binary",
write(chunk: any, encoding: BufferEncoding, callback: (error?: (Error | null)) => void) {
const command = new UploadPartCommand({
Body: chunk,
Bucket: that.bucketName,
Key: path,
UploadId: uploadIdentifier,
PartNumber: 0
});

that.client.send(command).catch((error: Error | null | undefined) => error ? callback(error) : callback(null));
},
writev(chunks: Array<{
chunk: any;
encoding: BufferEncoding;
}>, callback: (error?: (Error | null)) => void) {
for (let i = 0; i < chunks.length; i++) {
const command = new UploadPartCommand({
Body: chunks[i]?.chunk,
Bucket: that.bucketName,
Key: path,
UploadId: uploadIdentifier,
PartNumber: i
});

that.client.send(command).catch((error: Error | null | undefined) => error ? callback(error) : callback(null));
}
},
final(callback: (error?: (Error | null)) => void) {
const command = new CompleteMultipartUploadCommand({
Bucket: that.bucketName,
Key: path,
UploadId: uploadIdentifier
});

that.client.send(command).catch((error: Error | null | undefined) => {
if (error) {
callback(error);
return;
}

callback(null);
});
}
});
}

async stat(path: string): Promise<StatResponse> {
Expand Down