orangewise / s3-zip

Download selected files from an Amazon S3 bucket as a zip file

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom paths not working

robertoibarra opened this issue · comments

This is a great class by the way :) Thanks for sharing.

I was following the example in the documentation from "Organize your archive with custom paths and permissions" however when I passed the argument archiveFiles it was ignored.

Looking into the code from s3-zip.js I went to see how it is working. From what I can tell, you try to find the array index of the file being processed by searching inside the content of filesS3 and getting the index number. But I think there is some issue with how you trim the content that you search, let me show you.

Here is some sample debugged variables using your current code from my use case:
folder: N4KYyS4DCh0hA1T590cMDpcdbL6GBbgF
file.path: N4KYyS4DCh0hA1T590cMDpcdbL6GBbgF/Abundancia en Acción SAPI de CV (7246)/AA - Acta Constitutiva (Válido)/Acta 6118 Abundancia.pdf
file.path.startsWith(folder): true
file.path.substr(folder.length): /Abundancia en Acción SAPI de CV (7246)/AA - Acta Constitutiva (Válido)/Acta 6118 Abundancia.pdf

However the filesS3.indexOf(...) returns -1, thus ignoring the instructions I passed in archiveFiles, because actually the value stored in filesS3 is:
[ 'Abundancia en Acción SAPI de CV (7246)/AA - Acta Constitutiva (Válido)/Acta 6118 Abundancia.pdf',

So what I did is I increased by one the trimming and then the filesS3.indexOf(... would return the correct index value and everything worked. Like so:
file.path.substr(folder.length + 1): Abundancia en Acción SAPI de CV (7246)/AA - Acta Constitutiva (Válido)/Acta 6118 Abundancia.pdf

Original code:
const i = filesS3.indexOf(file.path.startsWith(folder) ? file.path.substr(folder.length) : file.path)

Suggested code:
const i = filesS3.indexOf(file.path.startsWith(folder) ? file.path.substr(folder.length + 1) : file.path)

Hi @robertoibarra, I'm not sure I understand. Can you add a PR with some tests?

Thanks!

Sure @orangewise, here's my function that zips up the files from a folder in a bucket. Without the "+1" I am suggesting in your code, the parameter archiveStructure I pass to s3Zip has no effect so the archive's structure doesn't get updated according to what I set in the array archiveStructure.

function zipFiles(files) {

	//Format how the archive's directory structure is created since we want to remove the parent folder which is the encrypted_folder_id
	var archiveStructure = [];

	var file = [];  
	for (i = 0; i < files.length; i++) { 
		file = { "name" : files[i], "date" : fileDates[encrypted_folder_id + "/" + files[i]] };
		archiveStructure.push(file);
	}    
	
	//Zlib level 0 to just store files together without compression, faster execution
	s3Zip
	 .archive({ region: process.env.S3_DEST_REGION, bucket: process.env.S3_DEST }, encrypted_folder_id, files, archiveStructure)
	 .pipe(uploadFromStream())
}

I've encountered this as well.

The problem seems to be that the folder argument is expected to end with a trailing slash, but this does not appear to be documented.

When the folder argument doesn't end with a trailing slash, the line:

const i = filesS3.indexOf(file.path.startsWith(folder) ? file.path.substr(folder.length) : file.path)

will wind up executing (as an example):

const i = filesS3.indexOf('/file_name_here.csv')

because the folder.length property is one too short.

This is resolved by adding a trailing slash to the name of the folder in the call to .archive.

Would it be possible to create a PR? I will merge it after my holiday.

I've encountered this as well.

The problem seems to be that the folder argument is expected to end with a trailing slash, but this does not appear to be documented.

When the folder argument doesn't end with a trailing slash, the line:

const i = filesS3.indexOf(file.path.startsWith(folder) ? file.path.substr(folder.length) : file.path)

will wind up executing (as an example):

const i = filesS3.indexOf('/file_name_here.csv')

because the folder.length property is one too short.

This is resolved by adding a trailing slash to the name of the folder in the call to .archive.

Yes, you are correct @magmastonealex , actually you mean a "leading" slash rather than a trailing one, thanks for including an example. I create the files array like so:
files.push("/" + file_names[i]);
Works perfectly with the current repository code.