How to access 'path' request parameter for aws-s3 upload when uploaded during entity creation

System Information
  • Strapi Version: 3.6.0
  • Operating System:
  • Database: mongoDB
  • Node Version: 12.13.1
  • NPM Version: 6.12.1
  • Yarn Version: 1.22.4

I am uploading a file using the aws-s3 upload provider plugin during entity creation (so like this). I see on that same documentation page but under the section, “Upload files related to an entry,” that it is possible to stipulate a path to upload files to a ‘subdirectory’ in S3. Can I pass in and make use of this path request parameter using the during entity creation version of upload?

My code looks like this and I’m not sure how to pass in a path param:

 (ID,entityProperties,entityFile) => {
   const formData = new FormData();
   if(entityFile){
     formData.append(`files.fileAttachment`,entityFile,entityFile.name);
   }
   if(entityProperties){
     formData.append('data', JSON.stringify(entityProperties));
   }
   return new Promise((resolve, reject) => {
     axios.put(
       putURL,
       formData
     ).then((response) =>{
       ...
     })
     .catch((error) =>{
       ...
     });
   });
 }

Hi @sethpkendall,

I’m not really sure about it but I think for AWS S3 the path can be set by adding your path/ to the file name.

i.e: your-path/${entityFile.name}

AWS S3 will know that it has to create a folder your-path and then upload your file into it :wink:

This is a good thought but does not work. I am not even sure how it doesn’t work but when I tried this the file appeared in the root directory of the s3 bucket with a file name like “path/filename”

Scratch my last response. The file does not appear with the “path/” prefix in the s3 bucket. The issue is that there does not seem to be a way to pass in a path OR to manipulate the filename when passing back to the upload provider. The path is always listed as null when it arrives to the upload function on the backend and any attempt to change the entityFile.name property is met with an error:

TypeError: Cannot set property name of #< File > which has only a getter

note that the third property in this line is not used and the filename is ultimately sourced from the ‘name’ property itself on the file entity

formData.append(files.fileAttachment,entityFile,entityFile.name);

note that the third property in this line is not used and the filename is ultimately sourced from the ‘name’ property itself on the file entity

Did you tried to change the name property itself ?

Yes, it is when I try to change that .name property that I get the aforementioned error:

TypeError: Cannot set property name of #< File > which has only a getter

The file entity in JS doesn’t allow you to mutate the name property it seems.