putObject
S3Client.putObject
将对象上传到存储桶。
参数
参数 | 类型 | 描述 |
---|---|---|
bucketName | string | 要上传对象的存储桶名称。 |
objectKey | string | 上传对象的名称。 |
data | string | ArrayBuffer | 要上传的对象内容。 |
params | PutObjectParams (可选) | 请求的选项。 |
PutObjectParams
名称 | 类型 | 描述 |
---|---|---|
contentDisposition | string (可选) | 指定对象的展示信息。更多信息请参阅 RFC 6266。 |
contentEncoding | string (可选) | 指定已应用于对象的内容编码,以及必须应用哪些解码机制才能获取 Content-Type 头部字段引用的媒体类型。更多信息请参阅 RFC 2616。 |
contentLength | number (可选) | 请求体的大小(字节)。当请求体大小无法自动确定时,此参数很有用。 |
contentMD5 | string (可选) | 根据 RFC 1864 对消息体(不包含头部)进行的 base64 编码的 128 位 MD5 摘要。此头部可用作消息完整性检查,以验证接收到的消息与发送的消息是否完全一致。 |
contentType | string (可选) | 描述对象数据格式的标准 MIME 类型。更多信息请参阅 RFC 2616。 |
返回值
类型 | 描述 |
---|---|
Promise<void> | 一个 Promise,在对象上传到 S3 存储桶后完成。 |
示例
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,
});
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
await s3.putObject(testBucketName, testFileKey, testFile, {
contentType: 'text/plain',
contentLength: testFile.length,
});
// And let's redownload it to verify it's correct
const obj = await s3.getObject(testBucketName, testFileKey);
console.log(JSON.stringify(obj));
}
一个将对象上传到 S3 存储桶的 k6 脚本