System Information
- Strapi Version: 4.25.4 (node v20.12.2)
- Operating System:
- Database: sqlite
- Node Version: v20.12.2
- NPM Version:
- Yarn Version:
While uploading image on s3 bucket, I also want to store tags. But in custom-aws-s3 file i’m always get options value as empty object. How can I pass tags value in custom-aws-s3?
// config/plugin.js
const path = require("path");
module.exports = ({ env }) => ({
"custom-upload": {
enabled: true,
resolve: "./src/plugins/custom-upload",
},
upload: {
config: {
provider: path.resolve(
__dirname,
"..",
"src",
"extensions",
"upload",
"providers",
"custom-aws-s3"
),
providerOptions: {
accessKeyId: env("AWS_ACCESS_KEY_ID"),
secretAccessKey: env("AWS_ACCESS_SECRET"),
region: env("AWS_REGION"),
params: {
ACL: env("AWS_ACL", "public-read"),
signedUrlExpires: env("AWS_SIGNED_URL_EXPIRES", 15 * 60),
Bucket: env("AWS_BUCKET"),
},
},
actionOptions: {
upload: {},
uploadStream: {},
delete: {},
},
},
},
});
// src/upload/controllers/custom-upload.js
"use strict";
module.exports = {
async upload(ctx) {
try {
const { tags } = ctx.request.body;
const { files } = ctx.request.files;
if (files && tags) {
// Upload file and get the response
const uploadedFile = await strapi.plugins[
"upload"
].services.upload.upload({
data: { metadata: { Tagging: tags } },
files,
options: {
Tagging: tags,
},
});
console.log("uploadedFile", uploadedFile);
ctx.send({
message: "File uploaded successfully",
fileUrl: uploadedFile,
});
} else {
ctx.send({ message: "File or text content missing" }, 400);
}
} catch (error) {
ctx.throw(500, error);
}
},
};
// src/extensions/provider/upload/custom-aws-s3.js
"use strict";
/**
* Module dependencies
*/
const { S3 } = require("aws-sdk");
module.exports = {
init(config) {
const { accessKeyId, secretAccessKey, region, params } = config;
const s3 = new S3({
accessKeyId,
secretAccessKey,
region,
});
return {
async upload(file, options) {
return new Promise((resolve, reject) => {
console.log("xxxxx", file, options);
const uploadParams = {
Bucket: params.Bucket,
Key: `${file.hash}${file.ext}`,
Body: file.stream || Buffer.from(file.buffer, "binary"),
ContentType: file.mime,
Tagging: options?.Tagging
? JSON.parse(options.Tagging)
.map((tag) => `${tag.key}=${tag.value}`)
.join("&")
: "",
};
s3.upload(uploadParams, (err, data) => {
if (err) {
return reject(err);
}
file.url = data.Location;
resolve(file.url);
});
});
},
async delete(file) {
return new Promise((resolve, reject) => {
const deleteParams = {
Bucket: params.Bucket,
Key: `${file.hash}${file.ext}`,
};
s3.deleteObject(deleteParams, (err, data) => {
if (err) {
return reject(err);
}
resolve();
});
});
},
};
},
};