Skip to content
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
121 changes: 121 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@
},
"license": "MIT",
"dependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@aws-sdk/client-s3": "^3.859.0",
"@aws-sdk/lib-storage": "^3.859.0",
"@aws-sdk/s3-request-presigner": "^3.864.0",
"@smithy/signature-v4": "^4.2.4",
"dotenv": "^17.2.1"
}
}
118 changes: 118 additions & 0 deletions packages/storage/src/lib/bucket/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { TigrisHeaders } from '@shared/headers';
import { createStorageClient } from '../http-client';
import type { TigrisStorageConfig, TigrisStorageResponse } from '../types';

export type UpdateBucketOptions = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some other options as well like object notification, object lifecycle. But we can add those incrementally.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I guess I will add those here in this same PR.

// access and sharing settings
access?: 'public' | 'private';
allowObjectAcl?: boolean;
disableDirectoryListing?: boolean;

// storage settings
// consistency?: 'strict' | 'default';
regions?: string | string[];
cacheControl?: string;

// data management settings
// TODO: Data Migration, TTL Config, Objects Lifecycle

// custom domain
customDomain?: string;

// cors settings
// TODO: Additional Headers, CORS Rules

// notification settings
// TODO: enableNotifications?: boolean;

// deletion settings
enableDeleteProtection?: boolean;

config?: Omit<TigrisStorageConfig, 'bucket'>;
};

export type UpdateBucketResponse = {
bucket: string;
updated: boolean;
};

export async function updateBucket(
bucketName: string,
options?: UpdateBucketOptions
): Promise<TigrisStorageResponse<UpdateBucketResponse, Error>> {
const { data: client, error } = createStorageClient(options?.config);

if (error || !client) {
return { error };
}

const body: Record<string, unknown> = {};
const headers: Record<string, string> = {};

// access and sharing settings
if (options?.access !== undefined) {
headers[TigrisHeaders.ACL] =
options.access === 'public' ? 'public-read' : 'private';
}

if (options?.allowObjectAcl !== undefined) {
body.acl_settings = { allow_object_acl: options.allowObjectAcl };
}

if (options?.disableDirectoryListing !== undefined) {
headers[TigrisHeaders.ACL_LIST_OBJECTS] =
options.disableDirectoryListing === true ? 'false' : 'true';
}

// storage settings
/*if (options?.consistency !== undefined) {
body.consistent = options.consistency === 'strict' ? true : false;
}*/

if (options?.regions !== undefined) {
body.object_regions = Array.isArray(options.regions)
? options.regions.join(',')
: options.regions;
}

if (options?.cacheControl !== undefined) {
body.cache_control = options.cacheControl;
}

// custom domain
if (options?.customDomain !== undefined) {
body.website = { domain_name: options.customDomain };
}

// deletion settings
if (options?.enableDeleteProtection !== undefined) {
body.protection = { protected: options.enableDeleteProtection };
}

const response = await client.request<
Record<string, unknown>,
{ status: 'success' | 'error'; message?: string }
>({
method: 'PATCH',
path: `/${bucketName}`,
body,
headers,
});

if (response.error) {
return { error: response.error };
}

if (response.data.status === 'error') {
return {
error: new Error(response.data.message ?? 'Failed to update bucket'),
};
}

return {
data: {
bucket: bucketName,
updated: true,
},
};
}
36 changes: 36 additions & 0 deletions packages/storage/src/lib/http-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createTigrisHttpClient, type TigrisHttpClient } from '@shared/index';
import { config } from './config';
import type { TigrisStorageConfig, TigrisStorageResponse } from './types';

function getStorageEndpoint(options?: TigrisStorageConfig): string {
return options?.endpoint ?? config.endpoint ?? 'https://t3.storage.dev';
}

export function createStorageClient(
options?: TigrisStorageConfig
): TigrisStorageResponse<TigrisHttpClient, Error> {
const sessionToken = options?.sessionToken ?? config.sessionToken;
const organizationId = options?.organizationId ?? config.organizationId;
const accessKeyId = options?.accessKeyId ?? config.accessKeyId;
const secretAccessKey = options?.secretAccessKey ?? config.secretAccessKey;

// Allow either session token, authorization, or credentials
const hasCredentials = accessKeyId && secretAccessKey;
if (!sessionToken && !hasCredentials) {
return {
error: new Error('Session token or credentials are required'),
};
}

if (sessionToken && (!organizationId || organizationId === '')) {
return { error: new Error('Organization ID is required') };
}

return createTigrisHttpClient({
baseUrl: getStorageEndpoint(options),
sessionToken,
organizationId,
accessKeyId,
secretAccessKey,
});
}
5 changes: 5 additions & 0 deletions packages/storage/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export {
type ListBucketSnapshotsOptions,
type ListBucketSnapshotsResponse,
} from './lib/bucket/snapshot';
export {
updateBucket,
type UpdateBucketOptions,
type UpdateBucketResponse,
} from './lib/bucket/update';
export { get, type GetOptions, type GetResponse } from './lib/object/get';
export { head, type HeadOptions, type HeadResponse } from './lib/object/head';
export {
Expand Down
3 changes: 3 additions & 0 deletions shared/headers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export enum TigrisHeaders {
ACL = 'X-Amz-Acl',
ACL_LIST_OBJECTS = 'X-Amz-Acl-Public-List-Objects-Enabled',
AUTHORIZATION = 'authorization',
SESSION_TOKEN = 'x-amz-security-token',
NAMESPACE = 'X-Tigris-Namespace',
STORAGE_CLASS = 'X-Amz-Storage-Class',
Expand Down
Loading