diff --git a/src/classes/MinioStorage.js b/src/classes/MinioStorage.js index 4af46b4..ab8c321 100644 --- a/src/classes/MinioStorage.js +++ b/src/classes/MinioStorage.js @@ -3,6 +3,8 @@ // 型定義をインポート const Minio = require('minio'); +// enum を使っている場合 +const { ProcessType } = require('../types/IStorage'); const fs = require('fs'); @@ -31,13 +33,83 @@ class MinioStorage { async uploadFile(filePath, objectKey) { return this.minioClient.fPutObject(this.bucketName, objectKey, filePath) .then(() => { - console.log('File uploaded successfully.'); + console.log('File uploaded successfully.'); }) .catch(err => { console.error('File upload error:', err); throw err; }); } + + /** + * オブジェクトをファイルとして保存(ローカル) + * @param {string} objectKey 取得するオブジェクトキー + * @param {string} destinationPath 保存先ファイルパス + * @returns {Promise} + */ + async downloadFile(objectKey, destinationPath) { + const dataStream = await this.minioClient.getObject(this.bucketName, objectKey); + + return new Promise((resolve, reject) => { + const fileStream = fs.createWriteStream(destinationPath); + dataStream.pipe(fileStream); + dataStream.on('end', () => { + console.log('File Download File end'); + resolve(); + }); + dataStream.on('error', reject); + }); + } + + /** + * オブジェクトをメモリ上に読み込む(Buffer) + * @param {string} objectKey 取得するオブジェクトキー + * @returns {Promise} + */ + async downloadContents(objectKey) { + const dataStream = await this.minioClient.getObject(this.bucketName, objectKey); + + return new Promise((resolve, reject) => { + const chunks = []; + dataStream.on('data', chunk => chunks.push(chunk)); + dataStream.on('end', () => resolve(Buffer.concat(chunks))); + dataStream.on('error', reject); + }); + } + + /** + * 単一オブジェクトを削除する + * @param {string} objectKey 削除するオブジェクトキー + * @returns {Promise} + */ + async delete(objectKey) { + await this.minioClient.removeObject(this.bucketName, objectKey); + console.log('File deleted successfully.'); + } + + + /** + * 署名付きURLを生成する(GET or PUT) + * @param {string} objectKey 対象のオブジェクトキー + * @param {number} processType 処理タイプ(0=GET, 1=PUT) + * @param {{ expiresIn: number }} options 有効期限(秒) + * @returns {Promise} 発行された署名付きURL + */ + async getSignedUrl(objectKey, processType, options) { + if (!options.expiresIn) { + options.expiresIn = 60 * 60; // 1 hours + } + switch (processType) { + case ProcessType.GET: + return this.minioClient.presignedGetObject(this.bucketName, objectKey, options.expiresIn); + case ProcessType.PUT: + return this.minioClient.presignedPutObject(this.bucketName, objectKey, options.expiresIn); + default: + throw new Error('Invalid process type.'); + } + } } + + module.exports = MinioStorage; \ No newline at end of file diff --git a/src/types/IStorage.d.ts b/src/types/IStorage.d.ts index 4f35694..8b5f392 100644 --- a/src/types/IStorage.d.ts +++ b/src/types/IStorage.d.ts @@ -32,7 +32,7 @@ export interface IStorage { * @param destinationPath 保存先ローカルファイルパス * @returns ダウンロードしたファイルの内容(Buffer) */ - download(objectKey: string, destinationPath: string): Promise; + downloadFile(objectKey: string, destinationPath: string): Promise; /** * オブジェクトをメモリ上に直接取得する(ファイル保存しない)