-
Notifications
You must be signed in to change notification settings - Fork 4
add object storage #76
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
Open
danielva-monday
wants to merge
10
commits into
master
Choose a base branch
from
add-object-storage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+823
−6
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e7a36d7
[beta] add object storage
danielva-monday 260dd5a
feat(sdk): update lock [beta]
danielva-monday 6c9811c
test(sdk): fix tests
danielva-monday 112837d
test(sdk): fix tests
danielva-monday a2ce539
test(sdk): new beta [beta]
danielva-monday 377d9aa
test(sdk): export types [beta]
danielva-monday c1fe72f
test(sdk): remove type exports
danielva-monday 34f89d1
test(sdk): [beta]
danielva-monday 01d6628
test(sdk): code review fixes [beta]
danielva-monday 5f35043
add new method to get predefined url for upload
maorb-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| export default { name: '@mondaycom/apps-sdk', version: '3.2.1' }; | ||
| export default { name: '@mondaycom/apps-sdk', version: '3.3.0-beta.4' }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { ObjectStorage } from './object-storage'; | ||
|
|
||
| export { | ||
| ObjectStorage | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| import { Bucket, File, Storage } from '@google-cloud/storage'; | ||
|
|
||
| import { InternalServerError } from 'errors/apps-sdk-error'; | ||
| import { TIME_IN_MILLISECOND } from 'lib/utils/time-enum'; | ||
| import { | ||
| DeleteFileResponse, | ||
| DownloadFileResponse, | ||
| FileInfo, | ||
| GetFileInfoResponse, | ||
| ListFilesOptions, | ||
| ListFilesResponse, | ||
| PresignedUrlOptions, | ||
| PresignedUrlResponse, | ||
| UploadFileOptions, | ||
| UploadFileResponse | ||
| } from 'types/object-storage'; | ||
| import { Logger } from 'utils/logger'; | ||
|
|
||
| const logger = new Logger('ObjectStorage', { mondayInternal: true }); | ||
|
|
||
| export class ObjectStorage { | ||
| private storage: Storage; | ||
| private bucketName: string; | ||
|
|
||
| constructor() { | ||
| if (!process.env.OBJECT_STORAGE_BUCKET) { | ||
| throw new InternalServerError('OBJECT_STORAGE_BUCKET is not set'); | ||
| } | ||
|
|
||
| this.storage = new Storage(); | ||
| this.bucketName = process.env.OBJECT_STORAGE_BUCKET; | ||
| logger.info(`ObjectStorage initialized with bucket: ${this.bucketName}`); | ||
| } | ||
|
|
||
| private getBucket(): Bucket { | ||
| return this.storage.bucket(this.bucketName); | ||
| } | ||
|
|
||
| private handleError(error: unknown, operation: string): { errorMessage: string; errorObj: Error } { | ||
| const errorMessage = error instanceof Error ? error.message : String(error); | ||
| const errorObj = error instanceof Error ? error : new Error(String(error)); | ||
| logger.error(`Failed to ${operation}:`, { error: errorObj }); | ||
| return { errorMessage, errorObj }; | ||
| } | ||
|
|
||
| async uploadFile(fileName: string, content: Buffer | string, options: UploadFileOptions = {}): Promise<UploadFileResponse> { | ||
| try { | ||
| const bucket = this.getBucket(); | ||
| const file: File = bucket.file(fileName); | ||
|
|
||
| const uploadOptions = { | ||
| metadata: { | ||
| contentType: options.contentType || 'application/octet-stream', | ||
| metadata: options.metadata || {} | ||
| } | ||
| }; | ||
|
|
||
| await file.save(content, uploadOptions); | ||
|
|
||
| const fileUrl = `gs://${this.bucketName}/${fileName}`; | ||
|
|
||
| logger.info(`File uploaded successfully: ${fileName}`); | ||
|
|
||
| return { | ||
| success: true, | ||
| fileName, | ||
| fileUrl | ||
| }; | ||
| } catch (error) { | ||
| const { errorMessage } = this.handleError(error, 'upload file'); | ||
| return { | ||
| success: false, | ||
| error: `Failed to upload file: ${errorMessage}` | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| async downloadFile(fileName: string): Promise<DownloadFileResponse> { | ||
| try { | ||
| const bucket = this.getBucket(); | ||
| const file: File = bucket.file(fileName); | ||
|
|
||
| const [exists] = await file.exists(); | ||
| if (!exists) { | ||
| return { | ||
| success: false, | ||
| error: 'File not found' | ||
| }; | ||
| } | ||
|
|
||
| const [content] = await file.download(); | ||
| const [metadata] = await file.getMetadata(); | ||
|
|
||
| return { | ||
| success: true, | ||
| content, | ||
| contentType: metadata.contentType || 'application/octet-stream' | ||
| }; | ||
| } catch (error) { | ||
| const { errorMessage } = this.handleError(error, 'download file'); | ||
| return { | ||
| success: false, | ||
| error: `Failed to download file: ${errorMessage}` | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| async deleteFile(fileName: string): Promise<DeleteFileResponse> { | ||
| try { | ||
| const bucket = this.getBucket(); | ||
| const file: File = bucket.file(fileName); | ||
|
|
||
| const [exists] = await file.exists(); | ||
| if (!exists) { | ||
| return { | ||
| success: false, | ||
| error: 'File not found' | ||
| }; | ||
| } | ||
|
|
||
| await file.delete(); | ||
|
|
||
| logger.info(`File deleted successfully: ${fileName}`); | ||
|
|
||
| return { success: true }; | ||
| } catch (error) { | ||
| const { errorMessage } = this.handleError(error, 'delete file'); | ||
| return { | ||
| success: false, | ||
| error: `Failed to delete file: ${errorMessage}` | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| async listFiles(options: ListFilesOptions = {}): Promise<ListFilesResponse> { | ||
| try { | ||
| const bucket = this.getBucket(); | ||
|
|
||
| const queryOptions = { | ||
| maxResults: options.maxResults || 100, | ||
| ...(options.prefix && { prefix: options.prefix }), | ||
| ...(options.pageToken && { pageToken: options.pageToken }) | ||
| }; | ||
|
|
||
| const [files, , apiResponse] = await bucket.getFiles(queryOptions); | ||
|
|
||
| const fileInfos: Array<FileInfo> = files.map((file: File) => ({ | ||
| name: file.name, | ||
| size: parseInt(String(file.metadata.size || '0'), 10) || 0, | ||
| contentType: file.metadata.contentType || 'application/octet-stream', | ||
| lastModified: new Date(file.metadata.updated || Date.now()), | ||
| etag: file.metadata.etag || '', | ||
| metadata: Object.fromEntries( | ||
| Object.entries(file.metadata.metadata || {}).map(([key, value]) => [ | ||
| key, | ||
| String(value || '') | ||
| ]) | ||
| ) | ||
| })); | ||
|
|
||
| return { | ||
| success: true, | ||
| files: fileInfos, | ||
| nextPageToken: (apiResponse as { nextPageToken?: string })?.nextPageToken | ||
| }; | ||
| } catch (error) { | ||
| const { errorMessage } = this.handleError(error, 'list files'); | ||
| return { | ||
| success: false, | ||
| error: `Failed to list files: ${errorMessage}` | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| async getFileInfo(fileName: string): Promise<GetFileInfoResponse> { | ||
| try { | ||
| const bucket = this.getBucket(); | ||
| const file: File = bucket.file(fileName); | ||
|
|
||
| const [exists] = await file.exists(); | ||
| if (!exists) { | ||
| return { | ||
| success: false, | ||
| error: 'File not found' | ||
| }; | ||
| } | ||
|
|
||
| const [metadata] = await file.getMetadata(); | ||
|
|
||
| const fileInfo: FileInfo = { | ||
| name: file.name, | ||
| size: parseInt(String(metadata.size || '0'), 10) || 0, | ||
| contentType: metadata.contentType || 'application/octet-stream', | ||
| lastModified: new Date(metadata.updated || Date.now()), | ||
| etag: metadata.etag || '', | ||
| metadata: Object.fromEntries( | ||
| Object.entries(metadata.metadata || {}).map(([key, value]) => [ | ||
| key, | ||
| String(value || '') | ||
| ]) | ||
| ) | ||
| }; | ||
|
|
||
| return { | ||
| success: true, | ||
| fileInfo | ||
| }; | ||
| } catch (error) { | ||
| const { errorMessage } = this.handleError(error, 'get file info'); | ||
| return { | ||
| success: false, | ||
| error: `Failed to get file info: ${errorMessage}` | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| async getPresignedUploadUrl(fileName: string, options: PresignedUrlOptions = {}): Promise<PresignedUrlResponse> { | ||
| try { | ||
| const bucket = this.getBucket(); | ||
| const file: File = bucket.file(fileName); | ||
|
|
||
| const fifteenMinutesFromNow = new Date(Date.now() + TIME_IN_MILLISECOND.MINUTE * 15); | ||
| const expires = options.expires || fifteenMinutesFromNow; | ||
|
|
||
| const signedUrlOptions = { | ||
| version: 'v4' as const, | ||
| action: 'write' as const, | ||
| expires, | ||
| ...(options.contentType && { | ||
| contentType: options.contentType | ||
| }) | ||
| }; | ||
|
|
||
| const [presignedUrl] = await file.getSignedUrl(signedUrlOptions); | ||
|
|
||
| logger.info(`Presigned upload URL generated for file: ${fileName}`); | ||
|
|
||
| return { | ||
| success: true, | ||
| presignedUrl, | ||
| fileName | ||
| }; | ||
| } catch (error) { | ||
| const { errorMessage } = this.handleError(error, 'generate presigned upload URL'); | ||
| return { | ||
| success: false, | ||
| error: `Failed to generate presigned upload URL: ${errorMessage}` | ||
| }; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| export type BaseResponse = { | ||
| success: boolean; | ||
| error?: string; | ||
| } | ||
|
|
||
| export type UploadFileOptions = { | ||
| contentType?: string; | ||
| metadata?: Record<string, string>; | ||
| } | ||
|
|
||
| export type UploadFileResponse = BaseResponse & { | ||
| fileName?: string; | ||
| fileUrl?: string; | ||
| } | ||
|
|
||
| export type DownloadFileResponse = BaseResponse & { | ||
| content?: Buffer; | ||
| contentType?: string; | ||
| } | ||
|
|
||
| export type DeleteFileResponse = BaseResponse; | ||
|
|
||
| export type ListFilesOptions = { | ||
| prefix?: string; | ||
| maxResults?: number; | ||
| pageToken?: string; | ||
| } | ||
|
|
||
| export type FileInfo = { | ||
| name: string; | ||
| size: number; | ||
| contentType: string; | ||
| lastModified: Date; | ||
| etag: string; | ||
| metadata: Record<string, string>; | ||
| } | ||
|
|
||
| export type ListFilesResponse = BaseResponse & { | ||
| files?: Array<FileInfo>; | ||
| nextPageToken?: string; | ||
| } | ||
|
|
||
| export type GetFileInfoResponse = BaseResponse & { | ||
| fileInfo?: FileInfo; | ||
| } | ||
|
|
||
| export type PresignedUrlOptions = { | ||
| expires?: Date; | ||
| contentType?: string; | ||
| } | ||
|
|
||
| export type PresignedUrlResponse = BaseResponse & { | ||
| presignedUrl?: string; | ||
| fileName?: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maorb-dev add another method that will provide the dev with a pre-signed url for uploading
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once added, also expose it with the mcode-sdk-api, simlar to this: https://github.com/DaPulse/mcode-sdk-api/pull/35/files
and also generate the python + PHP clients