uploadPart
S3Client.uploadPart
将分段上传的一部分上传到存储桶中。
参数 | 类型 | 描述 |
---|---|---|
bucketName | 字符串 | 要上传对象的存储桶名称。 |
objectKey | 字符串 | 要上传的对象名称。 |
uploadId | 字符串 | 分段上传的 UploadId。 |
partNumber | 数字 | 要上传的部分编号。 |
数据 | 字符串 | ArrayBuffer | 要上传的部分内容。 |
返回值
类型 | 描述 |
---|---|
Promise< S3Part> | 一个 Promise,它以表示 S3 分段上传的 S3Part 对象完成。 |
示例
import crypto from 'k6/crypto';
import exec from 'k6/execution';
import {
AWSConfig,
S3Client,
} from 'https://jslib.k6.io/aws/0.13.0/s3.js';
const awsConfig = new AWSConfig({
region: __ENV.AWS_REGION,
accessKeyId: __ENV.AWS_ACCESS_KEY_ID,
secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY,
sessionToken: __ENV.AWS_SESSION_TOKEN,
});
const s3 = new S3Client(awsConfig);
const testBucketName = 'test-jslib-aws';
const testFileKey = 'multipart.txt';
export default async function () {
// Produce random bytes to upload of size ~12MB, that
// we will upload in two 6MB parts. This is done as the
// minimum part size supported by S3 is 5MB.
const bigFile = crypto.randomBytes(12 * 1024 * 1024);
// Initialize a multipart upload
const multipartUpload = await s3.createMultipartUpload(testBucketName, testFileKey);
// Upload the first part
const firstPartData = bigFile.slice(0, 6 * 1024 * 1024);
const firstPart = await s3.uploadPart(
testBucketName,
testFileKey,
multipartUpload.uploadId,
1,
firstPartData
);
// Upload the second part
const secondPartData = bigFile.slice(6 * 1024 * 1024, 12 * 1024 * 1024);
const secondPart = await s3.uploadPart(
testBucketName,
testFileKey,
multipartUpload.uploadId,
2,
secondPartData
);
// Complete the multipart upload
await s3.completeMultipartUpload(testBucketName, testFileKey, multipartUpload.uploadId, [
firstPart,
secondPart,
]);
// Let's redownload it verify it's correct, and delete it
const obj = await s3.getObject(testBucketName, testFileKey);
await s3.deleteObject(testBucketName, testFileKey);
}
一个 k6 脚本,将分段上传的一部分内容上传到 S3 存储桶中