Open files in Init

Hi, all

In VU stage, I need make a HTTP Post call with a file in its body. The K6 doco says I need to do it as below, which works for me.

const fd = new FormData();
fd.append('images', http.file(fileToBeUploaded1, 'Test1.jpg', 'image/jpeg'));

const AppendFilesToApplicationResponse =, fd.body(), {
    headers: { 'Content-Type': 'multipart/form-data; boundary=' + fd.boundary },

The problem is I can not open a file in VU stage (otherwise I hit an run-time error from K6) hence I have to open it in Init stage, as below.

const fileToBeUploaded1 = open('C://temp//Test1.jpg');

Opening the file in Init stage works functionally and I am able to run my K6 script. However my test case is randomly picking one file from a set of 30 prepared test files. So I have to open ALL 30 files in Init stage so that I can randomly use one of the files in VU stage for that HTTP Post call. Even a bigger challenge is that the same file will be opened multiple times because the Init stage will be executed multiple times (4 times at least) in each test run, even I run with 1 vuser for 1 iteration. Some of my test files are as large as dozens Mbytes.

The HTTP Post call is an API for uploading various types of documents / files to a file processing engine, if you are curious why I need to do so in my K6 scripts.

Is there a better way to do this in K6? My original performance testing script is written in Load Runner which allows me to randomly pick one of the 30 files for that HTTP Post call. A file is opened (once) when it is picked, in Load Runner execution. That is what I would like to achieve.

Thank you!