Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { AWSError, S3 } from "aws-sdk";
- import { getJsonConfig, DatalakeConfig } from "./config";
- export class S3Client {
- private config: DatalakeConfig;
- private innerClient: S3;
- public constructor(config: string | DatalakeConfig) {
- if (typeof config === "string") {
- this.config = getJsonConfig("datalake.reader");
- } else {
- this.config = config;
- }
- this.innerClient = new S3({
- accessKeyId: this.config.accessKeyId,
- secretAccessKey: this.config.secretAccessKey
- });
- }
- private withPrefix(path: string): string {
- let result = this.config.bucketPath;
- if (!result.endsWith('/')) {
- result += "/";
- }
- if (path.includes(result)) {
- return path;
- }
- return result + path;
- }
- public async keyExists(key: string): Promise<boolean> {
- const params = {
- Bucket: this.config.bucketName,
- Key: this.withPrefix(key)
- };
- return new Promise<boolean>( (resolve, reject) => {
- this.innerClient.headObject(params, (err: AWSError) => {
- if (err) {
- if (err.code === "NotFound") {
- resolve(false);
- } else {
- reject(err);
- }
- } else {
- resolve(true);
- }
- });
- });
- }
- }
- if (!exists) {
- reject(Error(`Key not found in the datalake:
- Bucket: ${this.datalakeConfig.bucketName}
- BucketPath: ${this.datalakeConfig.bucketPath}
- Key: ${key}`));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement