Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. import { AWSError, S3 } from "aws-sdk";
  2. import { getJsonConfig, DatalakeConfig } from "./config";
  3.  
  4. export class S3Client {
  5. private config: DatalakeConfig;
  6. private innerClient: S3;
  7.  
  8. public constructor(config: string | DatalakeConfig) {
  9. if (typeof config === "string") {
  10. this.config = getJsonConfig("datalake.reader");
  11. } else {
  12. this.config = config;
  13. }
  14. this.innerClient = new S3({
  15. accessKeyId: this.config.accessKeyId,
  16. secretAccessKey: this.config.secretAccessKey
  17. });
  18. }
  19.  
  20. private withPrefix(path: string): string {
  21. let result = this.config.bucketPath;
  22. if (!result.endsWith('/')) {
  23. result += "/";
  24. }
  25. if (path.includes(result)) {
  26. return path;
  27. }
  28. return result + path;
  29. }
  30.  
  31. public async keyExists(key: string): Promise<boolean> {
  32. const params = {
  33. Bucket: this.config.bucketName,
  34. Key: this.withPrefix(key)
  35. };
  36. return new Promise<boolean>( (resolve, reject) => {
  37. this.innerClient.headObject(params, (err: AWSError) => {
  38. if (err) {
  39. if (err.code === "NotFound") {
  40. resolve(false);
  41. } else {
  42. reject(err);
  43. }
  44. } else {
  45. resolve(true);
  46. }
  47. });
  48. });
  49. }
  50. }
  51.  
  52. if (!exists) {
  53. reject(Error(`Key not found in the datalake:
  54. Bucket: ${this.datalakeConfig.bucketName}
  55. BucketPath: ${this.datalakeConfig.bucketPath}
  56. Key: ${key}`));
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement