What is @strapi/provider-upload-local?
@strapi/provider-upload-local is a provider for Strapi that allows you to upload files to the local file system. It is useful for development and testing environments where you don't need to store files in a cloud storage service.
What are @strapi/provider-upload-local's main functionalities?
File Upload
This feature allows you to upload a file to the local file system using Strapi's upload service. The code sample demonstrates how to read a file from the local disk and upload it using Strapi's upload service.
const strapi = require('strapi');
const fs = require('fs');
const uploadFile = async (filePath) => {
const file = fs.createReadStream(filePath);
const upload = await strapi.plugins['upload'].services.upload.upload({
data: {}, // mandatory declare the data(can be empty), otherwise it will give you an undefined error.
files: file,
});
return upload;
};
uploadFile('./path/to/your/file.jpg').then(console.log).catch(console.error);
File Retrieval
This feature allows you to retrieve a file's metadata from the local file system using Strapi's upload service. The code sample demonstrates how to fetch a file's details using its ID.
const strapi = require('strapi');
const getFile = async (fileId) => {
const file = await strapi.plugins['upload'].services.upload.fetch({ id: fileId });
return file;
};
getFile(1).then(console.log).catch(console.error);
File Deletion
This feature allows you to delete a file from the local file system using Strapi's upload service. The code sample demonstrates how to remove a file using its ID.
const strapi = require('strapi');
const deleteFile = async (fileId) => {
const result = await strapi.plugins['upload'].services.upload.remove({ id: fileId });
return result;
};
deleteFile(1).then(console.log).catch(console.error);
Other packages similar to @strapi/provider-upload-local
@strapi/provider-upload-aws-s3
This package allows you to upload files to AWS S3. It is suitable for production environments where you need scalable and reliable storage. Compared to @strapi/provider-upload-local, it provides cloud storage capabilities and integrates with AWS services.
@strapi/provider-upload-local
Resources
Links
Installation
yarn add @strapi/provider-upload-local
npm install @strapi/provider-upload-local --save
Configurations
This provider has only one parameter: sizeLimit
.
Provider Configuration
./config/plugins.js
module.exports = ({ env }) => ({
upload: {
config: {
provider: 'local',
providerOptions: {
sizeLimit: 100000,
},
},
},
});
The sizeLimit
parameter must be a number. Be aware that the unit is in bytes, and the default is 1000000. When setting this value high, you should make sure to also configure the body parser middleware maxFileSize
so the file can be sent and processed. Read more here
Security Middleware Configuration
Special configuration of the Strapi Security Middleware is not required on this provider since the default configuration allows loading images and media from "'self'"
.