Improve object storage key

This commit is contained in:
syuilo 2018-08-16 21:23:31 +09:00
parent 9d6641be3a
commit 7f9a35d7ac
2 changed files with 14 additions and 10 deletions

View file

@ -35,20 +35,19 @@ async function save(path: string, name: string, type: string, hash: string, size
if (config.drive && config.drive.storage == 'minio') { if (config.drive && config.drive.storage == 'minio') {
const minio = new Minio.Client(config.drive.config); const minio = new Minio.Client(config.drive.config);
const id = uuid.v4(); const key = `${config.drive.prefix}/${uuid.v4()}/${name}`;
const obj = `${config.drive.prefix}/${id}`; const thumbnailKey = `${config.drive.prefix}/${uuid.v4()}/${name}.thumbnail.jpg`;
const thumbnailObj = `${obj}-thumbnail`;
const baseUrl = config.drive.baseUrl const baseUrl = config.drive.baseUrl
|| `${ config.drive.config.secure ? 'https' : 'http' }://${ config.drive.config.endPoint }${ config.drive.config.port ? ':' + config.drive.config.port : '' }/${ config.drive.bucket }`; || `${ config.drive.config.secure ? 'https' : 'http' }://${ config.drive.config.endPoint }${ config.drive.config.port ? ':' + config.drive.config.port : '' }/${ config.drive.bucket }`;
await minio.putObject(config.drive.bucket, obj, fs.createReadStream(path), size, { await minio.putObject(config.drive.bucket, key, fs.createReadStream(path), size, {
'Content-Type': type, 'Content-Type': type,
'Cache-Control': 'max-age=31536000, immutable' 'Cache-Control': 'max-age=31536000, immutable'
}); });
if (thumbnail) { if (thumbnail) {
await minio.putObject(config.drive.bucket, thumbnailObj, thumbnail, size, { await minio.putObject(config.drive.bucket, thumbnailKey, thumbnail, size, {
'Content-Type': 'image/jpeg', 'Content-Type': 'image/jpeg',
'Cache-Control': 'max-age=31536000, immutable' 'Cache-Control': 'max-age=31536000, immutable'
}); });
@ -58,10 +57,11 @@ async function save(path: string, name: string, type: string, hash: string, size
withoutChunks: true, withoutChunks: true,
storage: 'minio', storage: 'minio',
storageProps: { storageProps: {
id: id key: key,
thumbnailKey: thumbnailKey
}, },
url: `${ baseUrl }/${ obj }`, url: `${ baseUrl }/${ key }`,
thumbnailUrl: thumbnail ? `${ baseUrl }/${ thumbnailObj }` : null thumbnailUrl: thumbnail ? `${ baseUrl }/${ thumbnailKey }` : null
}); });
const file = await DriveFile.insert({ const file = await DriveFile.insert({

View file

@ -7,11 +7,15 @@ export default async function(file: IDriveFile, isExpired = false) {
if (file.metadata.storage == 'minio') { if (file.metadata.storage == 'minio') {
const minio = new Minio.Client(config.drive.config); const minio = new Minio.Client(config.drive.config);
const obj = `${config.drive.prefix}/${file.metadata.storageProps.id}`; // 後方互換性のため、file.metadata.storageProps.key があるかどうかチェックしています。
// 将来的には const obj = file.metadata.storageProps.key; とします。
const obj = file.metadata.storageProps.key ? file.metadata.storageProps.key : `${config.drive.prefix}/${file.metadata.storageProps.id}`;
await minio.removeObject(config.drive.bucket, obj); await minio.removeObject(config.drive.bucket, obj);
if (file.metadata.thumbnailUrl) { if (file.metadata.thumbnailUrl) {
const thumbnailObj = `${config.drive.prefix}/${file.metadata.storageProps.id}-thumbnail`; // 後方互換性のため、file.metadata.storageProps.thumbnailKey があるかどうかチェックしています。
// 将来的には const thumbnailObj = file.metadata.storageProps.thumbnailKey; とします。
const thumbnailObj = file.metadata.storageProps.thumbnailKey ? file.metadata.storageProps.thumbnailKey : `${config.drive.prefix}/${file.metadata.storageProps.id}-thumbnail`;
await minio.removeObject(config.drive.bucket, thumbnailObj); await minio.removeObject(config.drive.bucket, thumbnailObj);
} }
} }