翻译:
这个 HttpHandler 是基于 XMLHttpRequest 并可以替换,如果
需要特定用途 fetch。 。 。 .
警告:警告:
建议 HttpHandler 对于类似浏览器的环境是 @smithy/fetch-http-handler, (中文(简体) ),
这是默认.
此替代品仅经过测试 S3 在浏览器中.
圆柱体
本套件可访问以下全球范围的实施:
XMLHttpRequestTextEncoderTransformStreamBlob
您必须提供多填充, 例如: TextEncoder 财务报告和审定财务报表 TransformStream,用于环境
不以当地方式执行.
使用大小写: XMLHttp 请上传进度事件
使用 Upload 类从 @aws-sdk/lib-storage 软件包为普通,但提供不同的
HttpHandler 创建时 S3Client 或者说 S3 对象.
另见: 自由储存/README.md。 。 。 .
import { XhrHttpHandler } from "@aws-sdk/xhr-http-handler";
import { S3Client } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
const client = new S3Client({
requestHandler: new XhrHttpHandler({}), // overrides default FetchHttpHandler in browsers.
});
const upload = new Upload({
client,
params: {/* ... */},
});
upload.on("httpUploadProgress", (progress) => {
// Note, this event will be emitted much more frequently when using the XhrHttpHandler.
// Your application should be ready to throttle the event listener if it is
// computationally expensive.
// The default FetchHttpHandler only emits this event upon the completion of each
// part, a minimum of 5 MB. Using XHR will emit this event continuously, including
// for files smaller than the chunk size, which use single-part upload.
console.log(progress);
console.log(
progress.loaded, // Bytes uploaded so far.
progress.total // Total bytes. Divide these two for progress percentage.
);
});
const completeMultiPartUpload = await upload.done();
使用大小写: XMLHttp 请求下载进度和其他事件
XhrHttpHandler 扩展 EventEmitter。 。 。 .
下载进度
import { XhrHttpHandler } from "@aws-sdk/xhr-http-handler";
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
const handler = new XhrHttpHandler({});
handler.on(XhrHttpHandler.EVENTS.PROGRESS, (progress, request) => {
if (progress.lengthComputable) {
console.log(
progress.loaded, // bytes
progress.total // bytes
);
console.log(
request // contains the request information to differentiate
// requests from the same handler.
);
}
});
const client = new S3Client({
requestHandler: handler,
});
await client.send(new GetObjectCommand(/*...*/));
访问 XMLHttpRequest 对象.
您可以访问 XMLHttpRequest 检查或检查对象
附加附加事件听众.
import { XhrHttpHandler } from "@aws-sdk/xhr-http-handler";
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
const handler = new XhrHttpHandler({});
handler.on(XhrHttpHandler.EVENTS.XHR_INSTANTIATED, (xhr) => {
// a new XMLHttpRequest is created for each command sent.
// this is immediately after instantiation.
});
handler.on(XhrHttpHandler.EVENTS.BEFORE_XHR_SEND, (xhr) => {
// a new XMLHttpRequest is created for each command sent.
// this is immediately before calling `xhr.send(body)`.
});
const client = new S3Client({
requestHandler: handler,
});
await client.send(new GetObjectCommand(/*...*/));
你可以检查来源 .ts 文件或已发布 .d.ts 文件
或检查 XhrHttpHandler.EVENTS
运行时的对象.