-
Notifications
You must be signed in to change notification settings - Fork 1
feat: stats and update methods #61
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
73f330b
chore(storage): mark consistency field deprecated
designcode 1149f3e
feat(storage): update object method
designcode 3a6cf10
refactor(storage): explands getBucketInfo to include more fields
designcode 35f2503
feat(storage): implement getStats to expose org level stats
designcode 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
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,86 +1,135 @@ | ||
| import { HeadBucketCommand } from '@aws-sdk/client-s3'; | ||
| import type { HttpResponse } from '@aws-sdk/types'; | ||
| import { TigrisHeaders } from '@shared/index'; | ||
| import { createTigrisClient } from '../tigris-client'; | ||
| import type { TigrisStorageConfig, TigrisStorageResponse } from '../types'; | ||
| import { StorageClass } from './create'; | ||
| import { createStorageClient } from '../http-client'; | ||
|
|
||
| export type GetBucketInfoOptions = { | ||
| config?: TigrisStorageConfig; | ||
| }; | ||
|
|
||
| export type BucketInfoResponse = { | ||
| isSnapshotEnabled: boolean; | ||
| /** | ||
| * @deprecated | ||
| * @see forkInfo.hasChildren | ||
| * This property is deprecated and will be removed in the next major version | ||
| */ | ||
| hasForks: boolean; | ||
| /** | ||
| * @deprecated | ||
| * @see forkInfo.parents[0].bucketName | ||
| * This property is deprecated and will be removed in the next major version | ||
| */ | ||
| sourceBucketName?: string; | ||
| /** | ||
| * @deprecated | ||
| * @see forkInfo.parents[0].snapshot | ||
| * This property is deprecated and will be removed in the next major version | ||
| */ | ||
| sourceBucketSnapshot?: string; | ||
|
|
||
| forkInfo: | ||
| | { | ||
| hasChildren: boolean; | ||
| parents: Array<{ | ||
| bucketName: string; | ||
| forkCreatedAt: Date; | ||
| snapshot: string; | ||
| snapshotCreatedAt: Date; | ||
| }>; | ||
| } | ||
| | undefined; | ||
| settings: { | ||
| allowObjectAcl: boolean; | ||
| defaultTier: StorageClass; | ||
| }; | ||
| sizeInfo: { | ||
| numberOfObjects: number | undefined; | ||
| size: number | undefined; | ||
| numberOfObjectsAllVersions: number | undefined; | ||
| }; | ||
| }; | ||
|
|
||
| type BucketInfoApiResponse = { | ||
| ForkInfo?: { | ||
| HasChildren: boolean; | ||
| Parents: Array<{ | ||
| BucketName: string; | ||
| ForkCreatedAt: string; | ||
| Snapshot: string; | ||
| SnapshotCreatedAt: string; | ||
| }>; | ||
| }; | ||
| name: string; | ||
| storage_class: StorageClass; | ||
| type?: 1; | ||
| tier_sizes: Record<string, number>; | ||
| acl_settings?: { | ||
| allow_object_acl: boolean; | ||
| }; | ||
| estimated_unique_rows?: number; // number of objects | ||
| estimated_size?: number; // estimated size of the bucket in bytes | ||
| estimated_rows?: number; // estimated number of objects in the bucket (all versions) | ||
| }; | ||
|
|
||
| export async function getBucketInfo( | ||
| bucketName: string, | ||
| options?: GetBucketInfoOptions | ||
| ): Promise<TigrisStorageResponse<BucketInfoResponse, Error>> { | ||
| const { data: tigrisClient, error } = createTigrisClient( | ||
| options?.config, | ||
| true | ||
| ); | ||
| const { data: storageHttpClient, error: storageHttpClientError } = | ||
| createStorageClient(options?.config); | ||
|
|
||
| if (error) { | ||
| return { error }; | ||
| if (storageHttpClientError) { | ||
| return { error: storageHttpClientError }; | ||
| } | ||
|
|
||
| const command = new HeadBucketCommand({ Bucket: bucketName }); | ||
| try { | ||
| const response = await storageHttpClient.request< | ||
| unknown, | ||
| BucketInfoApiResponse | ||
| >({ | ||
| method: 'GET', | ||
| path: `/${bucketName}?metadata&with-size=true`, | ||
| headers: { | ||
| Accept: 'application/json', | ||
| }, | ||
| }); | ||
|
|
||
| let headers: Record<string, string> = {}; | ||
| if (response.error) { | ||
| return { error: response.error }; | ||
| } | ||
|
|
||
| command.middlewareStack.add( | ||
| (next) => async (args) => { | ||
| const result = await next(args); | ||
| headers = (result.response as HttpResponse).headers; | ||
| const data = { | ||
| isSnapshotEnabled: response.data.type === 1, | ||
| hasForks: response.data.ForkInfo?.HasChildren ?? false, | ||
| sourceBucketName: response.data.ForkInfo?.Parents?.[0]?.BucketName, | ||
| sourceBucketSnapshot: response.data.ForkInfo?.Parents?.[0]?.Snapshot, | ||
|
|
||
| return result; | ||
| }, | ||
| { step: 'deserialize' } | ||
| ); | ||
| forkInfo: response.data.ForkInfo | ||
| ? { | ||
| hasChildren: response.data.ForkInfo.HasChildren, | ||
| parents: response.data.ForkInfo.Parents?.map((parent) => ({ | ||
| bucketName: parent.BucketName, | ||
| forkCreatedAt: new Date(parent.ForkCreatedAt), | ||
| snapshot: parent.Snapshot, | ||
| snapshotCreatedAt: new Date(parent.SnapshotCreatedAt), | ||
| })) ?? [], | ||
| } | ||
| : undefined, | ||
| settings: { | ||
| allowObjectAcl: response.data.acl_settings?.allow_object_acl ?? false, | ||
| defaultTier: response.data.storage_class as StorageClass, | ||
| }, | ||
| sizeInfo: { | ||
| numberOfObjects: response.data.estimated_unique_rows ?? undefined, | ||
| size: response.data.estimated_size ?? undefined, | ||
| numberOfObjectsAllVersions: response.data.estimated_rows ?? undefined, | ||
| }, | ||
| }; | ||
|
|
||
| try { | ||
| return tigrisClient | ||
| .send(command) | ||
| .then(() => { | ||
| return { | ||
| data: { | ||
| isSnapshotEnabled: | ||
| headers[TigrisHeaders.SNAPSHOT_ENABLED.toLowerCase()] !== | ||
| undefined && | ||
| headers[TigrisHeaders.SNAPSHOT_ENABLED.toLowerCase()] === 'true', | ||
| hasForks: | ||
| headers[TigrisHeaders.HAS_FORKS.toLowerCase()] !== undefined && | ||
| headers[TigrisHeaders.HAS_FORKS.toLowerCase()] === 'true', | ||
| ...(headers[TigrisHeaders.FORK_SOURCE_BUCKET.toLowerCase()] !== | ||
| undefined | ||
| ? { | ||
| sourceBucketName: | ||
| headers[TigrisHeaders.FORK_SOURCE_BUCKET.toLowerCase()], | ||
| } | ||
| : {}), | ||
| ...(headers[ | ||
| TigrisHeaders.FORK_SOURCE_BUCKET_SNAPSHOT.toLowerCase() | ||
| ] !== undefined | ||
| ? { | ||
| sourceBucketSnapshot: | ||
| headers[ | ||
| TigrisHeaders.FORK_SOURCE_BUCKET_SNAPSHOT.toLowerCase() | ||
| ], | ||
| } | ||
| : {}), | ||
| }, | ||
| }; | ||
| }) | ||
| .catch((error) => { | ||
| return { | ||
| error: new Error(`Unable to get bucket info ${error.message}`), | ||
| }; | ||
| }); | ||
| } catch { | ||
| return { error: new Error('Unable to get bucket info') }; | ||
| return { data }; | ||
| } catch (error) { | ||
| return { | ||
| error: error instanceof Error ? error : new Error(String(error)), | ||
| }; | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { PutObjectAclCommand } from '@aws-sdk/client-s3'; | ||
| import { createTigrisClient } from '../tigris-client'; | ||
| import { TigrisHeaders } from '@shared/headers'; | ||
| import type { TigrisStorageConfig, TigrisStorageResponse } from '../types'; | ||
| import { config, missingConfigError } from '../config'; | ||
| import { handleError } from '../utils'; | ||
| import { createStorageClient } from '../http-client'; | ||
|
|
||
| export type UpdateObjectOptions = { | ||
| config?: TigrisStorageConfig; | ||
| key?: string; | ||
| access?: 'public' | 'private'; | ||
| }; | ||
|
|
||
| export type UpdateObjectResponse = { | ||
| path: string; | ||
| }; | ||
|
|
||
| export async function updateObject( | ||
| path: string, | ||
| options?: UpdateObjectOptions | ||
| ): Promise<TigrisStorageResponse<UpdateObjectResponse, Error>> { | ||
| if (!options?.key && !options?.access) { | ||
| return { error: new Error('No update options provided') }; | ||
| } | ||
|
|
||
| const bucket = options?.config?.bucket ?? config.bucket; | ||
|
|
||
| if (!bucket) { | ||
| return missingConfigError('bucket'); | ||
| } | ||
|
|
||
| let currentKey = path; | ||
|
|
||
| // Rename first, so the ACL update targets the correct key | ||
| if (options?.key) { | ||
| const key = options.key; | ||
| const { data: storageHttpClient, error: storageHttpClientError } = | ||
| createStorageClient(options?.config); | ||
|
|
||
| if (storageHttpClientError) { | ||
| return { error: storageHttpClientError }; | ||
| } | ||
|
|
||
| try { | ||
| const response = await storageHttpClient.request({ | ||
| method: 'PUT', | ||
| path: `/${bucket}/${encodeURIComponent(key)}?x-id=CopyObject`, | ||
| headers: { | ||
| [TigrisHeaders.COPY_SOURCE]: `${bucket}/${encodeURIComponent(path)}`, | ||
| [TigrisHeaders.RENAME]: 'true', | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }); | ||
|
|
||
| if (response.error) { | ||
| return { error: response.error }; | ||
| } | ||
|
|
||
| currentKey = key; | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| return handleError(error as Error); | ||
| } | ||
| } | ||
|
|
||
| if (options?.access) { | ||
| const { data: tigrisClient, error } = createTigrisClient(options?.config); | ||
|
|
||
| if (error) { | ||
| return { error }; | ||
| } | ||
|
|
||
| try { | ||
| await tigrisClient.send( | ||
| new PutObjectAclCommand({ | ||
| Bucket: bucket, | ||
| Key: currentKey, | ||
| ACL: options.access === 'public' ? 'public-read' : 'private', | ||
| }) | ||
| ); | ||
| } catch (error) { | ||
| return handleError(error as Error); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| data: { | ||
| path: currentKey, | ||
| }, | ||
| }; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.