loopback 4.0如何使用存储组件?
how to use storage components in loopback 4.0?
我正在使用环回 3.0 中的存储组件来访问云存储。但我需要如何在环回 4.0.The 中实现 link 以使其在 3.0.
中成为样本
来自 LoopBack 团队的问候!
我们尚未研究 loopback-component-storage 与 LoopBack 版本 4 的集成。
由于存储组件的行为更像是连接器而不是组件,我认为应该可以通过 @loopback/service-proxy
层在 LoopBack 4 中使用它。有关文档,请参阅 Calling other APIS and web services。该页面上的代码片段以 loopback-connector-rest
为例,您将改用 loopback-component-storage
。
一旦您可以从 JavaScript/TypeScript 访问存储组件,您还需要公开 REST API。 LoopBack 4 不提供任何内置控制器,您必须自己编写一个。请参阅 storage-service.js 中 LB3 远程方法的定义,您将需要构建控制器方法以接受相同的参数,然后在后台调用服务代理。
如果您愿意研究此集成,那么最好在 loopback-next 中打开一个新的 GitHub 问题,我们可以在其中进行结构化对话。
我将与社区分享我的实现,因为即使 Miroslav Bajtoš is the key to do that, the documentation suggested is not very clear, I refer to Calling other APIS and web services 的答案。
- 首先 我使用
lb4
命令 cli 工具创建了一个新的 DataSource 并且我用与我相同的键填充了 json
文件用于环回 3 数据源。
// storage.datasource.ts
import { inject } from '@loopback/core';
import { juggler } from '@loopback/service-proxy';
import * as config from './storage-gc.datasource.json';
export class StorageGCDataSource extends juggler.DataSource {
static dataSourceName = 'StorageGC';
constructor(
@inject('datasources.config.StorageGC', { optional: true })
dsConfig: object = config,
) {
super(dsConfig);
}
}
// storage.datasource.json
{
"name": "Storage",
"connector": "loopback-component-storage",
"provider": "google",
"keyFilename": "your-project-key.json",
"projectId": "your-project-id",
"nameConflict": "makeUnique"
}
// Note: your-project-key.json is in the root folder at the same level that tsconfig.json
记得需要安装loopback-component-storage运行
npm install --save loopback-component-storage
- 第二位 我使用
lb4
命令 cli 工具创建了两个新模型。这些模型是:Container
和 File
.
// container.model.ts
import {Entity, model, property} from '@loopback/repository';
@model()
export class Container extends Entity {
@property({
type: 'string',
required: true,
})
name: string;
constructor(data?: Partial<Container>) {
super(data);
}
}
// file.model.ts
import { Entity, model, property } from '@loopback/repository';
@model()
export class File extends Entity {
@property({
type: 'string',
required: true,
})
name: string;
@property({
type: 'string',
})
type?: string;
@property({
type: 'string',
})
url?: string;
constructor(data?: Partial<File>) {
super(data);
}
}
- 第三名 我在
src/
目录中创建了一个名为 interfaces
的新文件夹。在这个新文件夹中,我创建了一个 index.ts
(主要是为了遵循导出约定)和一个 storage.interface.ts
(这是重要文件)
// index.ts
export * from './storage.interface';
// storage.interface.ts
import { Container, File } from "../models";
export type Callback<T> = (err: Error | null, reply: T) => void;
export interface IStorageService {
// container methods
createContainer(container: Partial<Container>, cb: Callback<Container>): void;
destroyContainer(containerName: string, cb: Callback<boolean>): void;
getContainers(cb: Callback<Container[]>): void;
getContainer(containerName: string, cb: Callback<Container>): void;
// file methods
getFiles(containerName: string, options: Object, cb: Callback<File[]>): void;
getFile(containerName: string, fileName: string, cb: Callback<File>): void;
removeFile(containerName: string, fileName: string, cb: Callback<boolean>): void;
// main methods
upload(containerName: string, req: any, res: any, options: Object, cb: Callback<any>): void;
download(containerName: string, fileName: string, req: any, res: any, cb: Callback<any>): void;
}
- 第四位我创建了
container.controller.ts
文件,但是在这一步之前你需要安装@loopback/service-proxy 运行 以下命令:
npm install --save @loopback/service-proxy
// storage-gc.controller.ts
import { inject } from '@loopback/core';
import { serviceProxy } from '@loopback/service-proxy';
import { post, requestBody, del, param, get, getFilterSchemaFor, Request, Response, RestBindings } from '@loopback/rest';
import { Filter } from '@loopback/repository';
import { promisify } from 'util';
import { IStorageService } from '../interfaces';
import { Container, File } from '../models';
export class StorageGcController {
@serviceProxy('StorageGC') // StorageGC is the name of the datasoruce
private storageGcSvc: IStorageService;
constructor(@inject(RestBindings.Http.REQUEST) public request: Request,
@inject(RestBindings.Http.RESPONSE) public response: Response) { }
@post('/containers', {
responses: {
'200': {
description: 'Container model instance',
content: { 'application/json': { schema: { 'x-ts-type': Container } } },
},
},
})
async createContainer(@requestBody() container: Container): Promise<Container> {
const createContainer = promisify(this.storageGcSvc.createContainer);
return await createContainer(container);
}
@get('/containers', {
responses: {
'200': {
description: 'Array of Containers model instances',
content: {
'application/json': {
schema: { type: 'array', items: { 'x-ts-type': Container } },
},
},
},
},
})
async findContainer(@param.query.object('filter', getFilterSchemaFor(Container)) filter?: Filter): Promise<Container[]> {
const getContainers = promisify(this.storageGcSvc.getContainers);
return await getContainers();
}
@get('/containers/{containerName}', {
responses: {
'200': {
description: 'Container model instance',
content: { 'application/json': { schema: { 'x-ts-type': Container } } },
},
},
})
async findContainerByName(@param.path.string('containerName') containerName: string): Promise<Container> {
const getContainer = promisify(this.storageGcSvc.getContainer);
return await getContainer(containerName);
}
@del('/containers/{containerName}', {
responses: {
'204': {
description: 'Container DELETE success',
},
},
})
async deleteContainerByName(@param.path.string('containerName') containerName: string): Promise<boolean> {
const destroyContainer = promisify(this.storageGcSvc.destroyContainer);
return await destroyContainer(containerName);
}
@get('/containers/{containerName}/files', {
responses: {
'200': {
description: 'Array of Files model instances belongs to container',
content: {
'application/json': {
schema: { type: 'array', items: { 'x-ts-type': File } },
},
},
},
},
})
async findFilesInContainer(@param.path.string('containerName') containerName: string,
@param.query.object('filter', getFilterSchemaFor(Container)) filter?: Filter): Promise<File[]> {
const getFiles = promisify(this.storageGcSvc.getFiles);
return await getFiles(containerName, {});
}
@get('/containers/{containerName}/files/{fileName}', {
responses: {
'200': {
description: 'File model instances belongs to container',
content: { 'application/json': { schema: { 'x-ts-type': File } } },
},
},
})
async findFileInContainer(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<File> {
const getFile = promisify(this.storageGcSvc.getFile);
return await getFile(containerName, fileName);
}
@del('/containers/{containerName}/files/{fileName}', {
responses: {
'204': {
description: 'File DELETE from Container success',
},
},
})
async deleteFileInContainer(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<boolean> {
const removeFile = promisify(this.storageGcSvc.removeFile);
return await removeFile(containerName, fileName);
}
@post('/containers/{containerName}/upload', {
responses: {
'200': {
description: 'Upload a Files model instances into Container',
content: { 'application/json': { schema: { 'x-ts-type': File } } },
},
},
})
async upload(@param.path.string('containerName') containerName: string): Promise<File> {
const upload = promisify(this.storageGcSvc.upload);
return await upload(containerName, this.request, this.response, {});
}
@get('/containers/{containerName}/download/{fileName}', {
responses: {
'200': {
description: 'Download a File within specified Container',
content: { 'application/json': { schema: { 'x-ts-type': Object } } },
},
},
})
async download(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<any> {
const download = promisify(this.storageGcSvc.download);
return await download(containerName, fileName, this.request, this.response);
}
}
通过这些步骤,您的存储组件,或者更准确地说是连接器,应该可以按预期工作。但是按照 Calling other APIS and web services 指南的说明改进集成测试的实现,你应该在控制器中使用提供者而不是 @serviceProxy
装饰器,为此我在里面创建了一个名为 providers
的新文件夹包含以下两个文件的 src/
文件夹:
// index.ts
export * from './storage-service.provider';
// storage-gc-service.provider.ts
import { getService, juggler } from '@loopback/service-proxy';
import { Provider } from '@loopback/core';
import { StorageGCDataSource } from '../datasources/storage-gc.datasource';
import { IStorageService } from '../interfaces';
export class StorageGCServiceProvider implements Provider<IStorageService> {
constructor(
protected dataSource: juggler.DataSource = new StorageGCDataSource()
/* I try to change the line above in the same way that documentation show,
as follows:
@inject('datasources.StorageGC')
protected dataSource: juggler.DataSource = new StorageGCDataSource()
and also, in the same way that repositories
@inject('datasources.StorageGC')
protected dataSource: StorageGCDataSource
but always return:
`Error: Cannot resolve injected arguments for StorageGCServiceProvider.[0]:
The arguments[0] is not decorated for dependency injection, but a value is
not supplied`
*/
) { }
value(): Promise<IStorageService> {
return getService(this.dataSource);
}
}
之后,您需要按以下方式修改您的src/index.ts
和您的storage-gc.controller.ts
// src/index.ts
import { ApplicationConfig } from '@loopback/core';
import { HocicosCuriososApp } from './application';
import { StorageGCServiceProvider } from './providers';
export { HocicosCuriososApp };
export async function main(options: ApplicationConfig = {}) {
const app = new HocicosCuriososApp(options);
/* Add this line, it add a service to the app after that you can
call them in the controller with dependency injection, like:
@inject('services.StorageGCService') */
app.serviceProvider(StorageGCServiceProvider);
await app.boot();
await app.start();
const url = app.restServer.url;
console.log(`Server is running at ${url}`);
console.log(`Try ${url}/ping`);
return app;
}
// storage-gc.controller.ts
import { inject } from '@loopback/core';
import { post, requestBody, del, param, get, getFilterSchemaFor, Request, Response, RestBindings } from '@loopback/rest';
import { Filter } from '@loopback/repository';
import { promisify } from 'util';
import { IStorageService } from '../interfaces';
import { Container, File } from '../models';
export class StorageGcController {
@inject('services.StorageGCService')
private storageGcSvc: IStorageService;
constructor(@inject(RestBindings.Http.REQUEST) public request: Request,
@inject(RestBindings.Http.RESPONSE) public response: Response) { }
@post('/containers', {
responses: {
'200': {
description: 'Container model instance',
content: { 'application/json': { schema: { 'x-ts-type': Container } } },
},
},
})
async createContainer(@requestBody() container: Container): Promise<Container> {
const createContainer = promisify(this.storageGcSvc.createContainer);
return await createContainer(container);
}
@get('/containers', {
responses: {
'200': {
description: 'Array of Containers model instances',
content: {
'application/json': {
schema: { type: 'array', items: { 'x-ts-type': Container } },
},
},
},
},
})
async findContainer(@param.query.object('filter', getFilterSchemaFor(Container)) filter?: Filter): Promise<Container[]> {
const getContainers = promisify(this.storageGcSvc.getContainers);
return await getContainers();
}
@get('/containers/{containerName}', {
responses: {
'200': {
description: 'Container model instance',
content: { 'application/json': { schema: { 'x-ts-type': Container } } },
},
},
})
async findContainerByName(@param.path.string('containerName') containerName: string): Promise<Container> {
const getContainer = promisify(this.storageGcSvc.getContainer);
return await getContainer(containerName);
}
@del('/containers/{containerName}', {
responses: {
'204': {
description: 'Container DELETE success',
},
},
})
async deleteContainerByName(@param.path.string('containerName') containerName: string): Promise<boolean> {
const destroyContainer = promisify(this.storageGcSvc.destroyContainer);
return await destroyContainer(containerName);
}
@get('/containers/{containerName}/files', {
responses: {
'200': {
description: 'Array of Files model instances belongs to container',
content: {
'application/json': {
schema: { type: 'array', items: { 'x-ts-type': File } },
},
},
},
},
})
async findFilesInContainer(@param.path.string('containerName') containerName: string,
@param.query.object('filter', getFilterSchemaFor(Container)) filter?: Filter): Promise<File[]> {
const getFiles = promisify(this.storageGcSvc.getFiles);
return await getFiles(containerName, {});
}
@get('/containers/{containerName}/files/{fileName}', {
responses: {
'200': {
description: 'File model instances belongs to container',
content: { 'application/json': { schema: { 'x-ts-type': File } } },
},
},
})
async findFileInContainer(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<File> {
const getFile = promisify(this.storageGcSvc.getFile);
return await getFile(containerName, fileName);
}
@del('/containers/{containerName}/files/{fileName}', {
responses: {
'204': {
description: 'File DELETE from Container success',
},
},
})
async deleteFileInContainer(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<boolean> {
const removeFile = promisify(this.storageGcSvc.removeFile);
return await removeFile(containerName, fileName);
}
@post('/containers/{containerName}/upload', {
responses: {
'200': {
description: 'Upload a Files model instances into Container',
content: { 'application/json': { schema: { 'x-ts-type': File } } },
},
},
})
async upload(@param.path.string('containerName') containerName: string): Promise<File> {
const upload = promisify(this.storageGcSvc.upload);
return await upload(containerName, this.request, this.response, {});
}
@get('/containers/{containerName}/download/{fileName}', {
responses: {
'200': {
description: 'Download a File within specified Container',
content: { 'application/json': { schema: { 'x-ts-type': Object } } },
},
},
})
async download(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<any> {
const download = promisify(this.storageGcSvc.download);
return await download(containerName, fileName, this.request, this.response);
}
}
就是这样,按照这些步骤,您必须让存储组件运行良好。祝你好运!
此致。
我正在使用环回 3.0 中的存储组件来访问云存储。但我需要如何在环回 4.0.The 中实现 link 以使其在 3.0.
中成为样本来自 LoopBack 团队的问候!
我们尚未研究 loopback-component-storage 与 LoopBack 版本 4 的集成。
由于存储组件的行为更像是连接器而不是组件,我认为应该可以通过 @loopback/service-proxy
层在 LoopBack 4 中使用它。有关文档,请参阅 Calling other APIS and web services。该页面上的代码片段以 loopback-connector-rest
为例,您将改用 loopback-component-storage
。
一旦您可以从 JavaScript/TypeScript 访问存储组件,您还需要公开 REST API。 LoopBack 4 不提供任何内置控制器,您必须自己编写一个。请参阅 storage-service.js 中 LB3 远程方法的定义,您将需要构建控制器方法以接受相同的参数,然后在后台调用服务代理。
如果您愿意研究此集成,那么最好在 loopback-next 中打开一个新的 GitHub 问题,我们可以在其中进行结构化对话。
我将与社区分享我的实现,因为即使 Miroslav Bajtoš is the key to do that, the documentation suggested is not very clear, I refer to Calling other APIS and web services 的答案。
- 首先 我使用
lb4
命令 cli 工具创建了一个新的 DataSource 并且我用与我相同的键填充了json
文件用于环回 3 数据源。
// storage.datasource.ts
import { inject } from '@loopback/core';
import { juggler } from '@loopback/service-proxy';
import * as config from './storage-gc.datasource.json';
export class StorageGCDataSource extends juggler.DataSource {
static dataSourceName = 'StorageGC';
constructor(
@inject('datasources.config.StorageGC', { optional: true })
dsConfig: object = config,
) {
super(dsConfig);
}
}
// storage.datasource.json
{
"name": "Storage",
"connector": "loopback-component-storage",
"provider": "google",
"keyFilename": "your-project-key.json",
"projectId": "your-project-id",
"nameConflict": "makeUnique"
}
// Note: your-project-key.json is in the root folder at the same level that tsconfig.json
记得需要安装loopback-component-storage运行
npm install --save loopback-component-storage
- 第二位 我使用
lb4
命令 cli 工具创建了两个新模型。这些模型是:Container
和File
.
// container.model.ts
import {Entity, model, property} from '@loopback/repository';
@model()
export class Container extends Entity {
@property({
type: 'string',
required: true,
})
name: string;
constructor(data?: Partial<Container>) {
super(data);
}
}
// file.model.ts
import { Entity, model, property } from '@loopback/repository';
@model()
export class File extends Entity {
@property({
type: 'string',
required: true,
})
name: string;
@property({
type: 'string',
})
type?: string;
@property({
type: 'string',
})
url?: string;
constructor(data?: Partial<File>) {
super(data);
}
}
- 第三名 我在
src/
目录中创建了一个名为interfaces
的新文件夹。在这个新文件夹中,我创建了一个index.ts
(主要是为了遵循导出约定)和一个storage.interface.ts
(这是重要文件)
// index.ts
export * from './storage.interface';
// storage.interface.ts
import { Container, File } from "../models";
export type Callback<T> = (err: Error | null, reply: T) => void;
export interface IStorageService {
// container methods
createContainer(container: Partial<Container>, cb: Callback<Container>): void;
destroyContainer(containerName: string, cb: Callback<boolean>): void;
getContainers(cb: Callback<Container[]>): void;
getContainer(containerName: string, cb: Callback<Container>): void;
// file methods
getFiles(containerName: string, options: Object, cb: Callback<File[]>): void;
getFile(containerName: string, fileName: string, cb: Callback<File>): void;
removeFile(containerName: string, fileName: string, cb: Callback<boolean>): void;
// main methods
upload(containerName: string, req: any, res: any, options: Object, cb: Callback<any>): void;
download(containerName: string, fileName: string, req: any, res: any, cb: Callback<any>): void;
}
- 第四位我创建了
container.controller.ts
文件,但是在这一步之前你需要安装@loopback/service-proxy 运行 以下命令:
npm install --save @loopback/service-proxy
// storage-gc.controller.ts
import { inject } from '@loopback/core';
import { serviceProxy } from '@loopback/service-proxy';
import { post, requestBody, del, param, get, getFilterSchemaFor, Request, Response, RestBindings } from '@loopback/rest';
import { Filter } from '@loopback/repository';
import { promisify } from 'util';
import { IStorageService } from '../interfaces';
import { Container, File } from '../models';
export class StorageGcController {
@serviceProxy('StorageGC') // StorageGC is the name of the datasoruce
private storageGcSvc: IStorageService;
constructor(@inject(RestBindings.Http.REQUEST) public request: Request,
@inject(RestBindings.Http.RESPONSE) public response: Response) { }
@post('/containers', {
responses: {
'200': {
description: 'Container model instance',
content: { 'application/json': { schema: { 'x-ts-type': Container } } },
},
},
})
async createContainer(@requestBody() container: Container): Promise<Container> {
const createContainer = promisify(this.storageGcSvc.createContainer);
return await createContainer(container);
}
@get('/containers', {
responses: {
'200': {
description: 'Array of Containers model instances',
content: {
'application/json': {
schema: { type: 'array', items: { 'x-ts-type': Container } },
},
},
},
},
})
async findContainer(@param.query.object('filter', getFilterSchemaFor(Container)) filter?: Filter): Promise<Container[]> {
const getContainers = promisify(this.storageGcSvc.getContainers);
return await getContainers();
}
@get('/containers/{containerName}', {
responses: {
'200': {
description: 'Container model instance',
content: { 'application/json': { schema: { 'x-ts-type': Container } } },
},
},
})
async findContainerByName(@param.path.string('containerName') containerName: string): Promise<Container> {
const getContainer = promisify(this.storageGcSvc.getContainer);
return await getContainer(containerName);
}
@del('/containers/{containerName}', {
responses: {
'204': {
description: 'Container DELETE success',
},
},
})
async deleteContainerByName(@param.path.string('containerName') containerName: string): Promise<boolean> {
const destroyContainer = promisify(this.storageGcSvc.destroyContainer);
return await destroyContainer(containerName);
}
@get('/containers/{containerName}/files', {
responses: {
'200': {
description: 'Array of Files model instances belongs to container',
content: {
'application/json': {
schema: { type: 'array', items: { 'x-ts-type': File } },
},
},
},
},
})
async findFilesInContainer(@param.path.string('containerName') containerName: string,
@param.query.object('filter', getFilterSchemaFor(Container)) filter?: Filter): Promise<File[]> {
const getFiles = promisify(this.storageGcSvc.getFiles);
return await getFiles(containerName, {});
}
@get('/containers/{containerName}/files/{fileName}', {
responses: {
'200': {
description: 'File model instances belongs to container',
content: { 'application/json': { schema: { 'x-ts-type': File } } },
},
},
})
async findFileInContainer(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<File> {
const getFile = promisify(this.storageGcSvc.getFile);
return await getFile(containerName, fileName);
}
@del('/containers/{containerName}/files/{fileName}', {
responses: {
'204': {
description: 'File DELETE from Container success',
},
},
})
async deleteFileInContainer(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<boolean> {
const removeFile = promisify(this.storageGcSvc.removeFile);
return await removeFile(containerName, fileName);
}
@post('/containers/{containerName}/upload', {
responses: {
'200': {
description: 'Upload a Files model instances into Container',
content: { 'application/json': { schema: { 'x-ts-type': File } } },
},
},
})
async upload(@param.path.string('containerName') containerName: string): Promise<File> {
const upload = promisify(this.storageGcSvc.upload);
return await upload(containerName, this.request, this.response, {});
}
@get('/containers/{containerName}/download/{fileName}', {
responses: {
'200': {
description: 'Download a File within specified Container',
content: { 'application/json': { schema: { 'x-ts-type': Object } } },
},
},
})
async download(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<any> {
const download = promisify(this.storageGcSvc.download);
return await download(containerName, fileName, this.request, this.response);
}
}
通过这些步骤,您的存储组件,或者更准确地说是连接器,应该可以按预期工作。但是按照 Calling other APIS and web services 指南的说明改进集成测试的实现,你应该在控制器中使用提供者而不是 @serviceProxy
装饰器,为此我在里面创建了一个名为 providers
的新文件夹包含以下两个文件的 src/
文件夹:
// index.ts
export * from './storage-service.provider';
// storage-gc-service.provider.ts
import { getService, juggler } from '@loopback/service-proxy';
import { Provider } from '@loopback/core';
import { StorageGCDataSource } from '../datasources/storage-gc.datasource';
import { IStorageService } from '../interfaces';
export class StorageGCServiceProvider implements Provider<IStorageService> {
constructor(
protected dataSource: juggler.DataSource = new StorageGCDataSource()
/* I try to change the line above in the same way that documentation show,
as follows:
@inject('datasources.StorageGC')
protected dataSource: juggler.DataSource = new StorageGCDataSource()
and also, in the same way that repositories
@inject('datasources.StorageGC')
protected dataSource: StorageGCDataSource
but always return:
`Error: Cannot resolve injected arguments for StorageGCServiceProvider.[0]:
The arguments[0] is not decorated for dependency injection, but a value is
not supplied`
*/
) { }
value(): Promise<IStorageService> {
return getService(this.dataSource);
}
}
之后,您需要按以下方式修改您的src/index.ts
和您的storage-gc.controller.ts
// src/index.ts
import { ApplicationConfig } from '@loopback/core';
import { HocicosCuriososApp } from './application';
import { StorageGCServiceProvider } from './providers';
export { HocicosCuriososApp };
export async function main(options: ApplicationConfig = {}) {
const app = new HocicosCuriososApp(options);
/* Add this line, it add a service to the app after that you can
call them in the controller with dependency injection, like:
@inject('services.StorageGCService') */
app.serviceProvider(StorageGCServiceProvider);
await app.boot();
await app.start();
const url = app.restServer.url;
console.log(`Server is running at ${url}`);
console.log(`Try ${url}/ping`);
return app;
}
// storage-gc.controller.ts
import { inject } from '@loopback/core';
import { post, requestBody, del, param, get, getFilterSchemaFor, Request, Response, RestBindings } from '@loopback/rest';
import { Filter } from '@loopback/repository';
import { promisify } from 'util';
import { IStorageService } from '../interfaces';
import { Container, File } from '../models';
export class StorageGcController {
@inject('services.StorageGCService')
private storageGcSvc: IStorageService;
constructor(@inject(RestBindings.Http.REQUEST) public request: Request,
@inject(RestBindings.Http.RESPONSE) public response: Response) { }
@post('/containers', {
responses: {
'200': {
description: 'Container model instance',
content: { 'application/json': { schema: { 'x-ts-type': Container } } },
},
},
})
async createContainer(@requestBody() container: Container): Promise<Container> {
const createContainer = promisify(this.storageGcSvc.createContainer);
return await createContainer(container);
}
@get('/containers', {
responses: {
'200': {
description: 'Array of Containers model instances',
content: {
'application/json': {
schema: { type: 'array', items: { 'x-ts-type': Container } },
},
},
},
},
})
async findContainer(@param.query.object('filter', getFilterSchemaFor(Container)) filter?: Filter): Promise<Container[]> {
const getContainers = promisify(this.storageGcSvc.getContainers);
return await getContainers();
}
@get('/containers/{containerName}', {
responses: {
'200': {
description: 'Container model instance',
content: { 'application/json': { schema: { 'x-ts-type': Container } } },
},
},
})
async findContainerByName(@param.path.string('containerName') containerName: string): Promise<Container> {
const getContainer = promisify(this.storageGcSvc.getContainer);
return await getContainer(containerName);
}
@del('/containers/{containerName}', {
responses: {
'204': {
description: 'Container DELETE success',
},
},
})
async deleteContainerByName(@param.path.string('containerName') containerName: string): Promise<boolean> {
const destroyContainer = promisify(this.storageGcSvc.destroyContainer);
return await destroyContainer(containerName);
}
@get('/containers/{containerName}/files', {
responses: {
'200': {
description: 'Array of Files model instances belongs to container',
content: {
'application/json': {
schema: { type: 'array', items: { 'x-ts-type': File } },
},
},
},
},
})
async findFilesInContainer(@param.path.string('containerName') containerName: string,
@param.query.object('filter', getFilterSchemaFor(Container)) filter?: Filter): Promise<File[]> {
const getFiles = promisify(this.storageGcSvc.getFiles);
return await getFiles(containerName, {});
}
@get('/containers/{containerName}/files/{fileName}', {
responses: {
'200': {
description: 'File model instances belongs to container',
content: { 'application/json': { schema: { 'x-ts-type': File } } },
},
},
})
async findFileInContainer(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<File> {
const getFile = promisify(this.storageGcSvc.getFile);
return await getFile(containerName, fileName);
}
@del('/containers/{containerName}/files/{fileName}', {
responses: {
'204': {
description: 'File DELETE from Container success',
},
},
})
async deleteFileInContainer(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<boolean> {
const removeFile = promisify(this.storageGcSvc.removeFile);
return await removeFile(containerName, fileName);
}
@post('/containers/{containerName}/upload', {
responses: {
'200': {
description: 'Upload a Files model instances into Container',
content: { 'application/json': { schema: { 'x-ts-type': File } } },
},
},
})
async upload(@param.path.string('containerName') containerName: string): Promise<File> {
const upload = promisify(this.storageGcSvc.upload);
return await upload(containerName, this.request, this.response, {});
}
@get('/containers/{containerName}/download/{fileName}', {
responses: {
'200': {
description: 'Download a File within specified Container',
content: { 'application/json': { schema: { 'x-ts-type': Object } } },
},
},
})
async download(@param.path.string('containerName') containerName: string,
@param.path.string('fileName') fileName: string): Promise<any> {
const download = promisify(this.storageGcSvc.download);
return await download(containerName, fileName, this.request, this.response);
}
}
就是这样,按照这些步骤,您必须让存储组件运行良好。祝你好运!
此致。