メインコンテンツまでスキップ

ストレージの保護

ストレージバケットを保護し、承認されたユーザーのみがファイルにアクセスできるようにする @secureStorage デコレーター。

Squid のストレージ機能を使うと、Squid Client SDK でファイルを管理でき、どのクライアントからでもファイルを操作できます。Squid Client SDK で Squid ストレージを使用する方法の詳細は、Squid ストレージのドキュメントを参照してください。

Squid ストレージバケットの integration を保護するには、Squid backend の SquidService クラス内で @secureStorage デコレーターを使用し、integration ID とアクションの種類を渡します。次のコードは、組み込みストレージバケットへのフルアクセスを許可します。

Backend code
import { secureStorage, SquidService } from '@squidcloud/backend';

@secureStorage('all', 'built_in_storage')
allowAllAccessToBuiltInStorage(): boolean {
return true;
}

別のストレージ integration の Squid ストレージバケットを保護するには、デコレーターに integration ID を指定します。

Backend code
import { secureStorage, SquidService } from '@squidcloud/backend';

@secureStorage('all', 'YOUR_STORAGE_INTEGRATION_ID')
allowAllAccessToStorageIntegration(): boolean {
return true;
}

利用可能なアクション種別は次のとおりです。

'read'

'read' アクションには、メタデータの読み取り、ダウンロード URL の生成、ファイルのダウンロード、ディレクトリ内容の一覧表示が含まれます。

'write'

'write' アクションには、新規ファイルの挿入、既存ファイルの更新、ファイルの削除が含まれます。

'insert'

'insert' アクションは新規ファイルの挿入を許可しますが、既存ファイルの内容の更新やファイルの削除は許可しません。

'delete'

'delete' アクションは既存ファイルの削除を許可します。

'all'

'all' アクションには、利用可能なバケットアクションがすべて含まれます。

書き込みの保護

次の関数は、認証済みユーザーが組み込みストレージバケットに対してファイルのアップロード、更新、削除を行えるようにします。

Backend code
import { secureStorage, SquidService } from '@squidcloud/backend';

@secureStorage('write', 'built_in_storage')
allowAuthenticatedWrites(): boolean {
return this.isAuthenticated();
}

読み取りの保護

ディレクトリ名の読み取り、ファイルメタデータの読み取り、ダウンロード URL の生成を保護するには、'read' アクション種別を使用します。次の関数は、ファイルパスがユーザー ID の場合にユーザーがダウンロード URL を生成できるようにします。

Backend code
import { secureStorage, SquidService, StorageContext } from '@squidcloud/backend';

@secureStorage('read', 'built_in_storage')
allowReadUserFiles(context: StorageContext): boolean {
// Validate if the requested action is to get a download URL
if (context.functionality !== 'getDownloadUrl') {
return false;
}

const userId = this.getUserAuth()?.userId;
if (!userId) return false;

// Check any paths the user is trying to read to verify they're in the user's directory
for (const path of context.pathsInBucket) {
if (if (!path.startsWith(`user/${userId}`)) {
return false
}
}
return true;
}

関数のパラメータに StorageContext オブジェクトを含めることで、セキュリティ関数はクライアントが実行したいアクションに関する情報へアクセスできます。次は StorageContext オブジェクトの例です。

{
integrationId: 'built_in_storage',
pathsInBucket: [ 'test/path/img.jpg' ],
action: 'read',
functionality: 'getFileMetadata'
}