Page updated Nov 12, 2023

Custom Plugin

You can create your custom pluggable for Storage. This may be helpful if you want to integrate your app with a custom storage backend.

To create a plugin implement the StorageProvider interface:

import { Storage, StorageProvider } from 'aws-amplify'; export default class MyStorageProvider implements StorageProvider { // category and provider name static category = 'Storage'; static providerName = 'MyStorage'; // you need to implement these seven methods // configure your provider configure(config: object): object; // copy object, optional copy?( src: { key: string; identityId: string; level: 'public' | 'protected' | 'private' }, dest: { key: string; level: 'public' | 'protected' | 'private' }, options? ): Promise<any> // get object/pre-signed url from storage get(key: string, options?): Promise<String|Object> // upload storage object put(key: string, object, options?): Promise<Object> // remove object remove(key: string, options?): Promise<any> // list objects for the path list(path, options?): Promise<any> // return 'Storage'; getCategory(): string; // return the name of you provider getProviderName(): string;
1import { Storage, StorageProvider } from 'aws-amplify';
2
3export default class MyStorageProvider implements StorageProvider {
4 // category and provider name
5 static category = 'Storage';
6 static providerName = 'MyStorage';
7
8 // you need to implement these seven methods
9 // configure your provider
10 configure(config: object): object;
11
12 // copy object, optional
13 copy?(
14 src: { key: string; identityId: string; level: 'public' | 'protected' | 'private' },
15 dest: { key: string; level: 'public' | 'protected' | 'private' },
16 options?
17 ): Promise<any>
18
19 // get object/pre-signed url from storage
20 get(key: string, options?): Promise<String|Object>
21
22 // upload storage object
23 put(key: string, object, options?): Promise<Object>
24
25 // remove object
26 remove(key: string, options?): Promise<any>
27
28 // list objects for the path
29 list(path, options?): Promise<any>
30
31 // return 'Storage';
32 getCategory(): string;
33
34 // return the name of you provider
35 getProviderName(): string;

You can now register your pluggable:

// add the plugin Storage.addPluggable(new MyStorageProvider()); // get the plugin Storage.getPluggable(MyStorageProvider.providerName); // remove the plugin Storage.removePluggable(MyStorageProvider.providerName); // send configuration into Amplify Storage.configure({ [MyStorageProvider.providerName]: { // My Storage provider configuration } });
1// add the plugin
2Storage.addPluggable(new MyStorageProvider());
3
4// get the plugin
5Storage.getPluggable(MyStorageProvider.providerName);
6
7// remove the plugin
8Storage.removePluggable(MyStorageProvider.providerName);
9
10// send configuration into Amplify
11Storage.configure({
12 [MyStorageProvider.providerName]: {
13 // My Storage provider configuration
14 }
15});

You can pass in your custom provider class to the Storage function to get Typescript support:

class MyStorageProvider implements StorageProvider { ... get(key: string, config: { config1: number; }): Promise<{ key: string }>; } // will automatically use the return type from the provider's function signature const getResult: Promise<{ key: string }> = Storage.get<MyStorageProvider>('key', { provider: 'MyStorage', config1: 123, });
1class MyStorageProvider implements StorageProvider {
2 ...
3 get(key: string, config: { config1: number; }): Promise<{ key: string }>;
4}
5
6// will automatically use the return type from the provider's function signature
7const getResult: Promise<{ key: string }> = Storage.get<MyStorageProvider>('key', {
8 provider: 'MyStorage',
9 config1: 123,
10});

The default provider (Amazon S3) is in use when you call Storage.put( ) unless you specify a different provider: Storage.put(key, object, {provider: 'MyStorage'}).