No results for

Powered byAlgolia
The APIs provided by this jslib operate synchronously, which means k6 must wait for operations that use the client classes to finish before proceeding with the rest of the script.

In some cases, such as downloading large files from S3, this could affect performance and test results. To minimize the impact on test performance, we recommend using these operations in thesetup and teardown lifecycle functions. These functions run before and after the test run and thus do not influence test results.

S3Client allows interacting with AWS S3's buckets and objects. Namely, it allows the user to list buckets and the objects they contain, as well as download, uploading, and deleting objects. The S3Client operations are blocking, and we recommend reserving their usage to the setup and teardown functions as much as possible.

S3Client is included in both the dedicated jslib s3.js bundle, and the aws.js one, containing all the services clients.

Methods

FunctionDescription
listBuckets()List the buckets the authenticated user has access to
listObjects(bucketName, [prefix])List the objects contained in a bucket
getObject(bucketName, objectKey)Download an object from a bucket
putObject(bucketName, objectKey, data)Upload an object to a bucket
deleteObject(bucketName, objectKey)Delete an object from a bucket

Throws

S3 Client methods will throw errors in case of failure.

ErrorCondition
InvalidSignatureErrorwhen invalid credentials were provided.
S3ServiceErrorwhen AWS replied to the requested operation with an error.

Example

import { check } from 'k6';
import exec from 'k6/execution';
import http from 'k6/http';
import { AWSConfig, S3Client } from 'https://jslib.k6.io/aws/0.7.2/s3.js';
const awsConfig = new AWSConfig({
region: __ENV.AWS_REGION,
accessKeyId: __ENV.AWS_ACCESS_KEY_ID,
secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY,
});
const s3 = new S3Client(awsConfig);
const testBucketName = 'test-jslib-aws';
const testInputFileKey = 'productIDs.json';
const testOutputFileKey = `results-${Date.now()}.json`;
export function setup() {
// If our test bucket does not exist, abort the execution.
const buckets = s3.listBuckets();
if (buckets.filter((b) => b.name === testBucketName).length == 0) {
exec.test.abort();
}
// If our test object does not exist, abort the execution.
const objects = s3.listObjects(testBucketName);
if (objects.filter((o) => o.key === testInputFileKey).length == 0) {
exec.test.abort();
}
// Download the S3 object containing our test data
const inputObject = s3.getObject(testBucketName, testInputFileKey);
// Let's return the downloaded S3 object's data from the
// setup function to allow the default function to use it.
return {
productIDs: JSON.parse(inputObject.data),
};
}
export default function (data) {
// Pick a random product ID from our test data
const randomProductID = data.productIDs[Math.floor(Math.random() * data.productIDs.length)];
// Query our ecommerce website's product page using the ID
const res = http.get(`http://your.website.com/product/${randomProductID}/`);
check(res, { 'is status 200': res.status === 200 });
}
export function handleSummary(data) {
// Once the load test is over, let's upload the results to our
// S3 bucket. This is executed after teardown.
s3.putObject(testBucketName, testOutputFileKey, JSON.stringify(data));
}