Hi K6 team!
I have a question related to upload of files.
Our API for upload has an endpoint that needs to determine the size of the file in bytes.
I used the command as displayed on your documentation to open and read the test.jpg file. This works.
However I run into an issue when I need to finalise the upload.
In the last endpoint called: finaliseResponse, I need a filesize of the file. In the code example, it shows the bytes hardcoded(this works), however I would like to have this dynamically done based on the file.
I tried to use the Blob: size property - Web APIs | MDN method as seen below, but that does not do the trick.
body = {
"fileName": filename,
"fileSize": blob.size, <----
"chunksCount": 1,
"sha256": file_sha,
}
I also tried to use
binFile.byteLength
blob.byteLength
What way of determining the size of the file or the blob, would be recommend?
Also tried to use webpack to import fs module, but this generates this issue:
Built at: 01/12/2022 1:47:47 PM
Asset Size Chunks Chunk Names
main.bundle.js 2.17 KiB 0 [emitted] main
Entrypoint main = main.bundle.js
[0] ./tests/script.js 3.02 KiB {0} [built]
[1] external "k6/http" 42 bytes {0} [built]
[2] external "k6" 42 bytes {0} [built]
[3] external "k6/crypto" 42 bytes {0} [built]
[5] external "fs" 42 bytes {0} [built]
+ 1 hidden module
➜ Prometheus-original git:(k6_init) ✗ k6 run dist/main.bundle.js
/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io
WARN[0000] The moduleSpecifier "fs" has no scheme but we will try to resolve it as remote module. This will be deprecated in the future and all remote modules will need to explicitly use "https" as scheme.
Thanks in advance, the last part is the actual code.
import http from 'k6/http';
import {check} from 'k6';
import crypto from 'k6/crypto';
const binFile = open('test.jpg', 'b');
export default function () {
let body
let filename = 'test.jpg'
const f = http.file(binFile, 'image/jpeg');
var blob = f.data
let file_sha = crypto.sha256(f.data, 'hex');
body = {
"client_id": 'some id,
"client_secret": 'some secret',
"grant_type": "client_credentials",
}
//Authorise yourself with OAUTH2
let authenticate_response = http.post(`SOMEURL/v7/authentication/oauth2/token/`, body,
{
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
let token = authenticate_response.json().access_token
//Prepare a file upload
const prep_response = http.post('SOMEURL/v7/upload/prepare', {},
{headers: {"Content-Type": "image/jpeg", Authorization: `Bearer ${token}`}}
);
let file_id = prep_response.json().file_id
check(prep_response, {
'is status 201': (r) => r.status === 201
});
const upload_chunk = http.post(`SOMEURL/v7/upload/${file_id}/chunk/0`, blob,
{
headers: {
"Content-Type": "multipart/form-data",
"Content-SHA256": file_sha, Authorization: `Bearer ${token}`
}
});
check(upload_chunk, {'is status 201': (r) => r.status === 201});
// Finalise the upload to the API
body = {
"fileName": filename,
"fileSize": 3048897, <---- HARDCODED VALUE
"chunksCount": 1,
"sha256": file_sha,
}
const finaliseResponse = http.post(`SOMEURL/v7/upload/${file_id}/finalise_api`, body,
{headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-SHA256": file_sha, Authorization : `Bearer ${token}`
}
});
check(finaliseResponse, {
'is status 201': (r) => r.status === 201});