
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
@strapi/provider-upload-local
Advanced tools
@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.
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);
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.
# using yarn
yarn add @strapi/provider-upload-local
# using npm
npm install @strapi/provider-upload-local --save
This provider has only one parameter: sizeLimit
.
./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
Special configuration of the Strapi Security Middleware is not required on this provider since the default configuration allows loading images and media from "'self'"
.
FAQs
Local provider for strapi upload
The npm package @strapi/provider-upload-local receives a total of 118,539 weekly downloads. As such, @strapi/provider-upload-local popularity was classified as popular.
We found that @strapi/provider-upload-local demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.