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

Squid Storage

storage bucket 内の file を安全に upload・download します​

Squid の storage feature を使用すると、Squid Client SDK で file を管理でき、任意の client から file と interface できます。seamless な development と user experience を実現するために、Squid Backend SDK を使用して file を保護します。application の作成時に Squid が提供する built-in storage instance を使用するか、独自の AWS storage bucket を接続します。

Squid storage には、以下の connector を通じて access できます。

接続後、Squid Client SDK を介して storage connector を使用できます。

Storage Instance に Access する​

Squid storage instance に access するには、Squid instance で .storage() を call します。built-in storage bucket では parameter なしで method を call します。その他の storage connector では connector ID を渡します。

Client code
squid.storage(); // accesses the built-in storage bucket

squid.storage('YOUR_CONNECTOR_ID'); // accesses a storage connector

File を Upload する​

storage に file を upload するには、uploadFile() method を call します。file の保存先となる storage bucket 内の directory path と、File または BlobAndFileName type の file を渡します。browser では File type を、Node.js application では BlobAndFileName type を使用します。

Client code
await squid.storage().uploadFile('dir/path/in/bucket', yourFile);

BlobAndFileName は次の format です。

{
blob: Blob;
name: string;
}

File Download URL を取得する​

file を download するには、getDownloadUrl() method を使用して download URL を生成します。file への path と、optional で second 単位の expiration time を渡します。default expiration time は 3600 second です。この function は URL を含む object に resolve する promise を返します。

Client code
const urlResponse = await squid.storage().getDownloadUrl('path/in/bucket/image.jpg', 7200);
console.log(urlResponse.url); // "https://prod-us-east-1-squid-storage.s3.amazonaws.com/abcd123..."

File Metadata を取得する​

file の metadata を read するには、file への path を渡して getFileMetadata() method を使用します。

Client code
const metadata = await squid.storage().getFileMetadata('path/in/bucket/image.jpg');
console.log(metadata);
/*
{
filename: 'image.jpg',
size: 120672,
lastModified: Mon Mar 11 2024 17:32:00 GMT-0500 (Central Daylight Time)
}
*/

Directory Content を List 化する​

指定 bucket directory のすべての content を list 化するには、bucket directory path を渡して listDirectoryContents method を使用します。

Client code
const contents = await squid.storage().listDirectoryContents('path/in/bucket');
console.log(contents.directories);
/*
['documents/', 'contacts/']
*/

console.log(contents.files);
/*
[
{
filename: 'test.txt',
absoluteFilePathInBucket: 'path/in/bucket/test.txt',
lastModified: Mon Mar 11 2024 17:32:00 GMT-0500 (Central Daylight Time),
size: 9210
}, {
filename: 'image.jpg',
absoluteFilePathInBucket: 'path/in/bucket/image.jpg',
lastModified: Mon Mar 11 2024 17:34:16 GMT-0500 (Central Daylight Time),
size: 120672
},
]
*/

File を削除する​

file を delete するには、削除する file の path を渡して deleteFile() method を call します。

Client code
await squid.storage().deleteFile('path/in/bucket/image.jpg');

複数の File を削除する​

複数の file を delete するには、file path の array を渡して deleteFiles() method を使用します。

Client code
await squid.storage().deleteFiles(['path/in/bucket/image.jpg', 'path/in/bucket/test.txt']);

Storage Bucket を保護する​

default では、storage bucket に対するすべての action は拒否されます。storage bucket resource への authorized access のみを確保するには、storage security function を使用します。詳細については、storage bucket の保護に関する documentationを参照してください。