# Upload Thanks to the plugin `Upload`, you can upload any kind of files on your server or externals providers such as **AWS S3**. ## Endpoints
| Method | Path | Description | | :----- | :---------------- | :------------------ | | GET | /upload/files | Get a list of files | | GET | /upload/files/:id | Get a specific file | | POST | /upload | Upload files | | DELETE | /upload/files/:id | Delete a file |
## Upload files To upload files into your application. ### Parameters - `files`: The file(s) to upload. The value(s) can be a Buffer or Stream. ### Code example ```html
``` ::: warning You have to send FormData in your request body ::: ## Upload files related to an entry To upload files that will be liked to an specific entry. ### Request parameters - `files`: The file(s) to upload. The value(s) can be a Buffer or Stream. - `path` (optional): The folder where the file(s) will be uploaded to (only supported on strapi-provider-upload-aws-s3). - `refId`: The ID of the entry which the file(s) will be linked to. - `ref`: The name of the model which the file(s) will be linked to (see more below). - `source` (optional): The name of the plugin where the model is located. - `field`: The field of the entry which the file(s) will be precisely linked to. ### Examples The `Restaurant` model attributes: ```json "attributes": { "name": { "type": "string" }, "cover": { "model": "file", "via": "related", "plugin": "upload" } } ``` Code ```html
``` ::: warning You have to send FormData in your request body ::: ## Upload file during entry creation You can also add files during your entry creation. ### Examples The `Restaurant` model attributes: ```json "attributes": { "name": { "type": "string" }, "cover": { "model": "file", "via": "related", "plugin": "upload" } } ``` Code ```html
``` You entry data have to be contained in a `data` key. You have to `JSON.stringify` your data object. And for your files, they have to be prefixed by `files`. Example here with cover attribute `files.cover`. ::: tip If you want to upload files for a group, you will have to specify the inidex of the item you wan to add the file. Example `files.my_group_name[the_index].attribute_name` ::: ::: warning You have to send FormData in your request body ::: ## Models definition Adding a file attribute to a model (or the model of another plugin) is like adding a new association. In the first example below, you will be able to upload and attach one file to the avatar attribute. **Path —** `User.settings.json`. ```json { "connection": "default", "attributes": { "pseudo": { "type": "string", "required": true }, "email": { "type": "email", "required": true, "unique": true }, "avatar": { "model": "file", "via": "related", "plugin": "upload" } } } ``` In our second example, you can upload and attach multiple pictures to the restaurant. **Path —** `Restaurant.settings.json`. ```json { "connection": "default", "attributes": { "name": { "type": "string", "required": true }, "convers": { "collection": "file", "via": "related", "plugin": "upload" } } } ``` ## Install providers By default Strapi provides a local file upload system. You might want to upload your files on AWS S3 or another provider. You can check all the available providers developed by the community on npmjs.org - [Providers list](https://www.npmjs.com/search?q=strapi-provider-upload-) To install a new provider run: ``` $ npm install strapi-provider-upload-aws-s3@beta --save ``` ::: tip If the provider is not in the mono repo, you probably don't need `@beta` depending if the creator published it with this tag or not. ::: Then, visit [http://localhost:1337/admin/plugins/upload/configurations/development](http://localhost:1337/admin/plugins/upload/configurations/development) on your web browser and configure the provider. ## Create providers If you want to create your own, make sure the name starts with `strapi-provider-upload-` (duplicating an existing one will be easier to create), modify the `auth` config object and customize the `upload` and `delete` functions. To use it you will have to publish it on **npm**. ### Create a local provider If you want to create your own provider without publishing it on **npm** you can follow these steps: - Create a `providers` folder in your application. - Create your provider as explained in the documentation eg. `./providers/strapi-provider-upload-[...]/...` - Then update your `package.json` to link your `strapi-provider-upload-[...]` dependency to the [local path](https://docs.npmjs.com/files/package.json#local-paths) of your new provider. ```json { ... "dependencies": { ... "strapi-provider-upload-[...]": "file:providers/strapi-provider-upload-[...]", ... } } ``` - Finally, run `yarn install` or `npm install` to install your new custom provider.