|
| 1 | +--- |
| 2 | +title: 'S3UploadedObject' |
| 3 | +head_title: 'S3UploadedObject' |
| 4 | +description: 'S3UploadedObject represents the response from S3 upload operations' |
| 5 | +weight: 20 |
| 6 | +--- |
| 7 | + |
| 8 | +# S3UploadedObject |
| 9 | + |
| 10 | +`S3UploadedObject` represents the response returned by S3 upload operations such as [`putObject`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/aws/s3client/putobject) and [`completeMultipartUpload`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/aws/s3client/completemultipartupload). It contains metadata about the uploaded object, including checksums and upload verification information. |
| 11 | + |
| 12 | +## Properties |
| 13 | + |
| 14 | +| Name | Type | Description | |
| 15 | +| :----------- | :---------------- | :---------------------------------------------------------------------------------------------------------------------------------------- | |
| 16 | +| eTag | string | The entity tag (ETag) of the uploaded object. This is a hash of the object that can be used to verify the integrity of the uploaded data. | |
| 17 | +| crc32 | string (optional) | The base64-encoded CRC32 checksum of the uploaded object, if available. | |
| 18 | +| crc32c | string (optional) | The base64-encoded CRC32C checksum of the uploaded object, if available. | |
| 19 | +| crc64nvme | string (optional) | The base64-encoded CRC64NVME checksum of the uploaded object, if available. | |
| 20 | +| sha1 | string (optional) | The base64-encoded SHA1 checksum of the uploaded object, if available. | |
| 21 | +| sha256 | string (optional) | The base64-encoded SHA256 checksum of the uploaded object, if available. | |
| 22 | +| checksumType | string (optional) | The type of checksum algorithm used. Can be "COMPOSITE" for multipart uploads or "FULL_OBJECT" for single-part uploads. | |
| 23 | +| size | number (optional) | The size of the uploaded object in bytes, if available. | |
| 24 | + |
| 25 | +## Example |
| 26 | + |
| 27 | +{{< code >}} |
| 28 | + |
| 29 | +```javascript |
| 30 | +import { |
| 31 | + AWSConfig, |
| 32 | + S3Client, |
| 33 | +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; |
| 34 | + |
| 35 | +const awsConfig = new AWSConfig({ |
| 36 | + region: __ENV.AWS_REGION, |
| 37 | + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, |
| 38 | + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, |
| 39 | +}); |
| 40 | + |
| 41 | +const s3 = new S3Client(awsConfig); |
| 42 | +const testBucketName = 'test-jslib-aws'; |
| 43 | +const testFileKey = 'test-file.txt'; |
| 44 | + |
| 45 | +export default async function () { |
| 46 | + const testData = 'Hello, World!'; |
| 47 | + |
| 48 | + // Upload the object and get the upload response |
| 49 | + const uploadResult = await s3.putObject(testBucketName, testFileKey, testData); |
| 50 | + |
| 51 | + // Access the upload metadata |
| 52 | + console.log('Upload ETag:', uploadResult.eTag); |
| 53 | + console.log('Object size:', uploadResult.size); |
| 54 | + |
| 55 | + // Check if checksums are available |
| 56 | + if (uploadResult.sha256) { |
| 57 | + console.log('SHA256 checksum:', uploadResult.sha256); |
| 58 | + } |
| 59 | + |
| 60 | + if (uploadResult.checksumType) { |
| 61 | + console.log('Checksum type:', uploadResult.checksumType); |
| 62 | + } |
| 63 | + |
| 64 | + // Verify the upload was successful |
| 65 | + if (uploadResult.eTag) { |
| 66 | + console.log('Upload successful, ETag received:', uploadResult.eTag); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +{{< /code >}} |
| 72 | + |
| 73 | +## Usage in Multipart Uploads |
| 74 | + |
| 75 | +For multipart uploads, the `S3UploadedObject` is returned by the `completeMultipartUpload` method: |
| 76 | + |
| 77 | +{{< code >}} |
| 78 | + |
| 79 | +```javascript |
| 80 | +import crypto from 'k6/crypto'; |
| 81 | +import { |
| 82 | + AWSConfig, |
| 83 | + S3Client, |
| 84 | +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; |
| 85 | + |
| 86 | +const awsConfig = new AWSConfig({ |
| 87 | + region: __ENV.AWS_REGION, |
| 88 | + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, |
| 89 | + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, |
| 90 | +}); |
| 91 | + |
| 92 | +const s3 = new S3Client(awsConfig); |
| 93 | + |
| 94 | +export default async function () { |
| 95 | + const testBucketName = 'test-jslib-aws'; |
| 96 | + const testFileKey = 'large-file.txt'; |
| 97 | + |
| 98 | + // Create large file data |
| 99 | + const bigFile = crypto.randomBytes(12 * 1024 * 1024); // 12MB |
| 100 | + |
| 101 | + // Initialize multipart upload |
| 102 | + const multipartUpload = await s3.createMultipartUpload(testBucketName, testFileKey); |
| 103 | + |
| 104 | + // Upload parts |
| 105 | + const firstPart = await s3.uploadPart( |
| 106 | + testBucketName, |
| 107 | + testFileKey, |
| 108 | + multipartUpload.uploadId, |
| 109 | + 1, |
| 110 | + bigFile.slice(0, 6 * 1024 * 1024) |
| 111 | + ); |
| 112 | + |
| 113 | + const secondPart = await s3.uploadPart( |
| 114 | + testBucketName, |
| 115 | + testFileKey, |
| 116 | + multipartUpload.uploadId, |
| 117 | + 2, |
| 118 | + bigFile.slice(6 * 1024 * 1024) |
| 119 | + ); |
| 120 | + |
| 121 | + // Complete the multipart upload and get the result |
| 122 | + const uploadResult = await s3.completeMultipartUpload( |
| 123 | + testBucketName, |
| 124 | + testFileKey, |
| 125 | + multipartUpload.uploadId, |
| 126 | + [firstPart, secondPart] |
| 127 | + ); |
| 128 | + |
| 129 | + // Access the completed upload metadata |
| 130 | + console.log('Multipart upload ETag:', uploadResult.eTag); |
| 131 | + console.log('Checksum type:', uploadResult.checksumType); // Likely "COMPOSITE" |
| 132 | + |
| 133 | + if (uploadResult.size) { |
| 134 | + console.log('Total object size:', uploadResult.size); |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +{{< /code >}} |
0 commit comments