@@ -13,13 +13,17 @@ S3Client is included in both the dedicated jslib `s3.js` bundle, and the `aws.js
1313
1414### Methods
1515
16- | Function | Description |
17- | :----------------------------------------------------------------------------------------------- | :---------------------------------------------------- |
18- | [ listBuckets()] ( /javascript-api/jslib/aws/s3client/s3client-listbuckets ) | List the buckets the authenticated user has access to |
19- | [ listObjects(bucketName, [ prefix] )] ( /javascript-api/jslib/aws/s3client/s3client-listobjects/ ) | List the objects contained in a bucket |
20- | [ getObject(bucketName, objectKey)] ( /javascript-api/jslib/aws/s3client/s3client-getobject/ ) | Download an object from a bucket |
21- | [ putObject(bucketName, objectKey, data)] ( /javascript-api/jslib/aws/s3client/s3client-putobject/ ) | Upload an object to a bucket |
22- | [ deleteObject(bucketName, objectKey)] ( /javascript-api/jslib/aws/s3client/s3client-deleteobject/ ) | Delete an object from a bucket |
16+ | Function | Description |
17+ | :------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------|
18+ | [ listBuckets()] ( /javascript-api/jslib/aws/s3client/s3client-listbuckets ) | List the buckets the authenticated user has access to |
19+ | [ listObjects(bucketName, [ prefix] )] ( /javascript-api/jslib/aws/s3client/s3client-listobjects/ ) | List the objects contained in a bucket |
20+ | [ getObject(bucketName, objectKey)] ( /javascript-api/jslib/aws/s3client/s3client-getobject/ ) | Download an object from a bucket |
21+ | [ putObject(bucketName, objectKey, data)] ( /javascript-api/jslib/aws/s3client/s3client-putobject/ ) | Upload an object to a bucket |
22+ | [ deleteObject(bucketName, objectKey)] ( /javascript-api/jslib/aws/s3client/s3client-deleteobject/ ) | Delete an object from a bucket |
23+ | [ createMultipartUpload(bucketName, objectKey)] ( /javascript-api/jslib/aws/s3client/s3client-createmultipartupload/ ) | Create a multipart upload for a given objectKey to a bucket |
24+ | [ uploadPart(bucketName, objectKey, uploadId, partNumber, data)] ( /javascript-api/jslib/aws/s3client/s3client-uploadpart/ ) | Upload a part in a multipart upload |
25+ | [ completeMultipartUpload(bucketName, objectKey, uploadId, parts)] ( /javascript-api/jslib/aws/s3client/s3client-completemultipartupload/ ) | Complete a previously assembled multipart upload |
26+ | [ abortMultipartUpload(bucketName, objectKey, uploadId)] ( /javascript-api/jslib/aws/s3client/s3client-abortmultipartupload/ ) | Abort a multipart upload |
2327
2428### Throws
2529
@@ -30,7 +34,7 @@ S3 Client methods will throw errors in case of failure.
3034| InvalidSignatureError | when invalid credentials were provided. |
3135| S3ServiceError | when AWS replied to the requested operation with an error. |
3236
33- ### Example
37+ ### Examples
3438
3539<CodeGroup labels ={[]} >
3640
@@ -39,7 +43,7 @@ import { check } from 'k6';
3943import exec from ' k6/execution' ;
4044import http from ' k6/http' ;
4145
42- import { AWSConfig , S3Client } from ' https://jslib.k6.io/aws/0.7.2 /s3.js' ;
46+ import { AWSConfig , S3Client } from ' https://jslib.k6.io/aws/0.8.0 /s3.js' ;
4347
4448const awsConfig = new AWSConfig ({
4549 region: __ENV .AWS_REGION ,
@@ -92,3 +96,75 @@ export function handleSummary(data) {
9296```
9397
9498</CodeGroup >
99+
100+ <CodeGroup labels ={[]} >
101+
102+ ``` javascript
103+ import crypto from ' k6/crypto' ;
104+ import exec from ' k6/execution' ;
105+
106+ import { AWSConfig , S3Client } from ' https://jslib.k6.io/aws/0.8.0/s3.js' ;
107+
108+ const awsConfig = new AWSConfig ({
109+ region: __ENV .AWS_REGION ,
110+ accessKeyId: __ENV .AWS_ACCESS_KEY_ID ,
111+ secretAccessKey: __ENV .AWS_SECRET_ACCESS_KEY ,
112+ sessionToken: __ENV .AWS_SESSION_TOKEN ,
113+ });
114+
115+ const s3 = new S3Client (awsConfig);
116+
117+ const testBucketName = ' test-jslib-aws' ;
118+ const testFileKey = ' multipart.txt' ;
119+
120+ export default function () {
121+ // List the buckets the AWS authentication configuration
122+ // gives us access to.
123+ const buckets = s3 .listBuckets ();
124+
125+ // If our test bucket does not exist, abort the execution.
126+ if (buckets .filter ((b ) => b .name === testBucketName).length == 0 ) {
127+ exec .test .abort ();
128+ }
129+
130+ // Produce random bytes to upload of size ~12MB, that
131+ // we will upload in two 6MB parts. This is done as the
132+ // minimum part size supported by S3 is 5MB.
133+ const bigFile = crypto .randomBytes (12 * 1024 * 1024 );
134+
135+ // Initialize a multipart upload
136+ const multipartUpload = s3 .createMultipartUpload (testBucketName, testFileKey);
137+
138+ // Upload the first part
139+ const firstPartData = bigFile .slice (0 , 6 * 1024 * 1024 );
140+ const firstPart = s3 .uploadPart (
141+ testBucketName,
142+ testFileKey,
143+ multipartUpload .uploadId ,
144+ 1 ,
145+ firstPartData
146+ );
147+
148+ // Upload the second part
149+ const secondPartData = bigFile .slice (6 * 1024 * 1024 , 12 * 1024 * 1024 );
150+ const secondPart = s3 .uploadPart (
151+ testBucketName,
152+ testFileKey,
153+ multipartUpload .uploadId ,
154+ 2 ,
155+ secondPartData
156+ );
157+
158+ // Complete the multipart upload
159+ s3 .completeMultipartUpload (testBucketName, testFileKey, multipartUpload .uploadId , [
160+ firstPart,
161+ secondPart,
162+ ]);
163+
164+ // Let's redownload it verify it's correct, and delete it
165+ const obj = s3 .getObject (testBucketName, testFileKey);
166+ s3 .deleteObject (testBucketName, testFileKey);
167+ }
168+ ```
169+
170+ </CodeGroup >
0 commit comments