Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
putObject
S3Client.putObject uploads an object to a bucket.
Parameters
PutObjectParams
Returns
Example
import {
AWSConfig,
S3Client,
} from 'https://jslib.k6.io/aws/0.14.0/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 testFileKey = 'bonjour.txt';
const testFile = open('./bonjour.txt', 'r');
export default async function () {
// Let's upload our test file to the bucket
const uploadResult = await s3.putObject(testBucketName, testFileKey, testFile, {
contentType: 'text/plain',
contentLength: testFile.length,
});
// Access the upload metadata
console.log('Upload successful! ETag:', uploadResult.eTag);
if (uploadResult.size) {
console.log('Object size:', uploadResult.size);
}
// Check if checksums are available
if (uploadResult.sha256) {
console.log('SHA256 checksum:', uploadResult.sha256);
}
// And let's redownload it to verify it's correct
const obj = await s3.getObject(testBucketName, testFileKey);
console.log(JSON.stringify(obj));
}A k6 script that will upload an object to a S3 bucket


